Class: Hyrax::Collections::CollectionMemberService
- Inherits:
-
Object
- Object
- Hyrax::Collections::CollectionMemberService
- Defined in:
- app/services/hyrax/collections/collection_member_service.rb
Overview
Retrieves collection members
Class Method Summary collapse
-
.add_member(collection_id:, new_member:, user:) ⇒ Hyrax::Resource
Add a work or collection as a member of a collection.
-
.add_member_by_id(collection_id:, new_member_id:, user:) ⇒ Hyrax::Resource
Add a work or collection as a member of a collection.
-
.add_members(collection_id:, new_members:, user:) ⇒ Enumerable<Hyrax::Resource>
Add works and/or collections as members of a collection.
-
.add_members_by_ids(collection_id:, new_member_ids:, user:) ⇒ Enumerable<Hyrax::Resource>
Add works and/or collections as members of a collection.
-
.member?(collection_id:, member:) ⇒ Boolean
Check if a work or collection is already a member of a collection.
-
.remove_member(collection_id:, member:, user:) ⇒ Hyrax::Resource
Remove a collection or work from the members set of a collection, also removing the inverse relationship.
-
.remove_member_by_id(collection_id:, member_id:, user:) ⇒ Hyrax::Resource
Remove collections and/or works from the members set of a collection.
-
.remove_members(collection_id:, members:, user:) ⇒ Enumerable<Hyrax::Resource>
Remove collections and/or works from the members set of a collection.
-
.remove_members_by_ids(collection_id:, member_ids:, user:) ⇒ Enumerable<Hyrax::Resource>
Remove collections and/or works from the members set of a collection.
Instance Method Summary collapse
-
#available_member_subcollections ⇒ Blacklight::Solr::Response
Collections which are members of the given collection.
-
#available_member_work_ids ⇒ Blacklight::Solr::Response
Work ids of the works which are members of the given collection.
-
#available_member_works ⇒ Blacklight::Solr::Response
Works which are members of the given collection.
-
#initialize(scope:, collection:, params:, user_params: nil, current_ability: nil, search_builder_class: Hyrax::CollectionMemberSearchBuilder) ⇒ CollectionMemberService
constructor
A new instance of CollectionMemberService.
Constructor Details
#initialize(scope:, collection:, params:, user_params: nil, current_ability: nil, search_builder_class: Hyrax::CollectionMemberSearchBuilder) ⇒ CollectionMemberService
Returns a new instance of CollectionMemberService.
14 15 16 17 18 19 20 21 22 23 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 14 def initialize(scope:, collection:, params:, user_params: nil, current_ability: nil, search_builder_class: Hyrax::CollectionMemberSearchBuilder) # rubocop:disable Metrics/ParameterLists Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \ "Instead, use the same method in 'Hyrax::Collections::CollectionMemberSearchService'.") @member_search_service = Hyrax::Collections::CollectionMemberSearchService(scope: scope, collection: collection, params: params, user_params: user_params, current_ability: current_ability, search_builder_class: search_builder_class) end |
Class Method Details
.add_member(collection_id:, new_member:, user:) ⇒ Hyrax::Resource
Add a work or collection as a member of a collection
106 107 108 109 110 111 112 113 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 106 def add_member(collection_id:, new_member:, user:) = Hyrax::MultipleMembershipChecker.new(item: new_member).check(collection_ids: [collection_id], include_current_members: true) raise Hyrax::SingleMembershipError, if .present? new_member.member_of_collection_ids += [collection_id] # only populate this direction new_member = Hyrax.persister.save(resource: new_member) (new_member, user) new_member end |
.add_member_by_id(collection_id:, new_member_id:, user:) ⇒ Hyrax::Resource
Add a work or collection as a member of a collection
97 98 99 100 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 97 def add_member_by_id(collection_id:, new_member_id:, user:) new_member = Hyrax.query_service.find_by(id: new_member_id) add_member(collection_id: collection_id, new_member: new_member, user: user) end |
.add_members(collection_id:, new_members:, user:) ⇒ Enumerable<Hyrax::Resource>
Add works and/or collections as members of a collection
83 84 85 86 87 88 89 90 91 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 83 def add_members(collection_id:, new_members:, user:) = [] new_members.map do |new_member| add_member(collection_id: collection_id, new_member: new_member, user: user) rescue Hyrax::SingleMembershipError => err += [err.] end raise Hyrax::SingleMembershipError, if .present? end |
.add_members_by_ids(collection_id:, new_member_ids:, user:) ⇒ Enumerable<Hyrax::Resource>
Add works and/or collections as members of a collection
74 75 76 77 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 74 def add_members_by_ids(collection_id:, new_member_ids:, user:) new_members = Hyrax.query_service.find_many_by_ids(ids: new_member_ids) add_members(collection_id: collection_id, new_members: new_members, user: user) end |
.member?(collection_id:, member:) ⇒ Boolean
Check if a work or collection is already a member of a collection
66 67 68 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 66 def member?(collection_id:, member:) member.member_of_collection_ids.include? collection_id end |
.remove_member(collection_id:, member:, user:) ⇒ Hyrax::Resource
Remove a collection or work from the members set of a collection, also removing the inverse relationship
145 146 147 148 149 150 151 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 145 def remove_member(collection_id:, member:, user:) return member unless member?(collection_id: collection_id, member: member) member.member_of_collection_ids.delete(collection_id) member = Hyrax.persister.save(resource: member) (member, user) member end |
.remove_member_by_id(collection_id:, member_id:, user:) ⇒ Hyrax::Resource
Remove collections and/or works from the members set of a collection
136 137 138 139 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 136 def remove_member_by_id(collection_id:, member_id:, user:) member = Hyrax.query_service.find_by(id: member_id) remove_member(collection_id: collection_id, member: member, user: user) end |
.remove_members(collection_id:, members:, user:) ⇒ Enumerable<Hyrax::Resource>
Remove collections and/or works from the members set of a collection
128 129 130 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 128 def remove_members(collection_id:, members:, user:) members.map { |member| remove_member(collection_id: collection_id, member: member, user: user) } end |
.remove_members_by_ids(collection_id:, member_ids:, user:) ⇒ Enumerable<Hyrax::Resource>
Remove collections and/or works from the members set of a collection
119 120 121 122 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 119 def remove_members_by_ids(collection_id:, member_ids:, user:) members = Hyrax.query_service.find_many_by_ids(ids: member_ids) remove_members(collection_id: collection_id, members: members, user: user) end |
Instance Method Details
#available_member_subcollections ⇒ Blacklight::Solr::Response
Collections which are members of the given collection
31 32 33 34 35 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 31 def available_member_subcollections Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \ "Instead, use the same method in 'Hyrax::Collections::CollectionMemberSearchService'.") @member_search_service.available_member_subcollections end |
#available_member_work_ids ⇒ Blacklight::Solr::Response
Work ids of the works which are members of the given collection
55 56 57 58 59 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 55 def available_member_work_ids Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \ "Instead, use the same method in 'Hyrax::Collections::CollectionMemberSearchService'.") @member_search_service.available_member_work_ids end |
#available_member_works ⇒ Blacklight::Solr::Response
Works which are members of the given collection
43 44 45 46 47 |
# File 'app/services/hyrax/collections/collection_member_service.rb', line 43 def available_member_works Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \ "Instead, use the same method in 'Hyrax::Collections::CollectionMemberSearchService'.") @member_search_service.available_member_works end |