Permalink
Browse files

`#reset_counters` verifies counter names.

Closes #9724.

Raise an `ArgumentError` when the name of the counter does not
match an association name.
  • Loading branch information...
1 parent ddb9040 commit b601399b72ab56cc01368f02615af99f45d14f02 @senny senny committed Mar 15, 2013
@@ -11,7 +11,7 @@ module ClassMethods
# ==== Parameters
#
# * +id+ - The id of the object you wish to reset a counter on.
- # * +counters+ - One or more counter names to reset
+ # * +counters+ - One or more association counters to reset
#
# ==== Examples
#
@@ -21,6 +21,7 @@ def reset_counters(id, *counters)
object = find(id)
counters.each do |association|
has_many_association = reflect_on_association(association.to_sym)
+ raise ArgumentError, "'#{self.name}' has no association called '#{association}'" unless has_many_association
if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection
has_many_association = has_many_association.through_reflection
@@ -131,4 +131,11 @@ class ::SpecialReply < ::Reply
Subscriber.reset_counters(subscriber.id, 'books')
end
end
+
+ test "the passed symbol needs to be an association name" do
+ e = assert_raises(ArgumentError) do
+ Topic.reset_counters(@topic.id, :replies_count)
+ end
+ assert_equal "'Topic' has no association called 'replies_count'", e.message
+ end
end

0 comments on commit b601399

Please sign in to comment.