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)


30
# File 'app/services/hyrax/listeners/member_cleanup_listener.rb', line 30

def on_collection_deleted(event); 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
# File 'app/services/hyrax/listeners/member_cleanup_listener.rb', line 11

def on_object_deleted(event)
  event = event.to_h
  return unless event[:object]

  object = event[:object]
  user = event[:user]
  return unless object.is_a?(Hyrax::Work)

  Hyrax.query_service.find_parents(resource: object).each do |parent|
    parent.member_ids -= [object.id]
    Hyrax.persister.save(resource: parent)
    Hyrax.index_adapter.save(resource: parent)
    Hyrax.publisher.publish('object.membership.updated', object: parent, user: user)
  end
end