Sha256: f496a1f5128cad7d79f41286d9da1577fe70e9ffacbc8b331ed69c5b8311c6e4

Contents?: true

Size: 1.71 KB

Versions: 17

Compression:

Stored size: 1.71 KB

Contents

module CouchFoo
  module Associations
    class BelongsToAssociation < AssociationProxy #:nodoc:
      def create(attributes = {})
        replace(@reflection.klass.create(attributes))
      end

      def build(attributes = {})
        replace(@reflection.klass.new(attributes))
      end

      def replace(record)
        counter_cache_name = @reflection.counter_cache_property

        if record.nil?
          if counter_cache_name && !@owner.new_record?
            @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
          end

          @target = @owner[@reflection.primary_key_name] = nil
        else
          raise_on_type_mismatch(record)

          if counter_cache_name && !@owner.new_record?
            @reflection.klass.increment_counter(counter_cache_name, record.id)
            @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
          end

          @target = (AssociationProxy === record ? record.target : record)
          @owner[@reflection.primary_key_name] = record.id unless record.new_record?
          @updated = true
        end

        loaded
        record
      end
      
      def updated?
        @updated
      end
      
      private
        def find_target
          @reflection.klass.find(
            @owner[@reflection.primary_key_name],
            :conditions => conditions,
            :include    => @reflection.options[:include],
            :readonly   => @reflection.options[:readonly]
          )
        end

        def foreign_key_present
          !@owner[@reflection.primary_key_name].nil?
        end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
georgepalmer-couch_foo-0.7.1 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.10 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.11 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.12 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.13 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.14 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.15 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.2 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.3 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.4 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.7 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.8 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.7.9 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.8.0 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.8.1 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.8.2 lib/couch_foo/associations/belongs_to_association.rb
georgepalmer-couch_foo-0.8.3 lib/couch_foo/associations/belongs_to_association.rb