X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fassociations%2Fbelongs_to_association.rb;fp=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fassociations%2Fbelongs_to_association.rb;h=f05c6be07587aac46cb1cb305370ae1f8bf9efb1;hb=d115f2e23823271635bad69229a42cd8ac68debe;hp=0000000000000000000000000000000000000000;hpb=37cb670bf3ddde90b214e591f100ed4446469484;p=depot.git

diff --git a/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb b/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
new file mode 100644
index 0000000..f05c6be
--- /dev/null
+++ b/vendor/rails/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -0,0 +1,58 @@
+module ActiveRecord
+  module Associations
+    class BelongsToAssociation < AssociationProxy #:nodoc:
+      def create(attributes = {})
+        replace(@reflection.create_association(attributes))
+      end
+
+      def build(attributes = {})
+        replace(@reflection.build_association(attributes))
+      end
+
+      def replace(record)
+        counter_cache_name = @reflection.counter_cache_column
+
+        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],
+            :select     => @reflection.options[:select],
+            :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