Class: Hyrax::Listeners::MemberCleanupListener

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/listeners/member_cleanup_listener.rb

Overview

Listens for resource deleted events and cleans up associated members

Instance Method Summary collapse

Instance Method Details

#on_collection_deleted(event) ⇒ void

This method returns an undefined value.

Called when 'collection.deleted' event is published

Parameters:

  • event (Dry::Events::Event)

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/hyrax/listeners/member_cleanup_listener.rb', line 31

def on_collection_deleted(event)
  return unless event.payload.key?(:collection) # legacy callback
  return if event[:collection].is_a?(ActiveFedora::Base) # handled by legacy code

  Hyrax.custom_queries.find_members_of(collection: event[:collection]).each do |resource|
    begin
      resource.member_of_collection_ids -= [event[:collection].id]
      Hyrax.persister.save(resource: resource)
      Hyrax.publisher
           .publish('collection.membership.updated', collection: event[:collection], user: event[:user])
    rescue StandardError
      Hyrax.logger.warn "Failed to remove collection reference from #{work.class}:#{work.id} " \
                        "during cleanup for collection: #{event[:collection]}. "
    end
  end
end

#on_object_deleted(event) ⇒ void

This method returns an undefined value.

Called when 'object.deleted' event is published

Parameters:

  • event (Dry::Events::Event)

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/hyrax/listeners/member_cleanup_listener.rb', line 11

def on_object_deleted(event)
  return unless event.payload.key?(:object) # legacy callback
  return if event[:object].is_a?(ActiveFedora::Base) # handled by legacy code

  Hyrax.custom_queries.find_child_file_sets(resource: event[:object]).each do |file_set|
    begin
      Hyrax.persister.delete(resource: file_set)
      Hyrax.publisher
           .publish('object.deleted', object: file_set, id: file_set.id, user: event[:user])
    rescue StandardError # we don't uncaught errors looping filesets
      Hyrax.logger.warn "Failed to delete #{file_set.class}:#{file_set.id} " \
                        "during cleanup for resource: #{event[:object]}. " \
                        'This member may now be orphaned.'
    end
  end
end