提交 7710d7f4 编写于 作者: R Rafael França

Merge pull request #23331 from amatsuda/valid_scope_name

Warn if a named scope is overwriting an existing scope or method
......@@ -151,6 +151,7 @@ def scope(name, body, &block)
"a class method with the same name."
end
valid_scope_name?(name)
extension = Module.new(&block) if block
if body.respond_to?(:to_proc)
......@@ -169,6 +170,15 @@ def scope(name, body, &block)
end
end
end
protected
def valid_scope_name?(name)
if respond_to?(name, true)
logger.warn "Creating scope :#{name}. " \
"Overwriting existing method #{self.name}.#{name}."
end
end
end
end
end
......
......@@ -440,6 +440,25 @@ def test_table_names_for_chaining_scopes_with_and_without_table_name_included
end
end
def test_scopes_with_reserved_names
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)
silence_warnings { Topic.scope(reserved_method, -> { }) }
end
end
def test_scopes_on_relations
# Topic.replied
approved_topics = Topic.all.approved.order('id DESC')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册