Skip to content

Commit

Permalink
Remove valid_scope_name? check - use ruby
Browse files Browse the repository at this point in the history
scope is syntactic sugar for defining a class method. Ruby allows
redefining methods but emits a warning when run with -w. So let's
not implement our own logic for this. Users should run with -w if they
want to be warned about redefined methods.
  • Loading branch information
jonleighton committed Mar 21, 2012
1 parent 884e5b7 commit f6db31e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 30 deletions.
12 changes: 1 addition & 11 deletions activerecord/lib/active_record/scoping/named.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ def scope_attributes? # :nodoc:
# Article.featured.titles # Article.featured.titles


def scope(name, scope_options = {}) def scope(name, scope_options = {})
valid_scope_name?(name)
extension = Module.new(&Proc.new) if block_given? extension = Module.new(&Proc.new) if block_given?


singleton_class.send(:redefine_method, name) do |*args| singleton_class.send(:define_method, name) do |*args|
options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
options = scoped.apply_finder_options(options) if options.is_a?(Hash) options = scoped.apply_finder_options(options) if options.is_a?(Hash)


Expand All @@ -184,15 +183,6 @@ def scope(name, scope_options = {})
extension ? relation.extending(extension) : relation extension ? relation.extending(extension) : relation
end 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 end
end end
Expand Down
19 changes: 0 additions & 19 deletions activerecord/test/cases/named_scope_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -393,25 +393,6 @@ def test_table_names_for_chaining_scopes_with_and_without_table_name_included
end end
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)
Topic.scope(reserved_method)
end
end

def test_scopes_on_relations def test_scopes_on_relations
# Topic.replied # Topic.replied
approved_topics = Topic.scoped.approved.order('id DESC') approved_topics = Topic.scoped.approved.order('id DESC')
Expand Down

0 comments on commit f6db31e

Please sign in to comment.