Skip to content

Commit

Permalink
Allowing :fields or :select to be used in find interchangeably. Fixes m…
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jun 1, 2009
1 parent 2733aaa commit ac5622c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/mongo_mapper/finder_options.rb
@@ -1,4 +1,3 @@
# $lt, $lte, $gt, $gte, $in, regex
module MongoMapper
class FinderOptions
attr_reader :options
Expand Down Expand Up @@ -40,7 +39,7 @@ def convert_conditions(conditions)

def convert_options(options)
{
:fields => convert_fields(options.delete(:fields)),
:fields => convert_fields(options.delete(:fields) || options.delete(:select)),
:offset => (options.delete(:offset) || 0).to_i,
:limit => (options.delete(:limit) || 0).to_i,
:sort => convert_sort(options.delete(:order))
Expand All @@ -49,10 +48,11 @@ def convert_options(options)

def convert_fields(fields)
return if fields.blank?

if fields.is_a?(String)
fields.split(',').map { |field| field.strip }
else
fields
fields.flatten.compact
end
end

Expand All @@ -63,8 +63,8 @@ def convert_sort(sort)

hash = OrderedHash.new
pairs.each do |pair|
field, direction = pair
hash[field] = direction
field, sort_direction = pair
hash[field] = sort_direction
end
hash.symbolize_keys
end
Expand Down
16 changes: 12 additions & 4 deletions test/test_finder_options.rb
Expand Up @@ -110,16 +110,24 @@ class FinderOptionsTest < Test::Unit::TestCase
FinderOptions.new({}).options[:fields].should be(nil)
end

should "be converted to nil if {}" do
FinderOptions.new(:fields => {}).options[:fields].should be(nil)
should "be converted to nil if empty string" do
FinderOptions.new(:fields => '').options[:fields].should be(nil)
end

should "be converted to nil if []" do
FinderOptions.new(:fields => []).options[:fields].should be(nil)
end

should "should work with array" do
FinderOptions.new({:fields => ['a', 'b']}).options[:fields].should == ['a', 'b']
FinderOptions.new({:fields => %w(a b)}).options[:fields].should == %w(a b)
end

should "convert comma separated list to array" do
FinderOptions.new({:fields => 'a, b'}).options[:fields].should == ['a', 'b']
FinderOptions.new({:fields => 'a, b'}).options[:fields].should == %w(a b)
end

should "also work as select" do
FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
end
end
end # FinderOptionsTest

0 comments on commit ac5622c

Please sign in to comment.