From 203fe9e1d522cdbbd76525992a295080acd2dc2c Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Mon, 9 Sep 2024 10:25:48 +0100 Subject: [PATCH] fix: move annotation and listing changes to CitesCop and EuRegulation models, as they have different potential behaviours --- app/models/event.rb | 2 -- app/models/events/cites_cop.rb | 10 ++++++++ app/models/events/eu_regulation.rb | 14 +++++++++++ spec/models/events/cites_cop_spec.rb | 32 +++++++++++++++++++++++- spec/models/events/eu_regulation_spec.rb | 30 ++++++++++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 067c530c9..1152217d9 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -41,8 +41,6 @@ class Event < ApplicationRecord attr_reader :effective_at_formatted belongs_to :designation, optional: true - has_many :listing_changes, dependent: :destroy - has_many :annotations, dependent: :destroy has_many :documents has_many :cites_processes diff --git a/app/models/events/cites_cop.rb b/app/models/events/cites_cop.rb index 5be514561..106bed293 100644 --- a/app/models/events/cites_cop.rb +++ b/app/models/events/cites_cop.rb @@ -36,6 +36,16 @@ class CitesCop < Event # Migrated to controller (Strong Parameters) # attr_accessible :is_current + ## + # The only time we would delete a CoP/EU regulation is just after we've + # created it by mistake, but we don't want to be able to delete the CoP + # event once it's started to be populated, whereas an EU Regulation + # starts off being populated with lots of associated data so we can't + # restrict deletion. + has_many :listing_changes, + dependent: :restrict_with_error, + foreign_key: :event_id + has_many :hash_annotations, class_name: 'Annotation', dependent: :destroy, diff --git a/app/models/events/eu_regulation.rb b/app/models/events/eu_regulation.rb index 5629db137..e5633926c 100644 --- a/app/models/events/eu_regulation.rb +++ b/app/models/events/eu_regulation.rb @@ -37,6 +37,20 @@ class EuRegulation < EuEvent # attr_accessible :listing_changes_event_id, :end_date attr_accessor :listing_changes_event_id + ## + # The only time we would delete a CoP/EU regulation is just after we've + # created it by mistake, but we don't want to be able to delete the CoP + # event once it's started to be populated, whereas an EU Regulation + # starts off being populated with lots of associated data so we can't + # restrict deletion. + has_many :listing_changes, + dependent: :destroy, + foreign_key: :event_id + + has_many :annotations, + dependent: :destroy, + foreign_key: :event_id + validate :designation_is_eu validates :effective_at, presence: true diff --git a/spec/models/events/cites_cop_spec.rb b/spec/models/events/cites_cop_spec.rb index d1dda9d5d..6d98295e7 100644 --- a/spec/models/events/cites_cop_spec.rb +++ b/spec/models/events/cites_cop_spec.rb @@ -51,14 +51,44 @@ describe :destroy do let(:cites_cop) { create_cites_cop } + context 'when no dependent objects attached' do specify { expect(cites_cop.destroy).to be_truthy } end + context 'when dependent objects attached' do - context 'when listing changes' do + ## + # NB: deleting EU regulation events will cause all its listing changes to + # be deleted, whereas CITES CoPs will refuse to be deleted until its + # listing changes are deleted. + context 'when listing changes exist' do let!(:listing_change) { create_cites_I_addition(event: cites_cop) } specify { expect(cites_cop.destroy).to be_falsey } end + + context 'when listing change and annotation' do + let!(:annotation) { create(:annotation, event: cites_cop) } + let!(:listing_change) do + create_cites_I_addition( + event: cites_cop, + annotation: annotation + ) + end + + specify { expect(cites_cop.destroy).to be_falsey } + end + + context 'when listing change and hash annotation' do + let!(:hash_annotation) { create(:annotation, event: cites_cop) } + let!(:listing_change) do + create_cites_I_addition( + event: cites_cop, + hash_annotation: hash_annotation + ) + end + + specify { expect(cites_cop.destroy).to be_falsey } + end end end end diff --git a/spec/models/events/eu_regulation_spec.rb b/spec/models/events/eu_regulation_spec.rb index 87a7d288d..7899b2d43 100644 --- a/spec/models/events/eu_regulation_spec.rb +++ b/spec/models/events/eu_regulation_spec.rb @@ -78,14 +78,44 @@ describe :destroy do let(:eu_regulation) { create_eu_regulation } + context 'when no dependent objects attached' do specify { expect(eu_regulation.destroy).to be_truthy } end + context 'when dependent objects attached' do + ## + # NB: deleting EU regulation events will cause all its listing changes to + # be deleted, whereas CITES CoPs will refuse to be deleted until its + # listing changes are deleted. context 'when listing changes' do let!(:listing_change) { create_eu_A_addition(event: eu_regulation) } specify { expect(eu_regulation.destroy).to be_truthy } end + + context 'when listing change and annotation' do + let!(:annotation) { create(:annotation, event: eu_regulation) } + let!(:listing_change) do + create_eu_A_addition( + event: eu_regulation, + annotation: annotation + ) + end + + specify { expect(eu_regulation.destroy).to be_truthy } + end + + context 'when listing change and hash annotation' do + let!(:hash_annotation) { create(:annotation, event: eu_regulation) } + let!(:listing_change) do + create_eu_A_addition( + event: eu_regulation, + hash_annotation: hash_annotation + ) + end + + specify { expect(eu_regulation.destroy).to be_truthy } + end end end end