Skip to content

Commit

Permalink
Ensure we don't realias ManyToOne.setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Saimon Moore committed Dec 14, 2008
1 parent 855ce9e commit 72e45b5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/dm-counter-cache.rb
Expand Up @@ -7,10 +7,14 @@ module CounterCacheable
def self.included(klass)
DataMapper::Associations::ManyToOne.module_eval {
extend DataMapper::CounterCacheable::ClassMethods

(class << self; self; end).class_eval do
alias_method :setup_without_counter_caching, :setup
alias_method :setup, :setup_with_counter_caching
unless method_defined?(:setup_without_counter_caching)
alias_method :setup_without_counter_caching, :setup
alias_method :setup, :setup_with_counter_caching
end
end

}
end

Expand All @@ -23,9 +27,9 @@ def setup_with_counter_caching(name, model, options = {})
if counter_cache_attribute
case counter_cache_attribute
when String, Symbol
counter_cache_attribute = counter_cache_attribute.intern
counter_cache_attribute = counter_cache_attribute.to_s
else
counter_cache_attribute = "#{model.storage_name}_count".intern
counter_cache_attribute = "#{model.storage_name}_count".to_s
end

relationship.parent_model.class_eval <<-EOS, __FILE__, __LINE__
Expand All @@ -39,13 +43,13 @@ def setup_with_counter_caching(name, model, options = {})
after :destroy, :decrement_counter_cache_for_#{parent_name}
def increment_counter_cache_for_#{parent_name}
if self.type == #{model.name}
if self.class == #{model.name}
self.#{parent_name}.update_attributes(:#{counter_cache_attribute} => self.#{parent_name}.#{counter_cache_attribute}.succ)
end
end
def decrement_counter_cache_for_#{parent_name}
if self.type == #{model.name}
if self.class == #{model.name}
self.#{parent_name}.update_attributes(:#{counter_cache_attribute} => self.#{parent_name}.#{counter_cache_attribute} - 1)
end
end
Expand Down
42 changes: 42 additions & 0 deletions spec/integration/dm-counter-cache_spec.rb
Expand Up @@ -19,24 +19,66 @@ class Comment

end

class User
include DataMapper::Resource

property :id, Integer, :serial => true

has n, :group_memberships
has n, :groups, :through => :group_memberships, :class_name => "Group", :remote_name => :group, :parent_key => [:id], :child_key => [:user_id]
end

class Group
include DataMapper::Resource

property :id, Integer, :serial => true
has n, :group_memberships
has n, :members, :through => :group_memberships, :class_name => "User", :remote_name => :user, :parent_key => [:id], :child_key => [:group_id]
end

class GroupMembership
include DataMapper::Resource
include DataMapper::CounterCacheable

property :id, Serial

belongs_to :group, :counter_cache => :members_count
belongs_to :user, :counter_cache => :groups_count
end

GroupMembership.auto_migrate!
User.auto_migrate!
Group.auto_migrate!
Comment.auto_migrate!
Post.auto_migrate!
end

before(:each) do
@post = Post.create
@user = User.create
@group = Group.create
end

it "should increment comments_count" do
@post.comments.create
@post.reload.comments_count.should == 1

@user.group_memberships.create(:group => @group)
@user.reload.groups_count.should == 1
@group.reload.members_count.should == 1
end

it "should decrement comments_count" do
comment1 = @post.comments.create
comment2 = @post.comments.create
comment2.destroy
@post.reload.comments_count.should == 1

gm1 = @user.group_memberships.create(:group => @group)
gm2 = @user.group_memberships.create(:group => @group)
gm2.destroy
@user.reload.groups_count.should == 1
@group.reload.members_count.should == 1
end

end

0 comments on commit 72e45b5

Please sign in to comment.