Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created September 3, 2015 15:44
Show Gist options
  • Save elrayle/d9519b1e1010acdb6a7f to your computer and use it in GitHub Desktop.
Save elrayle/d9519b1e1010acdb6a7f to your computer and use it in GitHub Desktop.
AddMembersToCollection service
module Hydra
module Collections
class AddMembersToCollection
##
# Add members to a collection.
#
# @param [Collection] :collection to which to add members
# @param [Object] :new_member_ids being added
#
# @return [Collection] the updated collection
def self.call(collection, new_member_ids, persist= false)
collection = Collection.find(collection) if collection.is_a? String
raise ArgumentError, "collection (#{collection}) not found" if collection.nil?
raise ArgumentError, 'collection must be a Collection' unless collection.is_a? Collection
collection.member_ids = new_member_ids.concat(collection.member_ids)
collection.save if persist
end
end
end
end
@elrayle
Copy link
Author

elrayle commented Sep 3, 2015

6.3 code in collections_controller_behavior.rb

    def add_members_to_collection collection = nil
      collection ||= @collection
      collection.member_ids = batch.concat(collection.member_ids)
    end

pcdm code in collections_controller_behavior.rb

    def add_members_to_collection collection = nil
      collection ||= @collection
      batch.each do |id|
        collection.child_generic_works << ActiveFedora::Base.find(id)
      end
      #TODO this old way was more efficient #collection.member_ids = batch.concat(collection.member_ids)
    end

For both becomes the following...

    def add_members_to_collection collection = nil
      collection ||= @collection
      Hydra::Collections::AddMembersToCollection.call(collection, batch)
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment