Permalink
Browse files

Model.select takes a variable list of arguments.

  This is a change from the behavior of passing in an Array of args.
  This solution attempts to answer issue #3165
  • Loading branch information...
Isaac Sanders Isaac Sanders
Isaac Sanders authored and Isaac Sanders committed Apr 29, 2012
1 parent 9ce057d commit d22a58fc3e724d7dba13dc32759c05e113b07f30
@@ -108,24 +108,27 @@ def references!(*args)
#
# The argument to the method can also be an array of fields.
#
- # >> Model.select([:field, :other_field, :and_one_more])
+ # >> Model.select(:field, :other_field, :and_one_more)
# => [#<Model field: "value", other_field: "value", and_one_more: "value">]
#
# Accessing attributes of an object that do not have fields retrieved by a select
# will throw <tt>ActiveModel::MissingAttributeError</tt>:
#
# >> Model.select(:field).first.other_field
# => ActiveModel::MissingAttributeError: missing attribute: other_field
- def select(value = Proc.new)
+ def select(*fields, &block)
if block_given?
- to_a.select { |*block_args| value.call(*block_args) }
+ to_a.select { |*block_args| block.call(*block_args) }
else
- clone.select!(value)
+ raise ArgumentError, 'Call this with at least one field' if fields.empty?
+ clone.select!(*fields)
end
end
- def select!(value)
- self.select_values += Array.wrap(value)
+ def select!(*fields)
+ fields.each do |field|
+ self.select_values += Array.wrap(field)
+ end
self
end
@@ -692,6 +692,13 @@ def test_delete_all_limit_error
assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }
end
+ def test_select_takes_a_variable_list_of_args
+ assert_nothing_raised do
+ Developer.select(:name)
+ Developer.select(:name, :salary)
+ end
+ end
+
def test_select_argument_error
assert_raises(ArgumentError) { Developer.select }
end

0 comments on commit d22a58f

Please sign in to comment.