Module: Hyrax::CollectionBehavior

Extended by:
ActiveSupport::Concern
Includes:
Hydra::AccessControls::Permissions, Hydra::AccessControls::WithAccessRight, Hydra::WithDepositor, Hydra::Works::CollectionBehavior, HasRepresentative, HumanReadableType, Noid, Permissions, RequiredMetadata
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, #to_param

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.nil? || new_member_ids.empty?
  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
66
67
68
69
70
71
72
# File 'app/models/concerns/hyrax/collection_behavior.rb', line 58

def bytes
  return 0 if member_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
  sizes = member_ids.collect do |work_id|
    argz = { fl: "id, #{file_size_field}",
             fq: "{!join from=#{member_ids_field} to=id}id:#{work_id}" }
    files = ::FileSet.search_with_conditions({}, argz)
    files.reduce(0) { |sum, f| sum + f[file_size_field].to_i }
  end

  sizes.reduce(0, :+)
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