Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/controllers/forms/declaration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ class DeclarationController < FormsController
def new
authorize current_form, :can_view_form?
@declaration_input = DeclarationInput.new(form: current_form).assign_form_values
@preview_html = preview_html(@declaration_input)
end

def create
authorize current_form, :can_view_form?
@declaration_input = DeclarationInput.new(**declaration_input_params)
@preview_html = preview_html(@declaration_input)

if @declaration_input.submit
success_message = if @declaration_input.mark_complete == "true"
Expand All @@ -22,10 +24,24 @@ def create
end
end

def render_preview
authorize current_form, :can_view_form?
@declaration_input = DeclarationInput.new(declaration_markdown: params[:markdown])
@declaration_input.validate

render json: { preview_html: preview_html(@declaration_input), errors: @declaration_input.errors[:declaration_markdown] }.to_json
end

private

def declaration_input_params
params.require(:forms_declaration_input).permit(:declaration_text, :mark_complete).merge(form: current_form)
params.require(:forms_declaration_input).permit(:declaration_markdown, :mark_complete).merge(form: current_form)
end

def preview_html(declaration_input)
return t("markdown_editor.no_markdown_content_html") if declaration_input.declaration_markdown.blank?

GovukFormsMarkdown.render(declaration_input.declaration_markdown, allow_headings: false)
end
end
end
11 changes: 5 additions & 6 deletions app/input_objects/forms/declaration_input.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
class Forms::DeclarationInput < Forms::MarkCompleteInput
include TextInputHelper

attr_accessor :declaration_text
attr_accessor :declaration_markdown

validates :declaration_text, length: { maximum: 2000 }
validates :declaration_markdown, length: { maximum: 2000 }

before_validation :strip_carriage_returns_from_input

def submit
return false if invalid?

form.declaration_text = declaration_text
form.declaration_markdown = MarkdownConversionService.new(declaration_text).to_markdown
form.declaration_markdown = declaration_markdown
form.declaration_section_completed = mark_complete
form.save_draft!
end

def assign_form_values
self.declaration_text = form.declaration_text
self.declaration_markdown = form.declaration_markdown
self.mark_complete = form.try(:declaration_section_completed)
self
end

private

def strip_carriage_returns_from_input
strip_carriage_returns!(declaration_text)
strip_carriage_returns!(declaration_markdown)
end
end
13 changes: 6 additions & 7 deletions app/input_objects/forms/welsh_translation_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Forms::WelshTranslationInput < Forms::MarkCompleteInput
attribute :support_phone_cy
attribute :support_url_cy
attribute :support_url_text_cy
attribute :declaration_text_cy
attribute :declaration_markdown_cy
attribute :what_happens_next_markdown_cy
attribute :payment_url_cy

Expand All @@ -35,8 +35,8 @@ class Forms::WelshTranslationInput < Forms::MarkCompleteInput
validates :support_url_text_cy, presence: true, if: -> { marked_complete? && form_has_support_url? }
validates :support_url_text_cy, length: { maximum: 120 }, if: -> { support_url_text_cy.present? }

validates :declaration_text_cy, presence: true, if: -> { marked_complete? && form_has_declaration? }
validates :declaration_text_cy, length: { maximum: 2000 }, if: -> { declaration_text_cy.present? }
validates :declaration_markdown_cy, presence: true, if: -> { marked_complete? && form_has_declaration? }
validates :declaration_markdown_cy, length: { maximum: 2000 }, if: -> { declaration_markdown_cy.present? }

validates :what_happens_next_markdown_cy, presence: true, if: -> { marked_complete? && form.what_happens_next_markdown.present? }
validates :what_happens_next_markdown_cy, markdown: { allow_headings: false }, if: -> { what_happens_next_markdown_cy.present? }
Expand Down Expand Up @@ -74,8 +74,7 @@ def submit
return false if invalid?

form.name_cy = name_cy
form.declaration_text_cy = form_has_declaration? ? declaration_text_cy : nil
form.declaration_markdown_cy = form_has_declaration? ? MarkdownConversionService.new(form.declaration_text_cy).to_markdown : nil
form.declaration_markdown_cy = form_has_declaration? ? declaration_markdown_cy : nil
form.payment_url_cy = form_has_payment_url? ? payment_url_cy : nil
form.privacy_policy_url_cy = privacy_policy_url_cy
form.support_email_cy = form_has_support_email? ? support_email_cy : nil
Expand Down Expand Up @@ -111,7 +110,7 @@ def assign_form_values
self.support_phone_cy = form.support_phone_cy
self.support_url_cy = form.support_url_cy
self.support_url_text_cy = form.support_url_text_cy
self.declaration_text_cy = form.declaration_text_cy
self.declaration_markdown_cy = form.declaration_markdown_cy
self.what_happens_next_markdown_cy = form.what_happens_next_markdown_cy
self.payment_url_cy = form.payment_url_cy

Expand All @@ -125,7 +124,7 @@ def assign_form_values
end

def form_has_declaration?
form.declaration_text.present?
form.declaration_markdown.present?
end

def form_has_payment_url?
Expand Down
3 changes: 1 addition & 2 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ def has_welsh_translation?
def normalise_welsh!
return unless available_languages.include?("cy")

self.declaration_text_cy = nil if declaration_text.blank?
self.declaration_markdown_cy = nil if declaration_text_cy.blank?
self.declaration_markdown_cy = nil if declaration_markdown.blank?
self.payment_url_cy = nil if payment_url.blank?
self.support_email_cy = nil if support_email.blank?
self.support_phone_cy = nil if support_phone.blank?
Expand Down
2 changes: 1 addition & 1 deletion app/services/task_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def pages_status

def declaration_status
return :completed if @form.declaration_section_completed
return :in_progress if @form.declaration_text.present?
return :in_progress if @form.declaration_markdown.present?

:not_started
end
Expand Down
10 changes: 6 additions & 4 deletions app/views/forms/_made_live_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<h3 class="govuk-heading-m"><%= t('.questions') %></h3>
<p><%= govuk_link_to t('.questions_link', count: form_document.steps.count), questions_path %></p>

<% if form_document.declaration_text.present? %>
<% if form_document.declaration_markdown.present? %>
<% if form_document.has_welsh_translation? %>
<%= govuk_summary_card(title: t('.declaration')) do |card| %>
<%= govuk_table do |table| %>
Expand All @@ -69,10 +69,10 @@
<%= table.with_body do |body|
body.with_row do |row|
row.with_cell do
form_document.declaration_text
GovukFormsMarkdown.render(form_document.declaration_markdown, locale: "en").html_safe
end
row.with_cell do
welsh_form_document.declaration_text
GovukFormsMarkdown.render(welsh_form_document.declaration_markdown, locale: "cy").html_safe
end
end
end %>
Expand All @@ -81,7 +81,9 @@
<% end %>
<% else %>
<h3 class="govuk-heading-m"><%= t('.declaration') %></h3>
<p><%= form_document.declaration_text %></p>
<div class="app-preview-area">
<%= GovukFormsMarkdown.render(form_document.declaration_markdown, locale: "en").html_safe %>
</div>
<%= govuk_details(summary_text: t('.what_is_declaration'), text: t('.declaration_description')) %>
<% end %>
<% end %>
Expand Down
9 changes: 8 additions & 1 deletion app/views/forms/declaration/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@

<%= t("declaration_input.new.body_html") %>

<%= f.govuk_text_area :declaration_text, max_chars: 2000, label: {size: 'm' } %>
<%= render MarkdownEditorComponent::View.new(:declaration_markdown,
form_builder: f,
render_preview_path: declaration_render_preview_path(@declaration_input.form.id),
preview_html: @preview_html,
form_model: @declaration_input,
label: "Enter a declaration for people to agree to (optional)",
hint: nil,
allow_headings: true) %>

<%= render MarkCompleteComponent::View.new(generate_form: false, form_builder: f, form_model: @declaration_input) %>

Expand Down
13 changes: 11 additions & 2 deletions app/views/forms/welsh_translation/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,18 @@

table.with_body do |body|
body.with_row do |row|
row.with_cell(text: @welsh_translation_input.form.declaration_text, classes: "govuk-!-text-break-word")
row.with_cell(text: @welsh_translation_input.form.declaration_markdown, classes: "govuk-!-text-break-word")
row.with_cell do
f.govuk_text_area :declaration_text_cy, lang: "cy"
f.govuk_text_area :declaration_markdown_cy, lang: "cy"
render MarkdownEditorComponent::View.new(:declaration_markdown_cy,
form_builder: f,
render_preview_path: welsh_translation_render_preview_path(@welsh_translation_input.form.id),
preview_html: nil,
form_model: @welsh_translation_input,
label: t("helpers.label.forms_welsh_translation_input.declaration_markdown_cy"),
lang: "cy",
allow_headings: true,
label_heading: false)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions config/locales/input_objects/declaration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ en:
models:
forms/declaration_input:
attributes:
declaration_text:
declaration_markdown:
too_long: The declaration cannot be longer than 2,000 characters
mark_complete:
blank: You must choose an option
helpers:
label:
forms_declaration_input:
declaration_text: Enter a declaration for people to agree to (optional)
declaration_markdown: Enter a declaration for people to agree to (optional)
4 changes: 2 additions & 2 deletions config/locales/input_objects/welsh_translation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ en:
models:
forms/welsh_translation_input:
attributes:
declaration_text_cy:
declaration_markdown_cy:
blank: Enter a declaration in Welsh
too_long: The declaration cannot be longer than 2,000 characters
mark_complete:
Expand Down Expand Up @@ -41,7 +41,7 @@ en:
helpers:
label:
forms_welsh_translation_input:
declaration_text_cy: Enter your Welsh declaration
declaration_markdown_cy: Enter your Welsh declaration
name_cy: Enter your Welsh form name
payment_url_cy: Enter Welsh GOV.UK Pay payment link
privacy_policy_url_cy: Enter link to your Welsh privacy information
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
post "/contact-details" => "forms/contact_details#create", as: :contact_details_create
get "/declaration" => "forms/declaration#new", as: :declaration
post "/declaration" => "forms/declaration#create", as: :declaration_create
post "/declaration-preview" => "forms/declaration#render_preview", as: :declaration_render_preview
get "/payment-link" => "forms/payment_link#new", as: :payment_link
post "/payment-link" => "forms/payment_link#create", as: :payment_link_create
get "/share-preview" => "forms/share_preview#new", as: :share_preview
Expand Down
12 changes: 6 additions & 6 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
),
],
question_section_completed: true,
declaration_text: "",
declaration_markdown: "",
declaration_section_completed: true,
privacy_policy_url: "https://www.gov.uk/help/privacy-notice",
submission_email:,
Expand All @@ -233,7 +233,7 @@
),
],
question_section_completed: true,
declaration_text: "",
declaration_markdown: "",
declaration_section_completed: true,
privacy_policy_url: "https://www.gov.uk/help/privacy-notice",
submission_email:,
Expand Down Expand Up @@ -310,7 +310,7 @@
),
],
question_section_completed: true,
declaration_text: "",
declaration_markdown: "",
declaration_section_completed: true,
privacy_policy_url: "https://www.gov.uk/help/privacy-notice",
submission_email:,
Expand Down Expand Up @@ -382,7 +382,7 @@
),
],
question_section_completed: true,
declaration_text: "",
declaration_markdown: "",
declaration_section_completed: true,
privacy_policy_url: "https://www.gov.uk/help/privacy-notice",
submission_email:,
Expand Down Expand Up @@ -443,8 +443,8 @@
),
],
question_section_completed: true,
declaration_text: "",
declaration_text_cy: "",
declaration_markdown: "",
declaration_markdown_cy: "",
declaration_section_completed: true,
privacy_policy_url: "https://www.gov.uk/help/welsh-privacy-notice",
privacy_policy_url_cy: "https://www.gov.uk/help/welsh-privacy-notice",
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/models/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
support_url { nil }
support_url_text { nil }
what_happens_next_markdown { nil }
declaration_text { nil }
declaration_markdown { nil }
question_section_completed { false }
declaration_section_completed { false }
share_preview_completed { false }
Expand Down
44 changes: 15 additions & 29 deletions spec/input_objects/forms/declaration_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
describe "validations" do
describe "Character length" do
it "is valid if less than 2000 characters" do
declaration_input = described_class.new(declaration_text: "a", mark_complete: true)
declaration_input = described_class.new(declaration_markdown: "a", mark_complete: true)

expect(declaration_input).to be_valid
end

it "is valid if 2000 characters" do
declaration_input = described_class.new(declaration_text: "a" * 2000, mark_complete: true)
declaration_input = described_class.new(declaration_markdown: "a" * 2000, mark_complete: true)

expect(declaration_input).to be_valid
end

it "is strips carriage returns before calculating the length" do
declaration_text = "#{'a' * 1000}\r\n#{'a' * 999}"
declaration_input = described_class.new(declaration_text:, mark_complete: true)
declaration_markdown = "#{'a' * 1000}\r\n#{'a' * 999}"
declaration_input = described_class.new(declaration_markdown:, mark_complete: true)

expect(declaration_input).to be_valid
end

it "is invalid if more than 2000 characters" do
declaration_input = described_class.new(declaration_text: "a" * 2001, mark_complete: true)
error_message = I18n.t("activemodel.errors.models.forms/declaration_input.attributes.declaration_text.too_long")
declaration_input = described_class.new(declaration_markdown: "a" * 2001, mark_complete: true)
error_message = I18n.t("activemodel.errors.models.forms/declaration_input.attributes.declaration_markdown.too_long")

expect(declaration_input).not_to be_valid

declaration_input.validate(:declaration_text)
declaration_input.validate(:declaration_markdown)

expect(declaration_input.errors.full_messages_for(:declaration_text)).to include(
"Declaration text #{error_message}",
expect(declaration_input.errors.full_messages_for(:declaration_markdown)).to include(
"Declaration markdown #{error_message}",
)
end
end

it "is valid if declaration text is blank" do
declaration_input = described_class.new(declaration_text: "", mark_complete: true)
declaration_input = described_class.new(declaration_markdown: "", mark_complete: true)

expect(declaration_input).to be_valid
end
Expand All @@ -52,31 +52,17 @@

describe "#submit" do
it "returns false if the data is invalid" do
form = OpenStruct.new(declaration_text: "", name: "Apply for a juggling licence")
declaration_input = described_class.new(declaration_text: ("abc" * 2001), form:)
form = OpenStruct.new(declaration_markdown: "", name: "Apply for a juggling licence")
declaration_input = described_class.new(declaration_markdown: ("abc" * 2001), form:)
expect(declaration_input.submit).to be false
end

it "sets the form's attribute values" do
form = OpenStruct.new(declaration_text: "abc", declaration_section_completed: "false")
declaration_input = described_class.new(form:, declaration_text: "new declaration text", mark_complete: "true")
form = OpenStruct.new(declaration_markdown: "abc", declaration_section_completed: "false")
declaration_input = described_class.new(form:, declaration_markdown: "new declaration text", mark_complete: "true")
declaration_input.submit
expect(declaration_input.form.declaration_text).to eq "new declaration text"
expect(declaration_input.form.declaration_markdown).to eq "new declaration text"
expect(declaration_input.form.declaration_section_completed).to eq "true"
end

it "sets the form's declaration_markdown attribute" do
form = OpenStruct.new(declaration_text: nil, declaration_markdown: nil)
declaration_input = described_class.new(form:, declaration_text: "<ul><li>declaration text</li></ul>", mark_complete: "true")
declaration_input.submit
expect(declaration_input.form.declaration_markdown).to eq "* declaration text\n\n"
end

it "sets the form's declaration_markdown to blank if the declaration text is blank" do
form = OpenStruct.new(declaration_text: "original", declaration_markdown: "original")
declaration_input = described_class.new(form:, declaration_text: "", mark_complete: "true")
declaration_input.submit
expect(declaration_input.form.declaration_markdown).to eq ""
end
end
end
Loading
Loading