Class: Hyrax::Transactions::Steps::UpdateWorkMembers

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/transactions/steps/update_work_members.rb

Overview

Adds and removes work members

Since:

  • 2.4.0

Instance Method Summary collapse

Instance Method Details

#call(obj, work_members_attributes: nil, user: nil) ⇒ Dry::Monads::Result

Parameters:

  • obj (Hyrax::Work)
  • work_members_attributes (Hash) (defaults to: nil)

Returns:

  • (Dry::Monads::Result)

Since:

  • 2.4.0



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hyrax/transactions/steps/update_work_members.rb', line 17

def call(obj, work_members_attributes: nil, user: nil)
  return Success(obj) if work_members_attributes.blank?

  attributes = extract_attributes(work_members_attributes)
  current_member_ids = obj.member_ids.map(&:id)
  destroys = attributes.select do |v|
    ActiveModel::Type::Boolean.new.cast(v['_destroy'])
  end

  inserts  = (attributes - destroys).map { |h| h['id'] }.compact - current_member_ids
  destroys = destroys.map { |h| h['id'] }.compact & current_member_ids
  obj.member_ids += inserts.map  { |id| Valkyrie::ID.new(id) }
  obj.member_ids -= destroys.map { |id| Valkyrie::ID.new(id) }

  save_resource(obj, user)
  Hyrax.publisher.publish('object.membership.updated', object: obj, user: user)

  Success(obj)
end