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-13.3.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-12.2.4 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.7 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.5 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.4 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-12.2.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.2.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.1.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-11.5.6 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-12.2.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-11.2.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-12.2.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-12.0.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-11.5.5 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.1.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.1.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.1.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-13.0.0 lib/active_fedora/associations/belongs_to_association.rb