diff --git a/app/models/zone.rb b/app/models/zone.rb index 73af1eb74..f8162b01b 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -27,4 +27,8 @@ class Zone < ApplicationRecord def available_at?(date) label_at(date) != 'NA' end + + def self.available_at(date) + Zone.all.filter { |zone| zone.available_at?(date) }.sort_by { |zone| zone.label_at(date) } + end end diff --git a/spec/models/zone_spec.rb b/spec/models/zone_spec.rb index 75dbb7e7e..3fd6889c1 100644 --- a/spec/models/zone_spec.rb +++ b/spec/models/zone_spec.rb @@ -65,5 +65,57 @@ describe Zone do expect(zone.available_at?(start_previous_government + 1.week)).to be_truthy end end -end + describe "#self.available_at?" do + let(:start_previous_government) { Date.parse('2020-07-06') } + let(:start_last_government) { Date.parse('2022-05-20') } + let(:start_futur_government) { Date.parse('2027-05-20') } + let(:om) do + create(:zone, labels: [ + { + designated_on: start_previous_government, + name: "Ministère des Outre-mer" + }, + { + designated_on: start_last_government, + name: "NA" + }, + { + designated_on: start_futur_government, + name: "Ministère des Territoires d'Outre-mer" + } + ]) + end + + let!(:culture) do + create(:zone, labels: [ + { + designated_on: start_previous_government, + name: "Ministère de la Culture" + } + ]) + end + + let!(:om) do + create(:zone, labels: [ + { + designated_on: start_previous_government, + name: "Ministère des Outre-mer" + }, + { + designated_on: start_last_government, + name: "NA" + }, + { + designated_on: start_futur_government, + name: "Ministère des Territoires d'Outre-mer" + } + ]) + end + + it 'returns only available zones at specific date' do + expect(Zone.available_at(start_last_government + 1.day)).to eq [culture] + expect(Zone.available_at(start_previous_government + 1.day)).to eq [culture, om] + end + end +end