add zone label

This commit is contained in:
Christophe Robillard 2022-08-11 19:05:12 +02:00
parent dc15b39648
commit a504e82021
4 changed files with 60 additions and 1 deletions

View file

@ -10,5 +10,10 @@
#
class Zone < ApplicationRecord
validates :acronym, presence: true, uniqueness: true
has_many :labels, -> { order(designated_on: :desc) }, class_name: 'ZoneLabel', inverse_of: :zone
has_many :procedures, -> { order(published_at: :desc) }, inverse_of: :zone
def label
labels.first.name
end
end

14
app/models/zone_label.rb Normal file
View file

@ -0,0 +1,14 @@
# == Schema Information
#
# Table name: zone_labels
#
# id :bigint not null, primary key
# designated_on :date not null
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# zone_id :bigint not null
#
class ZoneLabel < ApplicationRecord
belongs_to :zone
end

View file

@ -1,6 +1,14 @@
FactoryBot.define do
factory :zone do
sequence(:acronym) { |n| "MA#{n}" }
sequence(:label) { |n| "Ministère de l'Education Populaire #{n}" }
transient do
labels { [{ designated_on: '1981-05-08', name: "Ministère de l'Education Populaire" }] }
end
after(:create) do |zone, evaluator|
evaluator.labels.each do |label|
zone.labels.create(designated_on: label[:designated_on], name: label[:name])
end
end
end
end

32
spec/models/zone_spec.rb Normal file
View file

@ -0,0 +1,32 @@
describe Zone do
let(:now) { Time.zone.parse("2022-08-11") }
before do
Timecop.freeze(now)
end
after do
Timecop.return
end
describe '#label' do
let(:start_previous_government) { Date.parse('2020-07-06') }
let(:start_last_government) { Date.parse('2022-05-20') }
let(:zone) do
create(:zone, labels: [
{
designated_on: start_previous_government,
name: "Ministère de l'Économie, des Finances et de la Relance"
},
{
designated_on: start_last_government,
name: "Ministère de l'Économie, des Finances et de la Souveraineté industrielle et numérique"
}
])
end
it 'returns label for the current millesime' do
expect(zone.label).to eq "Ministère de l'Économie, des Finances et de la Souveraineté industrielle et numérique"
end
end
end