mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make sure named_scope names are not used as method names already
This commit is contained in:
parent
5a6596787b
commit
f0cde5be54
3 changed files with 18 additions and 7 deletions
|
@ -99,6 +99,11 @@ module ActiveRecord
|
|||
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
||||
def named_scope(name, options = {}, &block)
|
||||
name = name.to_sym
|
||||
|
||||
if !scopes[name] && respond_to?(name, true)
|
||||
raise ArgumentError, "Cannot define named_scope :#{name} because #{self.name}.#{name} method already exists."
|
||||
end
|
||||
|
||||
scopes[name] = lambda do |parent_scope, *args|
|
||||
Scope.new(parent_scope, case options
|
||||
when Hash
|
||||
|
|
|
@ -150,13 +150,13 @@ class NamedScopeTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_named_scopes_honor_current_scopes_from_when_defined
|
||||
assert !Post.ranked_by_comments.limit(5).empty?
|
||||
assert !authors(:david).posts.ranked_by_comments.limit(5).empty?
|
||||
assert_not_equal Post.ranked_by_comments.limit(5), authors(:david).posts.ranked_by_comments.limit(5)
|
||||
assert !Post.ranked_by_comments.limit_by(5).empty?
|
||||
assert !authors(:david).posts.ranked_by_comments.limit_by(5).empty?
|
||||
assert_not_equal Post.ranked_by_comments.limit_by(5), authors(:david).posts.ranked_by_comments.limit_by(5)
|
||||
assert_not_equal Post.top(5), authors(:david).posts.top(5)
|
||||
# Oracle sometimes sorts differently if WHERE condition is changed
|
||||
assert_equal authors(:david).posts.ranked_by_comments.limit(5).sort_by(&:id), authors(:david).posts.top(5).sort_by(&:id)
|
||||
assert_equal Post.ranked_by_comments.limit(5), Post.top(5)
|
||||
assert_equal authors(:david).posts.ranked_by_comments.limit_by(5).sort_by(&:id), authors(:david).posts.top(5).sort_by(&:id)
|
||||
assert_equal Post.ranked_by_comments.limit_by(5), Post.top(5)
|
||||
end
|
||||
|
||||
def test_active_records_have_scope_named__all__
|
||||
|
@ -374,6 +374,12 @@ class NamedScopeTest < ActiveRecord::TestCase
|
|||
Comment.for_first_post.for_first_author.all
|
||||
end
|
||||
end
|
||||
|
||||
def test_named_scopes_with_reserved_names
|
||||
[:where, :with_scope].each do |protected_method|
|
||||
assert_raises(ArgumentError) { Topic.named_scope protected_method }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class DynamicScopeMatchTest < ActiveRecord::TestCase
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class Post < ActiveRecord::Base
|
||||
named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'"
|
||||
named_scope :ranked_by_comments, :order => "comments_count DESC"
|
||||
named_scope :limit, lambda {|limit| {:limit => limit} }
|
||||
named_scope :limit_by, lambda {|limit| {:limit => limit} }
|
||||
named_scope :with_authors_at_address, lambda { |address| {
|
||||
:conditions => [ 'authors.author_address_id = ?', address.id ],
|
||||
:joins => 'JOIN authors ON authors.id = posts.author_id'
|
||||
|
@ -73,7 +73,7 @@ class Post < ActiveRecord::Base
|
|||
has_many :impatient_people, :through => :skimmers, :source => :person
|
||||
|
||||
def self.top(limit)
|
||||
ranked_by_comments.limit(limit)
|
||||
ranked_by_comments.limit_by(limit)
|
||||
end
|
||||
|
||||
def self.reset_log
|
||||
|
|
Loading…
Reference in a new issue