-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcopy_controller.rb
More file actions
57 lines (46 loc) · 1.55 KB
/
copy_controller.rb
File metadata and controls
57 lines (46 loc) · 1.55 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
module Forms
class CopyController < FormsController
def copy
authorize current_form, :copy?
form = if tag == "live"
current_live_form
elsif tag == "archived"
current_archived_form
else
current_form
end
set_back_link(form)
@copy_input = Forms::CopyInput.new(form: form, tag: tag).assign_form_values
render :confirm
end
def create
authorize current_form, :copy?
@copy_input = Forms::CopyInput.new(copy_input_params(current_form))
if @copy_input.submit
copied_form = FormCopyService.new(current_form, current_user).copy(tag: @copy_input.tag)
copied_form.update!(name: @copy_input.name)
redirect_to form_path(copied_form.id), success: t("banner.success.form.copied")
else
set_back_link(current_form)
render :confirm
end
end
private
def copy_input_params(form)
params.require(:forms_copy_input).permit(:name, :tag).merge(form:)
end
def tag
return "draft" if params[:tag].blank?
params[:tag]
end
def set_back_link(form)
@back_link = if tag == "live"
{ url: live_form_path(form.id), body: I18n.t("back_link.form_view") }
elsif tag == "archived"
{ url: archived_form_path(form.id), body: I18n.t("back_link.form_view") }
else
{ url: form_path(form.id), body: I18n.t("back_link.form_edit") }
end
end
end
end