Sha256: a3bab432cb9e0d4db1c5724e86048b1b77606e7b068672a0fdd89ccf9bdeb3e4

Contents?: true

Size: 1.71 KB

Versions: 13

Compression:

Stored size: 1.71 KB

Contents

module ActiveFedora
  module Associations
    class BasicContainsAssociation < SingularAssociation #:nodoc:
      # Implements the reader method, e.g. foo.bar for Foo.has_one :bar
      def reader(force_reload = false)
        super || build
      end

      def find_target
        find_or_initialize_target do |record|
          configure_datastream(record) if reflection.options[:block]
        end
      end

      def target_uri
        "#{owner.id}/#{reflection.name}"
      end

      private

        def raise_on_type_mismatch(record)
          return if record.is_a? LoadableFromJson::SolrBackedMetadataFile
          super
        end

        def find_or_initialize_target(&block)
          if reflection.klass < ActiveFedora::File
            reflection.build_association(target_uri, &block)
          else
            reflection.klass.find(target_uri)
          end
        rescue ActiveFedora::ObjectNotFoundError
          reflection.build_association(target_uri, &block)
        end

        def replace(record)
          if record
            raise_on_type_mismatch(record)
            run_type_validator(record)
            @updated = true
          end

          self.target = record
        end

        def new_record(method, attributes)
          record = super
          configure_datastream(record)
          record
        end

        def configure_datastream(record)
          # If you called has_metadata with a block, pass the block into the File class
          if reflection.options[:block].class == Proc
            reflection.options[:block].call(record)
          end
          return unless record.new_record? && reflection.options[:autocreate]
          record.datastream_will_change!
        end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
active-fedora-9.7.3 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.7.2 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.10.0.pre2 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.10.0.pre1 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.9.1 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.9.0 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.8.2 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.8.1 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.8.0 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.7.1 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.7.0 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.6.2 lib/active_fedora/associations/basic_contains_association.rb
active-fedora-9.6.1 lib/active_fedora/associations/basic_contains_association.rb