From 616528cd1598cb00fe4ccf20daf668eabce18080 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 12 Apr 2023 09:29:04 +0100 Subject: [PATCH] Add organisations page, controller and merge model --- app/controllers/merge_controller.rb | 42 ++++++++++++++++++++++++++ app/models/merge.rb | 6 ++++ app/views/merge/organisations.html.erb | 41 +++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 app/controllers/merge_controller.rb create mode 100644 app/models/merge.rb create mode 100644 app/views/merge/organisations.html.erb diff --git a/app/controllers/merge_controller.rb b/app/controllers/merge_controller.rb new file mode 100644 index 000000000..5a3f041a2 --- /dev/null +++ b/app/controllers/merge_controller.rb @@ -0,0 +1,42 @@ +class MergeController < ApplicationController + before_action :authenticate_user! + + def show + render form.view_path + end + + def update + if form.valid? && form.save! + redirect_to form.next_path + else + render form.view_path + end + end + + def organisations + @answer_options = answer_options + @merge = Merge.new(form_params) + @merging_organisations_list = Organisation.where(id: @merge.merging_organisations) + end + + def answer_options + answer_options = { "" => "Select an option" } + + Organisation.all.pluck(:id, :name).each do |organisation| + answer_options[organisation[0]] = organisation[1] + end + answer_options + end + +private + + def form_params + merge_params = params.fetch(:merge, {}).permit(:merging_organisations) + if merge_params[:merging_organisations].blank? + merge_params[:merging_organisations] = [params[:id]] + else + merge_params[:merging_organisations] = merge_params[:merging_organisations].split(" ") << params[:merge][:merging_organisation] + end + merge_params + end +end diff --git a/app/models/merge.rb b/app/models/merge.rb new file mode 100644 index 000000000..4daa16941 --- /dev/null +++ b/app/models/merge.rb @@ -0,0 +1,6 @@ +class Merge + include ActiveModel::Model + include ActiveModel::Attributes + + attribute :merging_organisations, array: true, default: [] +end diff --git a/app/views/merge/organisations.html.erb b/app/views/merge/organisations.html.erb new file mode 100644 index 000000000..d5b57c4fa --- /dev/null +++ b/app/views/merge/organisations.html.erb @@ -0,0 +1,41 @@ +<% title = "Tell us if your organisation is merging" %> +<% content_for :title, title %> + +<%= govuk_back_link href: organisation_path %> +

Tell us if your organisation is merging

+ +
+
+

Start typing to search for your managing agent

+ +<%= form_with model: @merge, url: merge_organisations_organisation_path, method: :patch do |f| %> + <%= f.govuk_error_summary %> + + <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { + field: :merging_organisation, + question: Form::Question.new("", { "answer_options" => @answer_options }, nil), + f:, + } %> + <%= f.hidden_field :merging_organisations %> + <%= f.govuk_submit "Add organisation", classes: "govuk-button--secondary" %> + <%= govuk_table do |table| %> + <% @merging_organisations_list.each do |merging_organisation| %> + <%= table.body do |body| %> + <%= body.row do |row| %> + <% row.cell(text: merging_organisation.name) %> + <% if current_user.data_coordinator? || current_user.support? %> + <% row.cell(html_attributes: { + scope: "row", + class: "govuk-!-text-align-right", + }) do %> + <%= govuk_link_to("Remove", merge_organisations_organisation_path) %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> + <% end %> +<% end %> + +<%= govuk_button_link_to "Continue", merge_organisations_organisation_path, button: true %> +