diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 87e496529..201f928a6 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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) diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 515357dd5..cf2860f47 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -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")