Browse Source

disable checkboxes for rent periods if they are in use so that users are not able to make existing logs invalid

hint text added to the question to explain this
I have also added all rent periods to a hidden field to remove the need to fetch them again form the db in the update method
pull/2389/head
Arthur Campbell 2 years ago
parent
commit
65cf2337bc
  1. 13
      app/controllers/organisations_controller.rb
  2. 10
      app/views/organisations/edit.html.erb
  3. 1
      spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb
  4. 4
      spec/requests/organisations_controller_spec.rb

13
app/controllers/organisations_controller.rb

@ -79,7 +79,7 @@ class OrganisationsController < ApplicationController
end
def create
selected_rent_periods = rent_period_params
selected_rent_periods = rent_period_params[:rent_periods].compact_blank
@organisation = Organisation.new(org_params)
if @organisation.save
OrganisationRentPeriod.transaction do
@ -96,6 +96,7 @@ class OrganisationsController < ApplicationController
def edit
if current_user.data_coordinator? || current_user.support?
current_allowed_rent_periods = @organisation.organisation_rent_periods.pluck(:rent_period).map(&:to_s)
@used_rent_periods = @organisation.lettings_logs.pluck(:period).uniq.compact.map(&:to_s)
@rent_periods = helpers.rent_periods_with_checked_attr(checked_periods: current_allowed_rent_periods)
render "edit", layout: "application"
else
@ -116,7 +117,7 @@ class OrganisationsController < ApplicationController
end
def update
selected_rent_periods = rent_period_params
selected_rent_periods = rent_period_params[: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]
@ -135,11 +136,9 @@ class OrganisationsController < ApplicationController
else
flash[:notice] = I18n.t("organisation.updated")
end
existing_rent_periods = @organisation.organisation_rent_periods.pluck(: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)
rent_periods_to_delete = rent_period_params[:all_rent_periods] - selected_rent_periods
OrganisationRentPeriod.transaction do
rent_periods_to_create.each { |period| OrganisationRentPeriod.create!(organisation: @organisation, rent_period: period) }
selected_rent_periods.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)
@ -294,7 +293,7 @@ private
end
def rent_period_params
params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank
params.require(:organisation).permit(rent_periods: [], all_rent_periods: [])
end
def codes_only_export?

10
app/views/organisations/edit.html.erb

@ -34,15 +34,21 @@
width: 20 %>
<%= f.govuk_check_boxes_fieldset :rent_periods,
legend: { text: "What are the rent periods for the organisation?" } do %>
legend: { text: "What are the rent periods for the organisation?" },
hint: { text: "It is not possible to deselect rent periods that are used in logs" } do %>
<% @rent_periods.map do |key, period| %>
<%= f.govuk_check_box :rent_periods,
key,
label: { text: period["value"] },
checked: period[:checked] %>
checked: period[:checked],
disabled: @used_rent_periods.include?(key) %>
<% end %>
<% end %>
<% @rent_periods.keys.each do |period_key| %>
<%= f.hidden_field :all_rent_periods, value: period_key, multiple: true %>
<% end %>
<%= f.govuk_submit "Save changes" %>
</div>
</div>

1
spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb

@ -91,6 +91,7 @@ RSpec.describe OrganisationsController, type: :request do
"organisation": {
name: organisation.name,
rent_periods: [initially_unchecked_rent_period_id],
all_rent_periods: [initially_unchecked_rent_period_id, initially_checked_rent_period_id],
},
}
end

4
spec/requests/organisations_controller_spec.rb

@ -8,7 +8,7 @@ RSpec.describe OrganisationsController, type: :request do
let(:user) { create(:user, :data_coordinator) }
let(:new_value) { "Test Name 35" }
let(:active) { nil }
let(:params) { { id: organisation.id, organisation: { name: new_value, active:, rent_periods: [] } } }
let(:params) { { id: organisation.id, organisation: { name: new_value, active:, rent_periods: [], all_rent_periods: [] } } }
before do
Timecop.freeze(Time.zone.local(2024, 3, 1))
@ -1426,7 +1426,7 @@ RSpec.describe OrganisationsController, type: :request do
end
describe "#update" do
let(:params) { { id: organisation.id, organisation: { active:, rent_periods: [] } } }
let(:params) { { id: organisation.id, organisation: { active:, rent_periods: [], all_rent_periods: [] } } }
context "with active parameter false" do
let(:active) { false }

Loading…
Cancel
Save