Sha256: 812279adf397f4d64447eea2c0f1e3189ca01b7a57932a301ec64300cf2af408

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module CurationConcerns
  module CollectionBehavior
    extend ActiveSupport::Concern

    include Hydra::AccessControls::WithAccessRight
    include CurationConcerns::Collection
    include CurationConcerns::Noid
    include CurationConcerns::HumanReadableType
    include CurationConcerns::HasRepresentative
    include CurationConcerns::Permissions

    included do
      validates_with HasOneTitleValidator
    end

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

    module ClassMethods
      def indexer
        CurationConcerns::CollectionIndexer
      end
    end

    # Compute the sum of each file in the collection using Solr to
    # avoid having to access Fedora
    #
    # @return [Fixnum] size of collection in bytes
    # @raise [RuntimeError] unsaved record does not exist in solr
    def bytes
      return 0 if member_ids.count == 0

      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

    private

      # Field name to look up when locating the size of each file in Solr.
      # Override for your own installation if using something different
      def file_size_field
        Solrizer.solr_name(:file_size, CurationConcerns::FileSetIndexer::STORED_LONG)
      end

      # Solr field name collections and works use to index member ids
      def member_ids_field
        Solrizer.solr_name('member_ids', :symbol)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
curation_concerns-2.0.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-2.0.0.rc2 app/models/concerns/curation_concerns/collection_behavior.rb