Sha256: 0e7dfdae012241853015c52aae31cf034631a449f1eac043b504935009466161

Contents?: true

Size: 1.68 KB

Versions: 53

Compression:

Stored size: 1.68 KB

Contents

module ActiveFedora
  module Associations
    class HasSubresourceAssociation < 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(id: target_uri, &block)
          else
            reflection.klass.find(target_uri)
          end
        rescue ActiveFedora::ObjectNotFoundError
          reflection.build_association(id: target_uri, &block)
        end

        def replace(record)
          if record
            raise_on_type_mismatch!(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

53 entries across 53 versions & 1 rubygems

Version Path
active-fedora-13.3.0 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-12.2.4 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.7 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.5 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.4 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-12.2.3 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.3 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.2 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.2.0 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.1.3 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-11.5.6 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-12.2.2 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-11.2.1 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-12.2.1 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-12.0.3 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-11.5.5 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.1.2 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.1.1 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.1.0 lib/active_fedora/associations/has_subresource_association.rb
active-fedora-13.0.0 lib/active_fedora/associations/has_subresource_association.rb