Skip to content

Commit

Permalink
Merge pull request rails#10134 from derikson/collection_proxy_select_…
Browse files Browse the repository at this point in the history
…with_multiple_args

Change CollectionProxy#select to take the same arguments as ActiveRecord::select
  • Loading branch information
rafaelfranca committed Jan 3, 2014
2 parents f89266a + a8ede36 commit 19b2188
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Expand Up @@ -66,11 +66,11 @@ def reset
@target = []
end

def select(select = nil)
def select(*fields)
if block_given?
load_target.select.each { |e| yield e }
else
scope.select(select)
scope.select(*fields)
end
end

Expand Down
Expand Up @@ -75,7 +75,7 @@ def loaded?
# # #<Pet id: nil, name: "Choo-Choo">
# # ]
#
# person.pets.select([:id, :name])
# person.pets.select(:id, :name )
# # => [
# # #<Pet id: 1, name: "Fancy-Fancy">,
# # #<Pet id: 2, name: "Spook">,
Expand Down Expand Up @@ -106,8 +106,8 @@ def loaded?
# # #<Pet id: 2, name: "Spook">,
# # #<Pet id: 3, name: "Choo-Choo">
# # ]
def select(select = nil, &block)
@association.select(select, &block)
def select(*fields, &block)
@association.select(*fields, &block)
end

# Finds an object in the collection responding to the +id+. Uses the same
Expand Down
Expand Up @@ -455,7 +455,11 @@ def test_default_select
end

def test_select_query_method
assert_equal ['id'], posts(:welcome).comments.select(:id).first.attributes.keys
assert_equal ['id', 'body'], posts(:welcome).comments.select(:id, :body).first.attributes.keys
end

def test_select_with_block
assert_equal [1], posts(:welcome).comments.select { |c| c.id == 1 }.map(&:id)
end

def test_select_without_foreign_key
Expand Down

0 comments on commit 19b2188

Please sign in to comment.