Skip to content

Commit

Permalink
reset_counter should work with non-traditional belongs_to and polymor…
Browse files Browse the repository at this point in the history
…phic belongs_to

[#4984 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Jul 8, 2010
1 parent 87f64ef commit 1e53404
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
12 changes: 9 additions & 3 deletions activerecord/lib/active_record/counter_cache.rb
Expand Up @@ -17,8 +17,14 @@ module CounterCache
def reset_counters(id, *counters)
object = find(id)
counters.each do |association|
child_class = reflect_on_association(association.to_sym).klass
belongs_name = self.name.demodulize.underscore.to_sym
has_many_association = reflect_on_association(association.to_sym)
polymorphic_class = has_many_association.options[:as]
child_class = has_many_association.klass
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
belongs_to_association = belongs_to.detect do |e|
polymorphic_class.nil? ? (e.class_name == self.name) : (e.class_name.to_s.downcase == polymorphic_class.to_s.downcase)
end
belongs_name = belongs_to_association.name
counter_name = child_class.reflect_on_association(belongs_name).counter_cache_column

self.unscoped.where(arel_table[self.primary_key].eq(object.id)).arel.update({
Expand Down Expand Up @@ -103,4 +109,4 @@ def decrement_counter(counter_name, id)
update_counters(id, counter_name => -1)
end
end
end
end
19 changes: 16 additions & 3 deletions activerecord/test/cases/counter_cache_test.rb
@@ -1,17 +1,20 @@
require 'cases/helper'
require 'models/topic'
require 'models/car'
require 'models/wheel'
require 'models/engine'
require 'models/reply'
require 'models/category'
require 'models/categorization'

class CounterCacheTest < ActiveRecord::TestCase
fixtures :topics, :categories, :categorizations
fixtures :topics, :categories, :categorizations, :cars

class SpecialTopic < ::Topic
class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
end

class SpecialReply < ::Reply
class ::SpecialReply < ::Reply
belongs_to :special_topic, :foreign_key => 'parent_id', :counter_cache => 'replies_count'
end

Expand Down Expand Up @@ -58,6 +61,16 @@ class SpecialReply < ::Reply
end
end

test "reset counter should with belongs_to which has class_name" do
car = cars(:honda)
assert_nothing_raised do
Car.reset_counters(car.id, :engines)
end
assert_nothing_raised do
Car.reset_counters(car.id, :wheels)
end
end

test "update counter with initial null value" do
category = categories(:general)
assert_equal 2, category.categorizations.count
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/fixtures/cars.yml
@@ -0,0 +1,4 @@
honda:
id: 1
name: honda
engines_count: 0
4 changes: 4 additions & 0 deletions activerecord/test/models/car.rb
@@ -0,0 +1,4 @@
class Car < ActiveRecord::Base
has_many :engines
has_many :wheels, :as => :wheelable
end
3 changes: 3 additions & 0 deletions activerecord/test/models/engine.rb
@@ -0,0 +1,3 @@
class Engine < ActiveRecord::Base
belongs_to :my_car, :class_name => 'Car', :foreign_key => 'car_id', :counter_cache => :engines_count
end
3 changes: 3 additions & 0 deletions activerecord/test/models/wheel.rb
@@ -0,0 +1,3 @@
class Wheel < ActiveRecord::Base
belongs_to :wheelable, :polymorphic => true, :counter_cache => true
end
13 changes: 13 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -82,6 +82,12 @@ def create_table(*args, &block)
t.string :name
end

create_table :cars, :force => true do |t|
t.string :name
t.integer :engines_count
t.integer :wheels_count
end

create_table :categories, :force => true do |t|
t.string :name, :null => false
t.string :type
Expand Down Expand Up @@ -179,6 +185,9 @@ def create_table(*args, &block)
end
add_index :edges, [:source_id, :sink_id], :unique => true, :name => 'unique_edge_index'

create_table :engines, :force => true do |t|
t.integer :car_id
end

create_table :entrants, :force => true do |t|
t.string :name, :null => false
Expand Down Expand Up @@ -566,6 +575,10 @@ def create_table(*args, &block)
t.integer :zine_id
end

create_table :wheels, :force => true do |t|
t.references :wheelable, :polymorphic => true
end

create_table :zines, :force => true do |t|
t.string :title
end
Expand Down

0 comments on commit 1e53404

Please sign in to comment.