-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathseeds.rb
88 lines (74 loc) · 2.7 KB
/
seeds.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true
require "faker"
email = ->(subdomain, role) { "#{subdomain}_#{role}@example.com" }
password = ->(env) { env.production? ? PasswordGenerator.call : "nw29nfsijrP!P392" }
fixture = ->(file) { YAML.load_file(File.expand_path("seeds/#{file}.yml", __dir__)) }
fixture["local_authorities"].each do |attrs|
LocalAuthority.find_or_create_by!(attrs.slice("subdomain")) do |lpa|
lpa.assign_attributes(attrs)
end
end
LocalAuthority.find_each do |lpa|
lpa.readonly!
lpa.api_users.find_or_create_by!(name: lpa.subdomain)
%w[assessor reviewer administrator].each do |role|
lpa.users.find_or_create_by!(email: email[lpa.subdomain, role]) do |user|
user.name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
user.password = user.password_confirmation = password[Rails.env]
user.role = role
user.otp_required_for_login = false
end
end
end
User.find_or_create_by!(role: "global_administrator") do |user|
user.role = "global_administrator"
user.password = PasswordGenerator.call
user.otp_required_for_login = false
user.name = "#{Faker::Name.first_name} #{Faker::Name.last_name}"
user.email = "[email protected]"
end
User.find_each do |user|
user.confirm unless user.confirmed?
end
fixture["decisions"].each do |attrs|
Decision.find_or_create_by!(attrs.slice("code")) do |decision|
decision.assign_attributes(attrs)
end
end
fixture["reporting_types"].each do |attrs|
ReportingType.find_or_create_by!(attrs.slice("code")) do |reporting_type|
reporting_type.assign_attributes(attrs)
end
end
fixture["application_types"].each do |attrs|
ApplicationType.find_or_create_by!(attrs.slice("code")) do |application_type|
application_type.assign_attributes(attrs)
end
end
fixture["constraints"].each do |category, types|
types.each do |type|
Constraint.find_or_create_by!(category:, type:)
end
end
# Create GPDO information - https://www.legislation.gov.uk/uksi/2015/596/contents
schedule = PolicySchedule.find_or_create_by!(number: 2, name: "Permitted development rights")
data = fixture["gpdo"]
data["en"]["schedules"].first["parts"].each do |part_key, part_data|
part = schedule.policy_parts.find_or_create_by!(
number: part_key,
name: part_data["name"]
)
part_data["classes"].each do |class_data|
policy_class = part.policy_classes.find_or_create_by!(
section: class_data["section"],
name: class_data["name"],
url: class_data["url"]
)
class_data["policies_attributes"].each do |section_data|
policy_class.policy_sections.find_or_create_by!(
section: section_data["section"].presence || class_data["section"],
description: section_data["description"]
)
end
end
end