Class: Hyrax::MultipleMembershipChecker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(item:) ⇒ MultipleMembershipChecker

Returns a new instance of MultipleMembershipChecker.

Parameters:

  • item (#member_of_collection_ids)

    an object that belongs to collections


10
11
12
# File 'app/services/hyrax/multiple_membership_checker.rb', line 10

def initialize(item:)
  @item = item
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.


7
8
9
# File 'app/services/hyrax/multiple_membership_checker.rb', line 7

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.

Parameters:

  • collection_ids (Array<String>)

    a list of collection identifiers

  • include_current_members (Boolean) (defaults to: false)

    if true, include item's existing collections in check; else if false, check passed in collections only

    • use `false` when collection_ids includes proposed new collections and existing collections (@see Hyrax::Actors::CollectionsMembershipActor #valid_membership?)

    • use `true` when collection_ids includes proposed new collections only (@see Hyrax::Collections::CollectionMemberService #add_member)

Returns:

  • (nil, String)

    nil if no conflicts; an error message string if so


53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/hyrax/multiple_membership_checker.rb', line 53

def check(collection_ids:, include_current_members: false)
  return unless single_membership_collection_types_exist?

  proposed_single_membership_collections = filter_to_single_membership_collections(collection_ids)
  return if proposed_single_membership_collections.blank?

  collections_to_check = collections_to_check(proposed_single_membership_collections,
                                              include_current_members)
  problematic_collections = check_collections(collections_to_check)
  build_error_message(problematic_collections)
end

#validateTrue, String

Deprecated.

Use #check instead; for removal in 4.0.0

Validate member_of_collection_ids does not have any membership conflicts based on the collection types of the members.

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.

Returns:

  • (True, String)

    true if no conflicts; otherwise, an error message string


25
26
27
28
29
30
31
32
33
34
# File 'app/services/hyrax/multiple_membership_checker.rb', line 25

def validate
  Deprecation.warn "#{self.class}##{__method__} is deprecated; use #check instead."
  return true if item.member_of_collection_ids.empty? || item.member_of_collection_ids.count <= 1
  return true unless single_membership_collection_types_exist?

  collections_to_check = filter_to_single_membership_collections(item.member_of_collection_ids)
  problematic_collections = check_collections(collections_to_check)
  errs = build_error_message(problematic_collections)
  errs.presence || true
end