Browse Source

Add update organisations

pull/1535/head
Kat 3 years ago
parent
commit
5f9a1ff0d2
  1. 39
      app/controllers/merge_requests_controller.rb
  2. 2
      app/models/merge_request.rb
  3. 2
      app/views/merge_requests/organisations.html.erb
  4. 47
      spec/requests/merge_requests_controller_spec.rb
  5. 6
      spec/requests/organisations_controller_spec.rb

39
app/controllers/merge_requests_controller.rb

@ -1,14 +1,9 @@
class MergeRequestsController < ApplicationController
before_action :authenticate_user!
# before_action :authenticate_scope!
def create
@merge_request = MergeRequest.new
end
before_action :find_resource, only: %i[organisations update_organisations]
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

2
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)

2
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 %>
<h2 class="govuk-heading-l">Tell us if your organisation is merging</h2>
<h2 class="govuk-heading-l">Which organisations are merging?</h2>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">

47
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

6
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

Loading…
Cancel
Save