Skip to content

Commit

Permalink
Run callbacks for curation concern lifecycle events
Browse files Browse the repository at this point in the history
Split `:after_create_content` callback into `:after_create_concern` and `:after_create_fileset` since we're already special-casing FileSets; this allows us to differentiate between attaching FileSets and creating other concerns.

Fixes #752
  • Loading branch information
mjgiarlo committed Apr 28, 2016
1 parent afd0fd6 commit 8d07ab2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
9 changes: 7 additions & 2 deletions app/actors/curation_concerns/base_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ def create(attributes)
@cloud_resources = attributes.delete(:cloud_resources.to_s)
apply_creation_data_to_curation_concern
apply_save_data_to_curation_concern(attributes)
next_actor.create(attributes) && save
next_actor.create(attributes) && save && run_callbacks(:after_create_concern)
end

def update(attributes)
apply_update_data_to_curation_concern
apply_save_data_to_curation_concern(attributes)
next_actor.update(attributes) && save
next_actor.update(attributes) && save && run_callbacks(:after_update_metadata)
end

protected

def run_callbacks(hook)
CurationConcerns.config.callback.run(hook, curation_concern, user)
true
end

def apply_creation_data_to_curation_concern
apply_depositor_metadata
apply_deposit_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def update
def destroy
title = curation_concern.to_s
curation_concern.destroy
CurationConcerns.config.callback.run(:after_destroy, curation_concern.id, current_user)
after_destroy_response(title)
end

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/ingest_file_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def perform(file_set, filename, mime_type, user, relation = 'original_file')

# Do post file ingest actions
CurationConcerns::VersioningService.create(file_set.send(relation.to_sym), user)
CurationConcerns.config.callback.run(:after_create_content, file_set, user)
CurationConcerns.config.callback.run(:after_create_fileset, file_set, user)
end
end
9 changes: 5 additions & 4 deletions lib/curation_concerns/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ def lock_retry_delay
@lock_retry_delay ||= 200 # milliseconds
end

callback.enable :after_create_content, :after_update_content,
:after_revert_content, :after_update_metadata,
:after_import_local_file_success,
callback.enable :after_create_concern, :after_create_fileset,
:after_update_content, :after_revert_content,
:after_update_metadata, :after_import_local_file_success,
:after_import_local_file_failure, :after_audit_failure,
:after_destroy, :after_import_url_success, :after_import_url_failure
:after_destroy, :after_import_url_success,
:after_import_url_failure

# Registers the given curation concern model in the configuration
# @param [Array<Symbol>,Symbol] curation_concern_types
Expand Down
24 changes: 22 additions & 2 deletions spec/actors/curation_concerns/work_actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
let(:user) { create(:user) }
let(:file) { curation_concerns_fixture_file_upload('files/image.png', 'image/png') }

let(:redlock_client_stub) { # stub out redis connection
# stub out redis connection
let(:redlock_client_stub) do
client = double('redlock client')
allow(client).to receive(:lock).and_yield(true)
allow(Redlock::Client).to receive(:new).and_return(client)
client
}
end

subject do
CurationConcerns::CurationConcern.actor(curation_concern, user)
Expand All @@ -32,6 +33,17 @@
end
end

context 'success' do
before { redlock_client_stub }

it "invokes the after_create_concern callback" do
allow(CharacterizeJob).to receive(:perform_later).and_return(true)
expect(CurationConcerns.config.callback).to receive(:run)
.with(:after_create_concern, curation_concern, user)
subject.create(title: ['Foo Bar'])
end
end

context 'valid attributes' do
let(:visibility) { Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_AUTHENTICATED }

Expand Down Expand Up @@ -151,6 +163,14 @@
end
end

context 'success' do
it "invokes the after_update_metadata callback" do
expect(CurationConcerns.config.callback).to receive(:run)
.with(:after_update_metadata, curation_concern, user)
subject.update(title: ['Other Title'])
end
end

context 'adding to collections' do
let!(:collection1) { create(:collection, user: user) }
let!(:collection2) { create(:collection, user: user) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
expect(GenericWork).not_to exist(work_to_be_deleted.id)
end

it "invokes the after_destroy callback" do
expect(CurationConcerns.config.callback).to receive(:run)
.with(:after_destroy, work_to_be_deleted.id, user)
delete :destroy, id: work_to_be_deleted
end

context 'someone elses public work' do
let(:work_to_be_deleted) { create(:private_generic_work) }
it 'shows unauthorized message' do
Expand Down
4 changes: 2 additions & 2 deletions spec/jobs/ingest_file_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class FileSetWithExtras < FileSet
end
end

describe "the after_create_content callback" do
describe "the after_create_fileset callback" do
subject { CurationConcerns.config.callback }
it 'runs with file_set and user arguments' do
expect(subject).to receive(:run).with(:after_create_content, file_set, user)
expect(subject).to receive(:run).with(:after_create_fileset, file_set, user)
described_class.perform_now(file_set, filename, 'image/png', user)
end
end
Expand Down

0 comments on commit 8d07ab2

Please sign in to comment.