Sha256: d991c08d7d39a29bfa286ff430f44de43d254f5c3658fe648681db08b3b4ea39

Contents?: true

Size: 1.49 KB

Versions: 18

Compression:

Stored size: 1.49 KB

Contents

module ActiveFedora
  module Associations
    class SingularAssociation < Association #:nodoc:
      # Implements the reader method, e.g. foo.bar for Foo.has_one :bar
      def reader(force_reload = false)
        if force_reload
          raise NotImplementedError, "Need to define the uncached method" # TODO
          # klass.uncached { reload }
        elsif !loaded? || stale_target?
          reload
        end
        target
      end

      # Implements the writer method, e.g. foo.items= for Foo.has_many :items
      def writer(record)
        replace(record)
      end

      def create(attributes = {})
        new_record(:create, attributes)
      end

      def create!(attributes = {})
        build(attributes).tap(&:save!)
      end

      def build(attributes = {})
        new_record(:build, attributes)
      end

      private

        def find_target
          # TODO: this forces a solr query, but I think it's likely we can just lookup from Fedora.
          # See https://github.com/samvera/active_fedora/issues/1330
          rec = scope.take
          rec.tap { |record| set_inverse_instance(record) }
        end

        # Implemented by subclasses
        def replace(_record)
          raise NotImplementedError
        end

        def new_record(method, attributes)
          attributes = {} # scoped.scope_for_create.merge(attributes || {})
          record = @reflection.send("#{method}_association", attributes)
          replace(record)
          record
        end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
active-fedora-13.3.0 lib/active_fedora/associations/singular_association.rb
active-fedora-12.2.4 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.7 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.5 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.4 lib/active_fedora/associations/singular_association.rb
active-fedora-12.2.3 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.3 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.2 lib/active_fedora/associations/singular_association.rb
active-fedora-13.2.0 lib/active_fedora/associations/singular_association.rb
active-fedora-13.1.3 lib/active_fedora/associations/singular_association.rb
active-fedora-12.2.2 lib/active_fedora/associations/singular_association.rb
active-fedora-12.2.1 lib/active_fedora/associations/singular_association.rb
active-fedora-13.1.2 lib/active_fedora/associations/singular_association.rb
active-fedora-13.1.1 lib/active_fedora/associations/singular_association.rb
active-fedora-13.1.0 lib/active_fedora/associations/singular_association.rb
active-fedora-13.0.0 lib/active_fedora/associations/singular_association.rb
active-fedora-12.1.1 lib/active_fedora/associations/singular_association.rb
active-fedora-12.1.0 lib/active_fedora/associations/singular_association.rb