From 274b8fa94a86712eca7e11cff7f175832a2d5c2c Mon Sep 17 00:00:00 2001 From: Krzysztof Zylawy Date: Wed, 15 Apr 2009 15:50:34 +0100 Subject: [PATCH] added check for counter method when counter cache optinal is specified --- activerecord/lib/active_record/associations.rb | 10 +++++++--- .../associations/belongs_to_associations_test.rb | 17 +++++++++++++++++ activerecord/test/models/tagging.rb | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6d25b36..5ea0ead 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1008,14 +1008,18 @@ module ActiveRecord method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) - association.class.increment_counter(cache_column, send(reflection.primary_key_name)) unless association.nil? + if association.respond_to?(cache_column) || !options[:counter_cache_optional] + association.class.increment_counter(cache_column, send(reflection.primary_key_name)) unless association.nil? + end end after_create method_name method_name = "belongs_to_counter_cache_before_destroy_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) - association.class.decrement_counter(cache_column, send(reflection.primary_key_name)) unless association.nil? + if association.respond_to?(cache_column) || !options[:counter_cache_optional] + association.class.decrement_counter(cache_column, send(reflection.primary_key_name)) unless association.nil? + end end before_destroy method_name @@ -1498,7 +1502,7 @@ module ActiveRecord mattr_accessor :valid_keys_for_belongs_to_association @@valid_keys_for_belongs_to_association = [ :class_name, :foreign_key, :foreign_type, :remote, :select, :conditions, - :include, :dependent, :counter_cache, :extend, :polymorphic, :readonly, + :include, :dependent, :counter_cache, :counter_cache_optional, :extend, :polymorphic, :readonly, :validate ] diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 13a78a1..7eeff0f 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -14,6 +14,7 @@ require 'models/tagging' require 'models/comment' require 'models/sponsor' require 'models/member' +require 'models/item' class BelongsToAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :topics, @@ -188,6 +189,22 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase topic.update_attributes(:title => "37signals") assert_equal 1, Topic.find(topic.id)[:replies_count] end + + def test_belongs_to_counter_without_counter_cache_column_after_create + item = Item.create!(:name => "new item") + assert_nothing_raised { + tagging = Tagging.create!(:taggable => item) + } + end + + def test_belongs_to_counter_without_counter_cache_column_after_destroy + item = Item.create!(:name => "new item") + + assert_nothing_raised { + tagging = Tagging.create!(:taggable => item) + tagging.destroy + } + end def test_belongs_to_counter_after_save topic = Topic.create("title" => "monday night") diff --git a/activerecord/test/models/tagging.rb b/activerecord/test/models/tagging.rb index a1fa1a9..f0dba41 100644 --- a/activerecord/test/models/tagging.rb +++ b/activerecord/test/models/tagging.rb @@ -6,5 +6,5 @@ class Tagging < ActiveRecord::Base belongs_to :tag, :include => :tagging belongs_to :super_tag, :class_name => 'Tag', :foreign_key => 'super_tag_id' belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id' - belongs_to :taggable, :polymorphic => true, :counter_cache => true + belongs_to :taggable, :polymorphic => true, :counter_cache => true, :counter_cache_optional => true end \ No newline at end of file -- 1.6.0.2