Sha256: aac249b62d69dde1ac0305cc78ba1b535720b79d487a72141cc6bbf47064c30c

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 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

        after_create :assign_struct_metadata!
      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

12 entries across 12 versions & 1 rubygems

Version Path
ddr-models-1.16.0 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.15.0 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.14.2 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.14.1 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.14.0 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.13.2 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.13.1 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.13.0 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.12.3 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.12.2 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.12.1 lib/ddr/models/has_struct_metadata.rb
ddr-models-1.12.0 lib/ddr/models/has_struct_metadata.rb