提交 b570b6b1 编写于 作者: M Mauro George

Raises ArgumentError when try to define a scope without a callable

This changes the actual exception `NoMethodError: undefined method `call'
for #<ActiveRecord::Relation []>` to a `ArgumentError` when try to define
a scope without a callable.
上级 be49ec4b
......@@ -139,6 +139,9 @@ def scope_attributes? # :nodoc:
# Article.published.featured.latest_article
# Article.featured.titles
def scope(name, body, &block)
raise ArgumentError, 'You need to suply a callable as scope body' unless
body.respond_to?(:call)
if dangerous_class_method?(name)
raise ArgumentError, "You tried to define a scope named \"#{name}\" " \
"on the model \"#{self.name}\", but Active Record already defined " \
......
......@@ -132,6 +132,12 @@ def test_scopes_honor_current_scopes_from_when_defined
assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5)
end
def test_scopes_body_is_a_callable
assert_raises(ArgumentError, 'You need to suply a callable as scope body') do
Post.class_eval { scope :containing_the_letter_z, where("body LIKE '%z%'") }
end
end
def test_active_records_have_scope_named__all__
assert !Topic.all.empty?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册