diff --git a/app/controllers/merge_requests_controller.rb b/app/controllers/merge_requests_controller.rb
index cb6a150fc..11241576f 100644
--- a/app/controllers/merge_requests_controller.rb
+++ b/app/controllers/merge_requests_controller.rb
@@ -1,14 +1,9 @@
class MergeRequestsController < ApplicationController
before_action :authenticate_user!
- # before_action :authenticate_scope!
+ before_action :find_resource, only: %i[organisations update_organisations]
- def create
- @merge_request = MergeRequest.new
- end
-
def create
@merge_request = MergeRequest.create!(merge_request_params)
-
redirect_to merge_request_organisations_path(@merge_request)
end
@@ -31,7 +26,28 @@ class MergeRequestsController < ApplicationController
render "organisations"
end
- private
+ def update_organisations
+ if @merge_request.merging_organisation_ids
+ @merge_request.merging_organisation_ids << params[:merge_request][:merging_organisation].to_i
+ @merge_request.save!
+ else
+ @merge_request.update!(merging_organisation_ids: [params[:merge_request][:merging_organisation]])
+ end
+ @answer_options = organisations_answer_options
+ @merging_organisations_list = [@merge_request.requesting_organisation] + @merge_request.merging_organisations
+ render "organisations"
+ end
+
+private
+
+ def organisations_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
def answer_options
answer_options = { "" => "Select an option" }
@@ -43,10 +59,13 @@ class MergeRequestsController < ApplicationController
end
def merge_request_params
- required_params = params.fetch(:merge_request, {}).permit(:requesting_organisation)
+ merge_params = params.fetch(:merge_request, {}).permit(:requesting_organisation)
- required_params[:requesting_organisation] = current_user.organisation
- required_params
+ merge_params[:requesting_organisation] = current_user.organisation
+ merge_params
end
+ def find_resource
+ @merge_request = MergeRequest.find(params[:merge_request_id])
+ end
end
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 009292c7f..1e0f888f6 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -1,5 +1,7 @@
class MergeRequest < ApplicationRecord
belongs_to :requesting_organisation, class_name: "Organisation"
+ # has_many :merging_organisations, class_name: "Organisation", primary_key: "merging_organisation_ids", foreign_key: "id"
+ # default_scope -> { select(column_names + ["merging_organisation_ids"]) }
def merging_organisations
Organisation.where(id: merging_organisation_ids)
diff --git a/app/views/merge_requests/organisations.html.erb b/app/views/merge_requests/organisations.html.erb
index a72b848c7..39144f7c1 100644
--- a/app/views/merge_requests/organisations.html.erb
+++ b/app/views/merge_requests/organisations.html.erb
@@ -1,7 +1,7 @@
<% title = "Tell us if your organisation is merging" %>
<% content_for :title, title %>
<%# <%= govuk_back_link href: merge_request_organisation_path %>
-
Tell us if your organisation is merging
+Which organisations are merging?
@@ -44,4 +44,4 @@
<% end %>
<% end %>
<%= govuk_button_link_to "Continue", "#", button: true %>
-
\ No newline at end of file
+
diff --git a/config/routes.rb b/config/routes.rb
index c35509437..a3229068a 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -123,7 +123,7 @@ Rails.application.routes.draw do
end
end
- resources :merge_requests, path: "/merge-request" do
+ resources :merge_requests, path: "/merge-request" do
post "merge-request", to: "merge_request#create_merge_request"
get "organisations", to: "merge_requests#organisations"
patch "organisations", to: "merge_requests#update_organisations"
diff --git a/spec/requests/merge_requests_controller_spec.rb b/spec/requests/merge_requests_controller_spec.rb
new file mode 100644
index 000000000..66a2cbb75
--- /dev/null
+++ b/spec/requests/merge_requests_controller_spec.rb
@@ -0,0 +1,47 @@
+require "rails_helper"
+
+RSpec.describe MergeRequestsController, type: :request do
+ let(:organisation) { user.organisation }
+ let!(:other_organisation) { FactoryBot.create(:organisation) }
+ let(:headers) { { "Accept" => "text/html" } }
+ let(:page) { Capybara::Node::Simple.new(response.body) }
+ let(:user) { FactoryBot.create(:user, :data_coordinator) }
+ let(:merge_request) { MergeRequest.create!(requesting_organisation: organisation) }
+
+ context "when user is signed in with a data coordinator user" do
+ before do
+ sign_in user
+ end
+
+ describe "#organisations" do
+ before do
+ organisation.update!(name: "Test Org")
+ post "/merge-request", headers:, params: {}
+ end
+
+ it "creates merge request with requesting organisation" do
+ follow_redirect!
+ expect(page).to have_content("Which organisations are merging?")
+ expect(page).to have_content("Test Org")
+ expect(page).not_to have_link("Remove")
+ end
+ end
+
+ describe "#update_organisations" do
+ let(:params) { { merge_request: { merging_organisation: other_organisation.id } } }
+
+ before do
+ other_organisation.update!(name: "Other Test Org")
+ patch "/merge-request/#{merge_request.id}/organisations", headers:, params:
+ end
+
+ it "updates the merge request" do
+ merge_request.reload
+ expect(merge_request.merging_organisations.count).to eq(1)
+ expect(page).to have_content("Test Org")
+ expect(page).to have_content("Other Test Org")
+ expect(page).to have_link("Remove")
+ end
+ end
+ end
+end
diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb
index b16ee4750..d526f218f 100644
--- a/spec/requests/organisations_controller_spec.rb
+++ b/spec/requests/organisations_controller_spec.rb
@@ -441,7 +441,7 @@ RSpec.describe OrganisationsController, type: :request do
end
end
- fdescribe "#merge" do
+ describe "#merge" do
context "with an organisation that the user belongs to" do
before do
get "/organisations/#{organisation.id}/merge-request", headers:, params: {}
@@ -455,8 +455,8 @@ RSpec.describe OrganisationsController, type: :request do
expect(page).to have_link("Back", href: "/organisations/#{organisation.id}")
end
- it "has a correct start no button" do
- expect(page).to have_link("Start now", href: "/merge-request/new")
+ it "has a correct start now button" do
+ expect(page).to have_button("Start now")
end
end