mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
defining a named_scope which overwrites an existing method is now allowed we just log a warning.
This was motivated by the fact that :open is defined on all classes as such the named_scope "open" can never be used, without hacking ActiveRecord with an "undef_method" [#4083 state:resolved] Signed-off-by: wycats <wycats@gmail.com>
This commit is contained in:
committed by
wycats
parent
77a2a3d9b3
commit
b0967cc5cf
@@ -102,7 +102,8 @@ module ActiveRecord
|
||||
name = name.to_sym
|
||||
|
||||
if !scopes[name] && respond_to?(name, true)
|
||||
raise ArgumentError, "Cannot define scope :#{name} because #{self.name}.#{name} method already exists."
|
||||
logger.warn "Creating scope :#{name}. " \
|
||||
"Overwriting existing method #{self.name}.#{name}."
|
||||
end
|
||||
|
||||
scopes[name] = lambda do |parent_scope, *args|
|
||||
|
||||
@@ -371,8 +371,21 @@ class NamedScopeTest < ActiveRecord::TestCase
|
||||
end
|
||||
|
||||
def test_named_scopes_with_reserved_names
|
||||
[:where, :with_scope].each do |protected_method|
|
||||
assert_raises(ArgumentError) { Topic.scope protected_method }
|
||||
class << Topic
|
||||
def public_method; end
|
||||
public :public_method
|
||||
|
||||
def protected_method; end
|
||||
protected :protected_method
|
||||
|
||||
def private_method; end
|
||||
private :private_method
|
||||
end
|
||||
|
||||
[:public_method, :protected_method, :private_method].each do |reserved_method|
|
||||
assert Topic.respond_to?(reserved_method, true)
|
||||
ActiveRecord::Base.logger.expects(:warn)
|
||||
Topic.scope(reserved_method)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user