Sha256: c94d4f5c3ab2851f8f3d5eb2f97d452cc04abb0ae82dd2d5f69be980eae3e198

Contents?: true

Size: 1.88 KB

Versions: 37

Compression:

Stored size: 1.88 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 : '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|
        query = ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::FileSet.to_class_uri)
        argz = { fl: "id, #{file_size_field}",
                 fq: "{!join from=#{member_ids_field} to=id}id:#{work_id}"
        }
        files = ActiveFedora::SolrService.query(query, 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_INTEGER)
      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

37 entries across 37 versions & 1 rubygems

Version Path
curation_concerns-1.5.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.4.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.3.3 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.3.2 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.3.1 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.3.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.2.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.1.2 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.1.1 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.1.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta10 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta9 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta8 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta7 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta6 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta5 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta4 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta3 app/models/concerns/curation_concerns/collection_behavior.rb
curation_concerns-1.0.0.beta2 app/models/concerns/curation_concerns/collection_behavior.rb