-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathforms.rb
More file actions
164 lines (138 loc) · 4.36 KB
/
forms.rb
File metadata and controls
164 lines (138 loc) · 4.36 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
FactoryBot.define do
factory :form, class: "Form" do
sequence(:name) { |n| "Form #{n}" }
submission_email { Faker::Internet.email(domain: "example.gov.uk") }
submission_type { "email" }
submission_format { [] }
privacy_policy_url { Faker::Internet.url(host: "gov.uk") }
support_email { nil }
support_phone { nil }
support_url { nil }
support_url_text { nil }
what_happens_next_markdown { nil }
declaration_markdown { nil }
question_section_completed { false }
declaration_section_completed { false }
share_preview_completed { false }
creator_id { nil }
state { :draft }
payment_url { nil }
external_id { nil }
send_daily_submission_batch { false }
trait :with_group do
transient do
group { nil }
end
after(:build) do |form, evaluator|
g = evaluator.group || FactoryBot.create(:group)
form.instance_variable_set(:@associated_group, g)
form.define_singleton_method(:group) { g }
end
after(:create) do |form, _evaluator|
g = form.instance_variable_get(:@associated_group)
GroupForm.create!(form_id: form.id, group_id: g.id) unless GroupForm.exists?(form_id: form.id, group_id: g.id)
end
end
trait :new_form do
submission_email { "" }
privacy_policy_url { "" }
pages { [] }
state { :draft }
end
trait :with_id do
sequence(:id) { |n| n }
end
trait :with_pages do
transient do
pages_count { 5 }
end
pages do
Array.new(pages_count) { association(:page) }
end
after(:build) do |form|
link_pages_list(form.pages) if form.pages.present?
end
question_section_completed { true }
end
trait :with_text_page do
pages do
Array.new(1) { association(:page, answer_type: "text", answer_settings: { input_type: %w[single_line long_text].sample }) }
end
question_section_completed { true }
end
trait :ready_for_live do
with_pages
support_email { Faker::Internet.email(domain: "example.gov.uk") }
what_happens_next_markdown { "We usually respond to applications within 10 working days." }
question_section_completed { true }
declaration_section_completed { true }
share_preview_completed { true }
end
trait :with_submission_email do
association :form_submission_email
end
trait :live do
ready_for_live
state { :live }
first_made_live_at { Time.zone.now }
after(:create) do |form|
form.available_languages.each do |language|
Mobility.with_locale(language) do
FormDocument.create(form:, tag: "live", content: form.as_form_document(live_at: form.updated_at), language:)
end
end
end
end
trait :live_with_draft do
live
state { :live_with_draft }
end
trait :archived do
ready_for_live
state { :archived }
first_made_live_at { Time.zone.now }
after(:create) do |form|
form.available_languages.each do |language|
Mobility.with_locale(language) do
FormDocument.create(form:, tag: "archived", content: form.as_form_document(live_at: form.updated_at), language:)
end
end
end
end
trait :archived_with_draft do
archived
state { :archived_with_draft }
end
trait :with_support do
support_email { Faker::Internet.email(domain: "example.gov.uk") }
support_phone { Faker::Lorem.paragraph(sentence_count: 2, supplemental: true, random_sentences_to_add: 4) }
support_url { Faker::Internet.url(host: "gov.uk") }
support_url_text { Faker::Lorem.sentence(word_count: 1, random_words_to_add: 4) }
end
trait :ready_for_routing do
transient do
pages_count { 5 }
end
pages do
Array.new(pages_count) { association(:page, :with_selection_settings) }
end
after(:build) do |form|
link_pages_list(form.pages) if form.pages.present?
end
end
trait :missing_pages do
ready_for_live
question_section_completed { false }
end
trait :with_welsh_translation do
available_languages { %w[en cy] }
name_cy { name.prepend("Welsh ") }
end
end
end
def link_pages_list(pages)
pages.to_enum.with_index(1).each do |page, index|
page.position = index
end
pages
end