Sha256: 6feaf16a7709e9d8a8280ca33eefa3502b3e29acf6804e31264687580db09b87

Contents?: true

Size: 1.45 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
active-fedora-15.0.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-15.0.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-14.0.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-14.0.0 lib/active_fedora/associations/belongs_to_association.rb