Class: Hyrax::MultipleMembershipChecker
- Inherits:
-
Object
- Object
- Hyrax::MultipleMembershipChecker
- Defined in:
- app/services/hyrax/multiple_membership_checker.rb
Overview
Service class for checking an item’s collection memberships, to make sure that the item is not added to multiple single-membership collections
Instance Attribute Summary collapse
-
#item ⇒ Object
readonly
Returns the value of attribute item.
Instance Method Summary collapse
-
#check(collection_ids:, include_current_members: false) ⇒ nil, String
Scan a list of collection_ids for multiple single-membership collections.
-
#initialize(item:) ⇒ MultipleMembershipChecker
constructor
A new instance of MultipleMembershipChecker.
Constructor Details
#initialize(item:) ⇒ MultipleMembershipChecker
Returns a new instance of MultipleMembershipChecker.
10 11 12 |
# File 'app/services/hyrax/multiple_membership_checker.rb', line 10 def initialize(item:) @item = item end |
Instance Attribute Details
#item ⇒ Object (readonly)
Returns the value of attribute item.
6 7 8 |
# File 'app/services/hyrax/multiple_membership_checker.rb', line 6 def item @item end |
Instance Method Details
#check(collection_ids:, include_current_members: false) ⇒ nil, String
Scan a list of collection_ids for multiple single-membership collections.
Collections that have a collection type declaring ‘allow_multiple_membership` as `false` require that its members do not also belong to other collections of the same type.
There are two contexts in which memberships are checked: when doing a wholesale replacement and when making an incremental change, such as adding a single collection membership to an object. In the former case, ‘#check` only scans the passed-in collection identifiers. In the latter, `#check` must also scan the collections to which an object currently belongs for potential conflicts.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/hyrax/multiple_membership_checker.rb', line 34 def check(collection_ids:, include_current_members: false) # short-circuit if no single membership types have been created return if single_membership_types.blank? # short-circuit if no new single_membership_collections passed in new_single_membership_collections = single_membership_collections(collection_ids) return if new_single_membership_collections.blank? collections_to_check = new_single_membership_collections # No need to check current members when coming in from the ActorStack, which does a wholesale collection membership replacement collections_to_check |= single_membership_collections(item.member_of_collection_ids) if include_current_members problematic_collections = collections_to_check.uniq(&:id) .group_by(&:collection_type_gid) .select { |_gid, list| list.count > 1 } return if problematic_collections.blank? (problematic_collections) end |