-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwelsh_translation_controller.rb
More file actions
102 lines (80 loc) · 3.47 KB
/
welsh_translation_controller.rb
File metadata and controls
102 lines (80 loc) · 3.47 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
module Forms
class WelshTranslationController < FormsController
def new
authorize current_form, :can_edit_form?
return redirect_to form_path(current_form) unless welsh_enabled?
@welsh_translation_input = WelshTranslationInput.new(form: form_with_pages_and_conditions).assign_form_values
@table_presenter = Forms::TranslationTablePresenter.new
end
def create
authorize current_form, :can_edit_form?
return redirect_to form_path(current_form) unless welsh_enabled?
@welsh_translation_input = WelshTranslationInput.new(welsh_translation_params)
@table_presenter = Forms::TranslationTablePresenter.new
if @welsh_translation_input.submit
success_message = if @welsh_translation_input.mark_complete == "true"
t("banner.success.form.welsh_translation_saved_and_completed")
else
t("banner.success.form.welsh_translation_saved")
end
redirect_to form_path(@welsh_translation_input.form), success: success_message
else
render :new, status: :unprocessable_content
end
end
def delete
authorize current_form, :can_edit_form?
return redirect_to form_path(current_form) unless welsh_enabled?
@delete_welsh_translation_input = Forms::DeleteWelshTranslationInput.new(form: current_form)
end
def destroy
authorize current_form, :can_edit_form?
return redirect_to form_path(current_form) unless welsh_enabled?
@delete_welsh_translation_input = Forms::DeleteWelshTranslationInput.new(delete_welsh_translation_params)
if @delete_welsh_translation_input.submit
if @delete_welsh_translation_input.confirmed?
redirect_to form_path(@delete_welsh_translation_input.form), success: t(".success")
else
redirect_to welsh_translation_path(@delete_welsh_translation_input.form)
end
else
render :delete, status: :unprocessable_content
end
end
def render_preview
authorize current_form, :can_view_form?
render json: { preview_html:, errors: [] }.to_json
end
def download
authorize current_form, :can_edit_form?
form_content_service = WelshCsvService.new(form_with_pages_and_conditions)
send_data form_content_service.as_csv,
type: "text/csv; charset=iso-8859-1",
disposition: "attachment; filename=#{form_content_service.filename}"
end
private
def preview_html
return t("guidance.no_guidance_added_html") if params[:markdown].blank?
GovukFormsMarkdown.render(params[:markdown], locale: "cy")
end
def welsh_enabled?
FeatureService.new(group: current_form.group).enabled?(:welsh)
end
def welsh_translation_params
params.require(:forms_welsh_translation_input).permit(
*WelshTranslationInput.attribute_names,
page_translations_attributes: [
*WelshPageTranslationInput.attribute_names,
{ selection_options_cy_attributes: %i[id name_cy] },
{ condition_translations_attributes: WelshConditionTranslationInput.attribute_names },
],
).merge(form: current_form)
end
def delete_welsh_translation_params
params.require(:forms_delete_welsh_translation_input).permit(:confirm).merge(form: current_form)
end
def form_with_pages_and_conditions
Form.includes(pages: [:routing_conditions]).find(current_form.id)
end
end
end