Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

… well.
  • Loading branch information...
commit 1ff7d4cb7ce302c4c1807eea7a72fe022b9ce35c 1 parent 3168211
Postmodern authored
Showing with 7 additions and 11 deletions.
  1. +7 −11 lib/dm-is-counter_cacheable/is/counter_cacheable.rb
18 lib/dm-is-counter_cacheable/is/counter_cacheable.rb
View
@@ -52,7 +52,9 @@ def counter_cacheable(relationship_name,options={})
@counter_cache[relationship_name] = counter_property
end
+ end
+ module InstanceMethods
private
#
@@ -61,12 +63,10 @@ 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)
@@ -74,12 +74,8 @@ def _create(*arguments)
end
end
- resource
+ super(*arguments)
end
- end
-
- module InstanceMethods
- private
#
# Destroys a resource and decrements the cache counters of the model.
Please sign in to comment.
Something went wrong with that request. Please try again.