Skip to content

Commit b0967cc

Browse files
matthewrudywycats
authored andcommitted
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>
1 parent 77a2a3d commit b0967cc

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

activerecord/lib/active_record/named_scope.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def scope(name, options = {}, &block)
102102
name = name.to_sym
103103

104104
if !scopes[name] && respond_to?(name, true)
105-
raise ArgumentError, "Cannot define scope :#{name} because #{self.name}.#{name} method already exists."
105+
logger.warn "Creating scope :#{name}. " \
106+
"Overwriting existing method #{self.name}.#{name}."
106107
end
107108

108109
scopes[name] = lambda do |parent_scope, *args|

activerecord/test/cases/named_scope_test.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,21 @@ def test_table_names_for_chaining_scopes_with_and_without_table_name_included
371371
end
372372

373373
def test_named_scopes_with_reserved_names
374-
[:where, :with_scope].each do |protected_method|
375-
assert_raises(ArgumentError) { Topic.scope protected_method }
374+
class << Topic
375+
def public_method; end
376+
public :public_method
377+
378+
def protected_method; end
379+
protected :protected_method
380+
381+
def private_method; end
382+
private :private_method
383+
end
384+
385+
[:public_method, :protected_method, :private_method].each do |reserved_method|
386+
assert Topic.respond_to?(reserved_method, true)
387+
ActiveRecord::Base.logger.expects(:warn)
388+
Topic.scope(reserved_method)
376389
end
377390
end
378391

0 commit comments

Comments
 (0)