Browse files

Hook _save and check for new?, this way we can hook Model.new.save as…

… well.
  • Loading branch information...
1 parent 3168211 commit 1ff7d4cb7ce302c4c1807eea7a72fe022b9ce35c @postmodern committed Jul 17, 2010
Showing with 7 additions and 11 deletions.
  1. +7 −11 lib/dm-is-counter_cacheable/is/counter_cacheable.rb
View
18 lib/dm-is-counter_cacheable/is/counter_cacheable.rb
@@ -52,7 +52,9 @@ def counter_cacheable(relationship_name,options={})
@counter_cache[relationship_name] = counter_property
end
+ end
+ module InstanceMethods
private
#
@@ -61,25 +63,19 @@ def counter_cacheable(relationship_name,options={})
# @return [DataMapper::Resource]
# The new resource.
#
- def _create(*arguments)
- resource = super(*arguments)
-
- if self.counter_cache
- self.counter_cache.each do |relationship,property|
- parent_resource = resource.send(relationship)
+ def _save(*arguments)
+ if (self.new? && self.class.counter_cache)
+ self.class.counter_cache.each do |relationship,property|
+ parent_resource = self.send(relationship)
count = parent_resource.attribute_get(property)
parent_resource.attribute_set(property,count + 1)
parent_resource.save
end
end
- resource
+ super(*arguments)
end
- end
-
- module InstanceMethods
- private
#
# Destroys a resource and decrements the cache counters of the model.

0 comments on commit 1ff7d4c

Please sign in to comment.