-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoptions_controller.rb
More file actions
80 lines (71 loc) · 3.98 KB
/
options_controller.rb
File metadata and controls
80 lines (71 loc) · 3.98 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
class Pages::Selection::OptionsController < PagesController
def new
@selection_options_input = Pages::Selection::OptionsInput.new(selection_options: draft_question.answer_settings[:selection_options]
.map { |option| { name: option[:name] } },
include_none_of_the_above: draft_question.is_optional,
draft_question:)
@selection_options_path = selection_options_create_path(current_form.id)
@back_link_url = selection_type_new_path(current_form.id)
@bulk_options_url = selection_bulk_options_new_path(current_form.id)
render "pages/selection/options", locals: { current_form: }
end
def create
@selection_options_input = Pages::Selection::OptionsInput.new(selection_options: selection_options_param_values,
include_none_of_the_above: include_none_of_the_above_param_values,
draft_question:)
@selection_options_path = selection_options_create_path(current_form.id)
@back_link_url = selection_type_new_path(current_form.id)
@bulk_options_url = selection_bulk_options_new_path(current_form.id)
if params[:add_another]
@selection_options_input.add_another
render "pages/selection/options", locals: { current_form: }
elsif params[:remove]
@selection_options_input.remove(params[:remove].to_i)
render "pages/selection/options", locals: { current_form: }
elsif @selection_options_input.submit
redirect_to new_question_path(current_form.id)
else
render "pages/selection/options", locals: { current_form: }
end
end
def edit
@selection_options_input = Pages::Selection::OptionsInput.new(selection_options: draft_question.answer_settings[:selection_options]
.map { |option| { name: option[:name] } },
include_none_of_the_above: draft_question.is_optional,
draft_question:)
@selection_options_path = selection_options_update_path(current_form.id)
@back_link_url = edit_question_path(current_form.id)
@bulk_options_url = selection_bulk_options_edit_path(current_form.id)
render "pages/selection/options", locals: { current_form: }
end
def update
@selection_options_input = Pages::Selection::OptionsInput.new(selection_options: selection_options_param_values,
include_none_of_the_above: include_none_of_the_above_param_values,
draft_question:)
@selection_options_path = selection_options_update_path(current_form.id)
@back_link_url = edit_question_path(current_form.id)
@bulk_options_url = selection_bulk_options_edit_path(current_form.id)
if params[:add_another]
@selection_options_input.add_another
render "pages/selection/options", locals: { current_form: }
elsif params[:remove]
@selection_options_input.remove(params[:remove].to_i)
render "pages/selection/options", locals: { current_form: }
elsif @selection_options_input.submit
redirect_to edit_question_path(current_form.id)
else
render "pages/selection/options", locals: { current_form: }
end
end
private
def selection_options_param_values
input_params[:selection_options] ? input_params[:selection_options].values : []
end
def include_none_of_the_above_param_values
input_params[:include_none_of_the_above]
end
def input_params
params.require(:pages_selection_options_input)
.permit(:include_none_of_the_above, selection_options: [ :name ]).to_h.deep_symbolize_keys
end
end