From 2616eb8ecee2f6d405223957c1d8798938b12147 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Tue, 23 Apr 2024 16:09:31 +0100 Subject: [PATCH] enable updating org rent periods update logic in #update related tests --- app/controllers/organisations_controller.rb | 11 ++++++ ...anisations_controller_rent_periods_spec.rb | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index d2ab06662..486489cbe 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -115,6 +115,7 @@ class OrganisationsController < ApplicationController end def update + selected_rent_periods = params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support? if @organisation.update(org_params) case org_params[:active] @@ -133,7 +134,17 @@ class OrganisationsController < ApplicationController else flash[:notice] = I18n.t("organisation.updated") end + existing_rent_periods = @organisation.organisation_rent_periods.map(&:rent_period) + rent_periods_to_create = selected_rent_periods.map(&:to_i) - existing_rent_periods + rent_periods_to_delete = existing_rent_periods - selected_rent_periods.map(&:to_i) + OrganisationRentPeriod.transaction do + rent_periods_to_create.each { |period| OrganisationRentPeriod.create!(organisation: @organisation, rent_period: period) } + OrganisationRentPeriod.where(organisation: @organisation, rent_period: rent_periods_to_delete).destroy_all + end redirect_to details_organisation_path(@organisation) + else + @rent_periods = helpers.rent_periods_with_checked_attr(checked_periods: selected_rent_periods) + render :edit, status: :unprocessable_entity end else head :unauthorized diff --git a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb index 6f317baa8..171eb57df 100644 --- a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb +++ b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb @@ -80,4 +80,40 @@ RSpec.describe OrganisationsController, type: :request do expect(expected_not_checked_checkbox[:checked]).to be false end end + + describe "#update" do + let(:organisation) { create(:organisation) } + let(:initially_checked_rent_period_id) { "1" } + let(:initially_unchecked_rent_period_id) { "2" } + let(:fake_rent_periods) do + { + initially_checked_rent_period_id => { "value" => "Every minute" }, + initially_unchecked_rent_period_id => { "value" => "Every decade" }, + } + end + let(:params) do + { + "organisation": { + name: organisation.name, + rent_periods: [initially_unchecked_rent_period_id], + }, + } + end + + before do + create(:organisation_rent_period, organisation:, rent_period: initially_checked_rent_period_id) + end + + it "creates and destroys organisation rent periods as appropriate" do + rent_periods = OrganisationRentPeriod.all + expect(rent_periods.count).to be 1 + expect(rent_periods.first.rent_period.to_s).to eq initially_checked_rent_period_id + + patch organisation_path(organisation, headers:, params:) + + rent_periods = OrganisationRentPeriod.all + expect(rent_periods.count).to be 1 + expect(rent_periods.first.rent_period.to_s).to eq initially_unchecked_rent_period_id + end + end end