mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #17374 from maurogeorge/scope-exception
Raises ArgumentError when try to define a scope without a callable
This commit is contained in:
commit
d616fec811
3 changed files with 15 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
* Raise `ArgumentError` when the body of a scope is not callable.
|
||||
|
||||
*Mauro George*
|
||||
|
||||
* Use type column first in multi-column indexes created with `add-reference`.
|
||||
|
||||
*Derek Prior*
|
||||
|
|
|
@ -139,6 +139,10 @@ module ActiveRecord
|
|||
# Article.published.featured.latest_article
|
||||
# Article.featured.titles
|
||||
def scope(name, body, &block)
|
||||
unless body.respond_to?:call
|
||||
raise ArgumentError, 'The scope body needs to be callable.'
|
||||
end
|
||||
|
||||
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,13 @@ class NamedScopingTest < ActiveRecord::TestCase
|
|||
assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5)
|
||||
end
|
||||
|
||||
def test_scopes_body_is_a_callable
|
||||
e = assert_raises ArgumentError do
|
||||
Class.new(Post).class_eval { scope :containing_the_letter_z, where("body LIKE '%z%'") }
|
||||
end
|
||||
assert_equal "The scope body needs to be callable.", e.message
|
||||
end
|
||||
|
||||
def test_active_records_have_scope_named__all__
|
||||
assert !Topic.all.empty?
|
||||
|
||||
|
|
Loading…
Reference in a new issue