Skip to content
Browse files

Use define_method when method name contains weird characters.

  • Loading branch information...
1 parent c4a7c31 commit 72de8946515725b8827939d90698e2a082a5eae1 @KrzysiekJ KrzysiekJ committed Feb 13, 2013
View
8 activerecord/lib/active_record/relation/delegation.rb
@@ -37,11 +37,9 @@ def #{method}(*args, &block)
end
RUBY
else
- module_eval <<-RUBY, __FILE__, __LINE__ + 1
- def #{method}(*args, &block)
- scoping { @klass.send(#{method.inspect}, *args, &block) }
- end
- RUBY
+ define_method method do |*args, &block|
+ scoping { @klass.send(method, *args, &block) }
+ end
end
end
end
View
14 activerecord/test/cases/named_scope_test.rb
@@ -271,6 +271,20 @@ def test_should_build_on_top_of_chained_scopes
assert_equal 'lifo', topic.author_name
end
+ # Method delegation for scope names which look like /\A[a-zA-Z_]\w*[!?]?\z/
+ # has been done by evaluating a string with a plain def statement. For scope
+ # names which contain spaces this approach doesn't work.
+ def test_spaces_in_scope_names
+ klass = Class.new(ActiveRecord::Base) do
+ self.table_name = "topics"
+ scope :"title containing space", -> { where("title LIKE '% %'") }
+ scope :approved, -> { where(:approved => true) }
+ end
+ assert_equal klass.send(:"title containing space"), klass.where("title LIKE '% %'")
+ assert_equal klass.approved.send(:"title containing space"), klass.approved.where("title LIKE '% %'")
+ end
+ end
+
def test_find_all_should_behave_like_select
assert_equal Topic.base.to_a.select(&:approved), Topic.base.to_a.find_all(&:approved)
end

0 comments on commit 72de894

Please sign in to comment.
Something went wrong with that request. Please try again.