Skip to content

Commit

Permalink
Allow events to trigger on specific conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinisukale committed Dec 6, 2023
1 parent 5c802b8 commit f2eff19
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
17 changes: 6 additions & 11 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ module Indexable
end
end

if (
instance_of?(DataciteDoi) || instance_of?(OtherDoi) ||
instance_of?(Doi)
) &&
(
saved_change_to_attribute?("related_identifiers") ||
saved_change_to_attribute?("creators") ||
saved_change_to_attribute?("funding_references")
)
if aasm_state == "findable" && !Rails.env.test?
send_import_message(to_jsonapi)
if (instance_of?(DataciteDoi) || instance_of?(OtherDoi) ||instance_of?(Doi))
if aasm_state == "findable"
# If following fields are modified or the state has changed (any other state to findable), send import message
if saved_change_to_attribute?("related_identifiers") || saved_change_to_attribute?("creators") || saved_change_to_attribute?("funding_references") || aasm_state_changed?
send_import_message(to_jsonapi)
end
end
elsif instance_of?(Event)
OtherDoiJob.perform_later(dois_to_import)
Expand Down
48 changes: 48 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@
end
end

describe 'after_commit' do
let(:doi) { create(:doi, aasm_state: 'findable') }
context 'when aasm_state is findable' do
before do
allow(doi).to receive(:saved_change_to_attribute?).and_return(false)
allow(doi).to receive(:aasm_state_changed?).and_return(false)
allow(doi).to receive(:send_import_message)
end

it 'sends import message if related_identifiers is modified' do
doi.related_identifiers = ['new_identifier']
doi.save
expect(doi).to have_received(:send_import_message).with(doi.to_jsonapi)
end

it 'sends import message if creators is modified' do
doi.creators = ['new_creator']
doi.save
expect(doi).to have_received(:send_import_message).with(doi.to_jsonapi)
end

it 'sends import message if funding_references is modified' do
doi.funding_references = ['new_reference']
doi.save
expect(doi).to have_received(:send_import_message).with(doi.to_jsonapi)
end

it 'sends import message if aasm_state is changed' do
doi.aasm_state = 'another_state'
doi.save
expect(doi).to have_received(:send_import_message).with(doi.to_jsonapi)
end

it 'does not send import message if none of the conditions are met' do
doi.save
expect(doi).not_to have_received(:send_import_message)
end
end

context 'when aasm_state is not findable' do
it 'does not send import message' do
allow(doi).to receive(:aasm_state).and_return('not_findable')
doi.save
expect(doi).not_to have_received(:send_import_message)
end
end
end

describe "validate agency" do
it "DataCite" do
subject = build(:doi, agency: "DataCite")
Expand Down

0 comments on commit f2eff19

Please sign in to comment.