Module: Hyrax::CollectionBehavior

Extended by:
ActiveSupport::Concern
Includes:
Hydra::AccessControls::Permissions, Hydra::AccessControls::WithAccessRight, Hydra::WithDepositor, Hydra::Works::CollectionBehavior, CoreMetadata, HasRepresentative, HumanReadableType, Noid, Permissions
Included in:
Collection
Defined in:
app/models/concerns/hyrax/collection_behavior.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Permissions::Readable

#private?, #public?, #registered?

Methods included from Permissions::Writable

#paranoid_permissions

Methods included from HumanReadableType

#human_readable_type, #to_solr

Methods included from Noid

#assign_id

Instance Method Details

#add_member_objects(new_member_ids) ⇒ Object

Add member objects by adding this collection to the objects’ member_of_collection association.



26
27
28
29
30
31
32
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 26

def add_member_objects(new_member_ids)
  Array(new_member_ids).each do |member_id|
    member = ActiveFedora::Base.find(member_id)
    member.member_of_collections << self
    member.save!
  end
end

#add_members(new_member_ids) ⇒ Object

Add members using the members association.



20
21
22
23
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 20

def add_members(new_member_ids)
  return if new_member_ids.blank?
  members << ActiveFedora::Base.find(new_member_ids)
end

#bytesFixnum

Compute the sum of each file in the collection using Solr to avoid having to access Fedora

Returns:

  • (Fixnum)

    size of collection in bytes

Raises:

  • (RuntimeError)

    unsaved record does not exist in solr



58
59
60
61
62
63
64
65
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 58

def bytes
  return 0 if member_object_ids.empty?

  raise "Collection must be saved to query for bytes" if new_record?

  # One query per member_id because Solr is not a relational database
  member_object_ids.collect { |work_id| size_for_work(work_id) }.sum
end

#member_object_idsObject

Use this query to get the ids of the member objects (since the containment association has been flipped)



69
70
71
72
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 69

def member_object_ids
  return [] unless id
  ActiveFedora::Base.search_with_conditions("member_of_collection_ids_ssim:#{id}").map(&:id)
end

#member_objectsObject



34
35
36
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 34

def member_objects
  ActiveFedora::Base.where("member_of_collection_ids_ssim:#{id}")
end

#to_sObject



38
39
40
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 38

def to_s
  title.present? ? title.join(' | ') : 'No Title'
end