Sha256: 102fe67931a681a1d331275b94d306bd18ab43acec8ed089d126634cad9ab377

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module Ddr
  module Models
    module HasStructMetadata
      extend ActiveSupport::Concern

      FILE_USE_MASTER = 'master'
      FILE_USE_REFERENCE = 'reference'

      included do
        has_metadata "structMetadata",
                     type: Ddr::Datastreams::StructuralMetadataDatastream,
                     versionable: true,
                     control_group: "M"

        has_attributes :file_group, :file_use, :order,
                       datastream: "structMetadata", multiple: false
      end

      def assign_struct_metadata!
        self.file_use = default_file_use if file_use.blank?
        self.order = default_order if order.nil?
        self.file_group = default_file_group if file_group.blank?
        save! if changed?
      end

      private

      def default_file_use
        if has_content?
          master_file? ? FILE_USE_MASTER : FILE_USE_REFERENCE
        end
      end

      def default_order
        siblings.size + 1
      end

      def default_file_group
        identifier.first if has_content?
      end

      def siblings
        if respond_to?(:parent) && parent.present?
          if file_use && parent.respond_to?(:children_by_file_use)
            sibs = parent.children_by_file_use[file_use]
          else
            sibs = parent.children
          end
        end
        sibs || []
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddr-models-1.17.1 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.17.0 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.16.2 lib/ddr/models/has_struct_metadata.rb
ddr-models-2.0.0.pre.1 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.16.1 lib/ddr/models/has_struct_metadata.rb