Skip to content
Draft
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
8 changes: 6 additions & 2 deletions app/controllers/pages/exit_page_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Pages::ExitPageController < PagesController
before_action :can_add_page_routing, only: %i[new create delete destroy]
before_action :ensure_answer_value_present, only: %i[new create]
before_action :ensure_answer_value_present, only: %i[new create], unless: -> { current_form&.group.present? && FeatureService.new(group: current_form.group).enabled?(:multiple_branches) }

def new
exit_page_input = Pages::ExitPageInput.new(form: current_form, page:, answer_value: params[:answer_value])
Expand Down Expand Up @@ -81,7 +81,11 @@ def render_preview
private

def can_add_page_routing
authorize current_form, :can_add_page_routing_conditions?
if current_form&.group.present? && FeatureService.new(group: current_form.group).enabled?(:multiple_branches)
authorize current_form, :can_edit_form?
else
authorize current_form, :can_add_page_routing_conditions?
end
end

def exit_page_input_params
Expand Down
3 changes: 3 additions & 0 deletions app/input_objects/pages/conditions_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def update_condition

record.skip_to_end = skip_to_end
record.goto_page_id = goto_page_id
exit_page_to_delete = record&.exit_page
record.exit_page_id = nil
exit_page_to_delete.destroy! if exit_page_to_delete
record.exit_page_heading = nil
record.exit_page_markdown = nil
end
Expand Down
66 changes: 63 additions & 3 deletions app/models/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,52 @@ class Condition < ApplicationRecord
has_one :form, through: :routing_page
belongs_to :exit_page, optional: true

accepts_nested_attributes_for :exit_page, update_only: true
validates_associated :exit_page

before_destroy :destroy_postconditions

translates :exit_page_heading
translates :exit_page_markdown, presence: false # Without presence here, the value is nil if set to ""

def self.create_and_update_form!(...)
condition = Condition.new(...)
def self.create_and_update_form!(**args)
exit_page_attributes = args.slice(:exit_page_heading, :exit_page_markdown)

condition = Condition.new(**args)

if exit_page_attributes.present?
heading = exit_page_attributes.fetch(:exit_page_heading, "")
markdown = exit_page_attributes.fetch(:exit_page_markdown, "")
condition.exit_page = ExitPage.create(heading:, markdown:, question_page: condition.routing_page)
end

condition.save_and_update_form
condition
end

def save_and_update_form
def save_and_update_form(**args)
exit_page_attributes = args.slice(:exit_page_heading, :exit_page_markdown)

if exit_page_attributes.present?
heading = exit_page_attributes.fetch(:exit_page_heading, "")
markdown = exit_page_attributes.fetch(:exit_page_markdown, "")

if exit_page.present?
exit_page.update!(heading:, markdown:)
else
ExitPage.create!(heading:, markdown:, question_page: routing_page)
end
end

save!
form.save_question_changes!
end

def destroy_and_update_form!
exit_page_to_delete = exit_page
destroy! && form.save_question_changes!

exit_page_to_delete.presence&.destroy! unless FeatureService.new(group: form.group).enabled?(:multiple_branches)
end

def validation_errors
Expand All @@ -40,6 +68,38 @@ def validation_errors
].compact
end

def exit_page_heading(**args)
return exit_page&.heading(**args) if exit_page.present?

super(**args)
end

def exit_page_markdown(**args)
return exit_page&.markdown(**args) if exit_page.present?

super(**args)
end

def exit_page_heading=(value, **args)
if exit_page.present?
exit_page.send(:heading=, value, **args)
else
attributes[:exit_page] = ExitPage.new(heading: value, question_page: routing_page, **args)
end

super(value, **args)
end

def exit_page_markdown=(value, **args)
if exit_page.present?
exit_page.send(:markdown=, value, **args)
else
attributes[:exit_page] = ExitPage.new(markdown: value, question_page: routing_page, **args)
end

super(value, **args)
end

def warning_goto_page_doesnt_exist
return nil if is_exit_page?
# goto_page_id isn't needed if the route is skipping to the end of the form
Expand Down
24 changes: 24 additions & 0 deletions spec/models/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,28 @@
end
end
end

describe "using the new ExitPage model" do
describe "#exit_page_heading" do
let(:condition) { create :condition, exit_page_heading: "English heading", exit_page_heading_cy: "Welsh heading" }

it "returns the heading" do
expect(condition.exit_page_heading).to eq("English heading")
end

context "when the condition has an ExitPage" do
before do
condition.exit_page = create :exit_page, question_page: condition.routing_page, heading: "Exit page english heading", heading_cy: "Exit page welsh heading"
end

it "returns the heading from the ExitPage" do
expect(condition.exit_page_heading).to eq("Exit page english heading")
end

it "returns the welsh heading if the condition an ExitPage" do
expect(condition.exit_page_heading_cy).to eq("Exit page welsh heading")
end
end
end
end
end
7 changes: 7 additions & 0 deletions spec/requests/pages/exit_page_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@
expect(response.status).to eq(403)
expect(response).to render_template("errors/forbidden")
end

context "when mutlple branches is enabled", :multiple_branches do
it "Renders the forbidden page" do
expect(response.status).to eq(403)
expect(response).to render_template("errors/forbidden")
end
end
end
end

Expand Down
Loading