Class: Hyrax::Collections::CollectionMemberService

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/collections/collection_member_service.rb

Overview

Retrieves collection members

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, collection:, params:, user_params: nil, current_ability: nil, search_builder_class: Hyrax::CollectionMemberSearchBuilder) ⇒ CollectionMemberService

Returns a new instance of CollectionMemberService.

Parameters:

  • scope (#repository)

    Typically a controller object which responds to :repository

  • collection (::Collection)
  • params (ActionController::Parameters)

    the query params

  • user_params (ActionController::Parameters) (defaults to: nil)
  • current_ability (::Ability) (defaults to: nil)
  • search_builder_class (Class) (defaults to: Hyrax::CollectionMemberSearchBuilder)

    a SearchBuilder



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • new_member (Hyrax::Resource)

    the new child collection or child work

Returns:

Raises:



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:)
  message = Hyrax::MultipleMembershipChecker.new(item: new_member).check(collection_ids: [collection_id], include_current_members: true)
  raise Hyrax::SingleMembershipError, message if message.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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • new_member_id (Valkyrie::ID)

    the id of the new child collection or child work

Returns:



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • new_members (Enumerable<Hyrax::Resource>)

    the new child collections and/or child works

Returns:

Raises:



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:)
  messages = []
  new_members.map do |new_member|
    add_member(collection_id: collection_id, new_member: new_member, user: user)
  rescue Hyrax::SingleMembershipError => err
    messages += [err.message]
  end
  raise Hyrax::SingleMembershipError, messages if messages.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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • new_member_ids (Enumerable<Valkyrie::ID>)

    the ids of the new child collections and/or child works

Returns:



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • member (Hyrax::Resource)

    the child collection and/or child work to check

Returns:

  • (Boolean)

    true if already in the member set; otherwise, false



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • member (Hyrax::Resource)

    the child collection or child work to be removed

Returns:



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • member_id (Valkyrie::ID)

    the id of the child collection or child work to be removed

Returns:



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • members (Enumerable<Valkyrie::Resource>)

    the child collections and/or child works to be removed

Returns:



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

Parameters:

  • collection_id (Valkyrie::ID)

    the id of the parent collection

  • member_ids (Enumerable<Valkyrie::ID>)

    the ids of the child collections and/or child works to be removed

Returns:



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_subcollectionsBlacklight::Solr::Response

Collections which are members of the given collection

Returns:

  • (Blacklight::Solr::Response)

    (up to 50 solr documents)



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_idsBlacklight::Solr::Response

Work ids of the works which are members of the given collection

Returns:

  • (Blacklight::Solr::Response)


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_worksBlacklight::Solr::Response

Works which are members of the given collection

Returns:

  • (Blacklight::Solr::Response)


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