Sha256: f446e31c7a19f6427bee6fcb0905c659d27b23b679f522a82a6162453be59222

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

module NinjaModel
  module Associations
    class BelongsToAssociation < SingularAssociation

      def replace(record)
        raise_on_type_mismatch(record) if record

        replace_keys(record)

        @updated = true if record

        self.target = record
      end

      def updated?
        @updated
      end

      private

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

      def different_target?(record)
        record.nil? && owner[reflection.foreign_key] ||
          record && record.id != owner[reflection.foreign_key]
      end

      def replace_keys(record)
        if record
          owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)]
        else
          owner[reflection.foreign_key] = nil
        end
      end

      def foreign_key_present?
        owner[reflection.foreign_key]
      end

      def target_id
        if options[:primary_key]
          owner.send(reflection.name).try(:id)
        else
          owner[reflection.foreign_key]
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-1.0.4 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-1.0.3 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-1.0.2 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-1.0.1 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-1.0.0 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.6 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.5 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.4 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.3 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.2 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.1 lib/ninja_model/associations/belongs_to_association.rb
ninja-model-0.9.0 lib/ninja_model/associations/belongs_to_association.rb