Sha256: 388f06bab1722d7b54005e807a2ac90a37f6884fe6d3b847e0396d4fb5848feb

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true
require 'dry/monads'

module Hyrax
  module Transactions
    module Steps
      ##
      # Adds and removes work members
      class UpdateWorkMembers
        include Dry::Monads[:result]

        ##
        # @param [Hyrax::Work] obj
        # @param [Hash] work_members_attributes
        #
        # @return [Dry::Monads::Result]
        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)
          Success(obj)
        end

        private

        def extract_attributes(attribute_hash)
          attribute_hash
            .sort_by { |i, _| i.to_i }
            .map { |_, attributes| attributes }
        end

        def save_resource(obj, user)
          saved = Hyrax.persister.save(resource: obj)
          user ||= ::User.find_by_user_key(obj.depositor)
          Hyrax.publisher.publish('object.metadata.updated', object: saved, user: user)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hyrax-5.0.0.rc2 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-5.0.0.rc1 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-3.6.0 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0.rc3 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0.rc2 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0.rc1 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-3.5.0 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0.beta2 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-3.4.2 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-4.0.0.beta1 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-3.4.1 lib/hyrax/transactions/steps/update_work_members.rb
hyrax-3.4.0 lib/hyrax/transactions/steps/update_work_members.rb