Browse Source

CLDC-2740: Sort rent periods in organisation details page (#2680)

* Sort rent periods in organisation details page

* Make rent periods uniq

* Update tests to check rent periods are in order
pull/2697/head
Manny Dinssa 2 months ago committed by GitHub
parent
commit
ed58d8e9c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      app/models/organisation.rb
  2. 27
      spec/models/organisation_spec.rb

4
app/models/organisation.rb

@ -115,7 +115,9 @@ class Organisation < ApplicationRecord
mappings = RentPeriod.rent_period_mappings
return %w[All] if (mappings.keys.map(&:to_i) - rent_period_ids).empty?
rent_period_ids.map { |id| mappings.dig(id.to_s, "value") }.compact
rent_period_ids.map { |id| mappings.dig(id.to_s, "value") }.compact.uniq.sort_by do |label|
mappings.keys.index { |key| mappings[key]["value"] == label }
end
end
def data_protection_confirmed?

27
spec/models/organisation_spec.rb

@ -111,45 +111,52 @@ RSpec.describe Organisation, type: :model do
context "with associated rent periods" do
let(:organisation) { create(:organisation) }
let(:period_1_label) { "Every minute" }
let(:period_2_label) { "Every decade" }
let(:period_3_label) { "Every century" }
let(:period_4_label) { "Every millennium" }
let(:fake_rent_periods) do
{
"1" => { "value" => period_1_label },
"2" => { "value" => "Every decade" },
"2" => { "value" => period_2_label },
"3" => { "value" => period_3_label },
"4" => { "value" => period_4_label },
}
end
before do
create(:organisation_rent_period, organisation:, rent_period: 1)
[4, 2, 1].each do |rent_period|
create(:organisation_rent_period, organisation:, rent_period:)
end
allow(RentPeriod).to receive(:rent_period_mappings).and_return(fake_rent_periods)
end
context "when the org does not use all rent periods" do
it "#rent_periods returns the correct ids" do
expect(organisation.rent_periods).to eq [1]
expect(organisation.rent_periods).to match_array([4, 2, 1])
end
it "#rent_period_labels returns the correct labels" do
expect(organisation.rent_period_labels).to eq [period_1_label]
it "#rent_period_labels returns the correct labels in order" do
expect(organisation.rent_period_labels).to eq [period_1_label, period_2_label, period_4_label]
end
context "and has organisation rent periods associated for rent periods that no longer appear in the form" do
before do
create(:organisation_rent_period, organisation:, rent_period: 3)
create(:organisation_rent_period, organisation:, rent_period: 6)
end
it "#rent_period_labels returns the correct labels" do
expect(organisation.rent_period_labels).to eq [period_1_label]
expect(organisation.rent_period_labels).to eq [period_1_label, period_2_label, period_4_label]
end
end
end
context "when the org uses all rent periods" do
before do
create(:organisation_rent_period, organisation:, rent_period: 2)
create(:organisation_rent_period, organisation:, rent_period: 3)
end
it "#rent_periods returns the correct ids" do
expect(organisation.rent_periods).to eq [1, 2]
expect(organisation.rent_periods).to match_array([4, 2, 1, 3])
end
it "#rent_period_labels returns All" do
@ -158,7 +165,7 @@ RSpec.describe Organisation, type: :model do
context "and has organisation rent periods associated for rent periods that no longer appear in the form" do
before do
create(:organisation_rent_period, organisation:, rent_period: 3)
create(:organisation_rent_period, organisation:, rent_period: 6)
end
it "#rent_period_labels returns All" do

Loading…
Cancel
Save