Sha256: 81f6d62ab5fbd038890e7d22339f7ed2464791bbe5577990217264accc3c61f2

Contents?: true

Size: 1.45 KB

Versions: 70

Compression:

Stored size: 1.45 KB

Contents

module ActiveFedora
  module Associations
    class BelongsToAssociation < SingularAssociation #:nodoc:
      def handle_dependency
        target.send(options[:dependent]) if load_target
      end

      def replace(record)
        if record
          raise_on_type_mismatch!(record)
          update_counters_on_replace(record)
          replace_keys(record)
          set_inverse_instance(record)
          @updated = true
        else
          decrement_counters
          remove_keys
        end

        self.target = record
      end

      def reset
        super
        @updated = false
      end

      def updated?
        @updated
      end

      def decrement_counters # :nodoc:
        # noop
      end

      def increment_counters # :nodoc:
        # noop
      end

      private

        def find_target?
          !loaded? && foreign_key_present? && klass
        end

        def update_counters_on_replace(_record)
          # noop
        end

        def replace_keys(record)
          owner[reflection.foreign_key] = record.id
        end

        def remove_keys
          owner[reflection.foreign_key] = nil
        end

        def foreign_key_present?
          owner[reflection.foreign_key]
        end

        # belongs_to is not invertible (unless we implement has_one, then make an exception here)
        def invertible_for?(_)
          false
        end

        def stale_state
          owner[reflection.foreign_key]
        end
    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
active-fedora-10.0.0.beta2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-10.0.0.beta1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.13.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.12.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.11.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.10.4 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.10.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.10.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.10.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.10.0 lib/active_fedora/associations/belongs_to_association.rb