Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use define_method when method name contains weird characters. #9274

Merged
merged 1 commit into from Mar 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions activerecord/lib/active_record/relation/delegation.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -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
Expand Down