-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathguidance_controller.rb
More file actions
78 lines (64 loc) · 2.73 KB
/
guidance_controller.rb
File metadata and controls
78 lines (64 loc) · 2.73 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
class Pages::GuidanceController < PagesController
def new
guidance_input = Pages::GuidanceInput.new(page_heading: draft_question.page_heading,
guidance_markdown: draft_question.guidance_markdown)
back_link = new_question_path(current_form.id)
render :guidance, locals: view_locals(nil, guidance_input, back_link)
end
def create
guidance_input = Pages::GuidanceInput.new(guidance_input_params)
back_link = new_question_path(current_form.id)
case route_to
when :preview
guidance_input.valid?
render :guidance, locals: view_locals(nil, guidance_input, back_link)
when :save_and_continue
if guidance_input.submit
redirect_to new_question_path(current_form.id)
else
render :guidance, locals: view_locals(nil, guidance_input, back_link), status: :unprocessable_content
end
end
end
def edit
guidance_input = Pages::GuidanceInput.new(page_heading: draft_question.page_heading,
guidance_markdown: draft_question.guidance_markdown)
back_link = edit_question_path(current_form.id, page.id)
render :guidance, locals: view_locals(page, guidance_input, back_link)
end
def update
guidance_input = Pages::GuidanceInput.new(guidance_input_params)
back_link = edit_question_path(current_form.id, page.id)
case route_to
when :preview
guidance_input.valid?
render :guidance, locals: view_locals(page, guidance_input, back_link)
when :save_and_continue
if guidance_input.submit
redirect_to edit_question_path(current_form.id, page.id)
else
render :guidance, locals: view_locals(page, guidance_input, back_link), status: :unprocessable_content
end
end
end
def render_preview
guidance_input = Pages::GuidanceInput.new(guidance_markdown: params[:markdown])
guidance_input.validate
render json: { preview_html: preview_html(guidance_input), errors: guidance_input.errors[:guidance_markdown] }.to_json
end
private
def guidance_input_params
params.require(:pages_guidance_input).permit(:page_heading, :guidance_markdown).merge(draft_question:)
end
def route_to
params[:route_to].to_sym
end
def preview_html(guidance_input)
return t("markdown_editor.no_markdown_content_html") if guidance_input.guidance_markdown.blank?
GovukFormsMarkdown.render(guidance_input.guidance_markdown, locale: "en")
end
def view_locals(current_page, guidance_input, back_link)
post_url = current_page.present? && current_page.id.present? ? guidance_edit_path : guidance_new_path
{ current_form:, page: @page, guidance_input:, preview_html: preview_html(guidance_input), post_url:, back_link: }
end
end