Browse Source

enable updating org rent periods

update logic in #update
related tests
pull/2389/head
Arthur Campbell 2 years ago
parent
commit
2616eb8ece
  1. 11
      app/controllers/organisations_controller.rb
  2. 36
      spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb

11
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

36
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

Loading…
Cancel
Save