Skip to content
This repository
Browse code

Merge pull request #9274 from KrzysiekJ/spaces_in_scope_names

Use define_method when method name contains weird characters.
  • Loading branch information...
commit 7b0402fabb2ed9801108b060434f83e6f25a4748 2 parents e743850 + 72de894
Rafael Mendonça França rafaelfranca authored
8 activerecord/lib/active_record/relation/delegation.rb
@@ -37,11 +37,9 @@ def #{method}(*args, &block)
37 37 end
38 38 RUBY
39 39 else
40   - module_eval <<-RUBY, __FILE__, __LINE__ + 1
41   - def #{method}(*args, &block)
42   - scoping { @klass.send(#{method.inspect}, *args, &block) }
43   - end
44   - RUBY
  40 + define_method method do |*args, &block|
  41 + scoping { @klass.send(method, *args, &block) }
  42 + end
45 43 end
46 44 end
47 45 end
14 activerecord/test/cases/named_scope_test.rb
@@ -271,6 +271,20 @@ def test_should_build_on_top_of_chained_scopes
271 271 assert_equal 'lifo', topic.author_name
272 272 end
273 273
  274 + # Method delegation for scope names which look like /\A[a-zA-Z_]\w*[!?]?\z/
  275 + # has been done by evaluating a string with a plain def statement. For scope
  276 + # names which contain spaces this approach doesn't work.
  277 + def test_spaces_in_scope_names
  278 + klass = Class.new(ActiveRecord::Base) do
  279 + self.table_name = "topics"
  280 + scope :"title containing space", -> { where("title LIKE '% %'") }
  281 + scope :approved, -> { where(:approved => true) }
  282 + end
  283 + assert_equal klass.send(:"title containing space"), klass.where("title LIKE '% %'")
  284 + assert_equal klass.approved.send(:"title containing space"), klass.approved.where("title LIKE '% %'")
  285 + end
  286 + end
  287 +
274 288 def test_find_all_should_behave_like_select
275 289 assert_equal Topic.base.to_a.select(&:approved), Topic.base.to_a.find_all(&:approved)
276 290 end

0 comments on commit 7b0402f

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