-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsecondary_skip_controller.rb
More file actions
102 lines (83 loc) · 3.83 KB
/
secondary_skip_controller.rb
File metadata and controls
102 lines (83 loc) · 3.83 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
class Pages::SecondarySkipController < PagesController
before_action :ensure_branch_routing_feature_enabled, :ensure_page_has_skip_condition
before_action :ensure_secondary_skip_blank, only: %i[new create]
before_action :ensure_secondary_skip_exists, only: %i[edit update delete destroy]
def new
secondary_skip_input = Pages::SecondarySkipInput.new(form: current_form, page:)
render locals: {
secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}
end
def create
secondary_skip_input = Pages::SecondarySkipInput.new(secondary_skip_input_params)
if secondary_skip_input.submit
redirect_to show_routes_path(form_id: current_form.id, page_id: page.id)
else
render template: "pages/secondary_skip/new", locals: {
secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}, status: :unprocessable_entity
end
end
def edit
secondary_skip_input = Pages::SecondarySkipInput.new(form: current_form, page:, record: secondary_skip_condition).assign_values
render template: "pages/secondary_skip/edit", locals: {
secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}
end
def update
secondary_skip_input = Pages::SecondarySkipInput.new(secondary_skip_input_params.merge(record: secondary_skip_condition))
if secondary_skip_input.submit
redirect_to show_routes_path(form_id: current_form.id, page_id: page.id)
else
render template: "pages/secondary_skip/edit", locals: {
secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}, status: :unprocessable_entity
end
end
def delete
delete_secondary_skip_input = Pages::DeleteSecondarySkipInput.new(form: current_form, page:, record: secondary_skip_condition)
render template: "pages/secondary_skip/delete", locals: {
delete_secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}
end
def destroy
delete_secondary_skip_input = Pages::DeleteSecondarySkipInput.new(delete_secondary_skip_input_params.merge(record: secondary_skip_condition))
if delete_secondary_skip_input.submit
redirect_to show_routes_path(form_id: current_form.id, page_id: page.id)
else
render template: "pages/secondary_skip/delete", locals: {
delete_secondary_skip_input:,
back_link_url: show_routes_path(form_id: current_form.id, page_id: page.id),
}, status: :unprocessable_entity
end
end
private
def secondary_skip_input_params
params.require(:pages_secondary_skip_input).permit(:routing_page_id, :goto_page_id).merge(form: current_form, page:)
end
def delete_secondary_skip_input_params
params.require(:pages_delete_secondary_skip_input).permit(:confirm).merge(form: current_form, page:)
end
def ensure_branch_routing_feature_enabled
raise ActionController::RoutingError, "branch_routing feature not enabled" unless FeatureService.new(group: current_form.group).enabled?(:branch_routing)
end
def ensure_page_has_skip_condition
unless page.routing_conditions.any? { |c| c.answer_value.present? }
redirect_to form_pages_path(current_form.id)
end
end
def ensure_secondary_skip_exists
redirect_to show_routes_path(form_id: current_form.id, page_id: page.id) if secondary_skip_condition.blank?
end
def ensure_secondary_skip_blank
redirect_to show_routes_path(form_id: current_form.id, page_id: page.id) if secondary_skip_condition.present?
end
def secondary_skip_condition
@secondary_skip_condition ||= FormRepository.pages(current_form).flat_map(&:routing_conditions).compact_blank.find { |c| c.secondary_skip? && c.check_page_id == page.id }
end
end