Browse Source

changes following review

pull/2389/head
Arthur Campbell 2 years ago
parent
commit
d743faa79d
  1. 14
      app/controllers/organisations_controller.rb
  2. 8
      app/helpers/organisations_helper.rb

14
app/controllers/organisations_controller.rb

@ -79,7 +79,7 @@ class OrganisationsController < ApplicationController
end end
def create def create
selected_rent_periods = params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank selected_rent_periods = rent_period_params
@organisation = Organisation.new(org_params) @organisation = Organisation.new(org_params)
if @organisation.save if @organisation.save
OrganisationRentPeriod.transaction do OrganisationRentPeriod.transaction do
@ -95,8 +95,8 @@ class OrganisationsController < ApplicationController
def edit def edit
if current_user.data_coordinator? || current_user.support? if current_user.data_coordinator? || current_user.support?
checked_periods = @organisation.organisation_rent_periods.map(&:rent_period).map(&:to_s) current_allowed_rent_periods = @organisation.organisation_rent_periods.pluck(:rent_period).map(&:to_s)
@rent_periods = helpers.rent_periods_with_checked_attr(checked_periods:) @rent_periods = helpers.rent_periods_with_checked_attr(checked_periods: current_allowed_rent_periods)
render "edit", layout: "application" render "edit", layout: "application"
else else
head :unauthorized head :unauthorized
@ -116,7 +116,7 @@ class OrganisationsController < ApplicationController
end end
def update def update
selected_rent_periods = params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank selected_rent_periods = rent_period_params
if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support? if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support?
if @organisation.update(org_params) if @organisation.update(org_params)
case org_params[:active] case org_params[:active]
@ -135,7 +135,7 @@ class OrganisationsController < ApplicationController
else else
flash[:notice] = I18n.t("organisation.updated") flash[:notice] = I18n.t("organisation.updated")
end end
existing_rent_periods = @organisation.organisation_rent_periods.map(&:rent_period) 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_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 = existing_rent_periods - selected_rent_periods.map(&:to_i)
OrganisationRentPeriod.transaction do OrganisationRentPeriod.transaction do
@ -293,6 +293,10 @@ private
params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone, :holds_own_stock, :provider_type, :housing_registration_no, :active) params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone, :holds_own_stock, :provider_type, :housing_registration_no, :active)
end end
def rent_period_params
params.require(:organisation).permit(rent_periods: [])[:rent_periods].compact_blank
end
def codes_only_export? def codes_only_export?
params.require(:codes_only) == "true" params.require(:codes_only) == "true"
end end

8
app/helpers/organisations_helper.rb

@ -40,12 +40,8 @@ module OrganisationsHelper
end end
def rent_periods_with_checked_attr(checked_periods: nil) def rent_periods_with_checked_attr(checked_periods: nil)
all_rent_periods = RentPeriod.rent_period_mappings RentPeriod.rent_period_mappings.each_with_object({}) do |(period_code, period_value), result|
rent_periods = all_rent_periods.map do |code, period| result[period_code] = period_value.merge(checked: checked_periods.nil? || checked_periods.include?(period_code))
period_copy = period.clone
period_copy[:checked] = true if checked_periods.nil? || checked_periods.include?(code)
[code, period_copy]
end end
rent_periods.to_h
end end
end end

Loading…
Cancel
Save