Skip to content

Commit

Permalink
fix: move annotation and listing changes to CitesCop and EuRegulation…
Browse files Browse the repository at this point in the history
… models, as they have different potential behaviours
  • Loading branch information
pdl committed Sep 10, 2024
1 parent 707758a commit 203fe9e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 0 additions & 2 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions app/models/events/cites_cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions app/models/events/eu_regulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 31 additions & 1 deletion spec/models/events/cites_cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/models/events/eu_regulation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 203fe9e

Please sign in to comment.