Permalink
Browse files

Adding support for entering boolean values 'true' and 'false' for mat…

…ching boolean field values, as well as fields with nil values
  • Loading branch information...
1 parent daa1e46 commit d9d1c72e9569006a7beb65c6e1f3dce0245ea4ce @vicentemundim committed Sep 10, 2010
Showing with 38 additions and 3 deletions.
  1. +19 −2 lib/mongoid_query_string_interface.rb
  2. +1 −1 lib/version.rb
  3. +18 −0 spec/mongoid_query_string_interface_spec.rb
@@ -95,15 +95,20 @@ def unescape(value)
end
def parse_value(value, operator)
- parse_date(value) or parse_integer(value) or parse_float(value) or parse_array(value, operator) or parse_regex(value) or value
+ parse_date(value) or
+ parse_integer(value) or
+ parse_float(value) or
+ parse_array(value, operator) or
+ parse_regex(value) or
+ parse_boolean_and_nil(value)
end
def parse_date(date)
date.to_time and Time.parse(date)
rescue Exception
nil
end
-
+
def parse_integer(integer)
if match = integer.match(/^\d+$/)
match[0].to_i
@@ -128,6 +133,18 @@ def parse_regex(regex)
end
end
+ def parse_boolean_and_nil(value)
+ unless value.nil? || value.empty?
+ if ['true', 'false'].include?(value.strip)
+ value.strip == 'true'
+ elsif value.strip == 'nil'
+ nil
+ else
+ value
+ end
+ end
+ end
+
def array_operator?(operator)
ARRAY_CONDITIONAL_OPERATORS.map { |op| "$#{op}" }.include?(operator.to_s)
end
View
@@ -1,6 +1,6 @@
# encoding: utf-8
module Mongoid #:nodoc
module QueryStringInterface #:nodoc
- VERSION = "0.1.6"
+ VERSION = "0.1.7"
end
end
@@ -35,13 +35,15 @@ class EmbeddedDocument
let :document do
Document.create :title => 'Some Title', :some_integer => 1, :some_float => 1.1, :status => 'published',
:created_at => 5.days.ago.to_time, :tags => ['esportes', 'basquete', 'flamengo'],
+ :some_boolean => true, :other_boolean => false, :nil_value => nil,
:embedded_document => { :name => 'embedded document',
:tags => ['bar', 'foo', 'yeah'] }
end
let :other_document do
Document.create :title => 'Some Other Title', :some_integer => 2, :some_float => 2.2, :status => 'published',
:created_at => 2.days.ago.to_time, :tags => ['esportes', 'futebol', 'jabulani', 'flamengo'],
+ :some_boolean => false, :other_boolean => true, :nil_value => 'not_nil',
:embedded_document => { :name => 'other embedded document',
:tags => ['yup', 'uhu', 'yeah', 'H4', '4H', '4H4', 'H4.1', '4.1H', '4.1H4.1'] }
end
@@ -225,6 +227,22 @@ class EmbeddedDocument
end
end
+ context 'with boolean values' do
+ it 'should accept "true" string as a boolean value' do
+ Document.filter_by('some_boolean' => 'true').should == [document]
+ end
+
+ it 'should accept "false" string as a boolean value' do
+ Document.filter_by('other_boolean' => 'false').should == [document]
+ end
+ end
+
+ context 'with nil value' do
+ it 'should accept "nil" string as nil value' do
+ Document.filter_by('nil_value' => 'nil').should == [document]
+ end
+ end
+
context 'with array values' do
let :document_with_similar_tags do
Document.create :title => 'Some Title', :some_number => 1, :status => 'published',

0 comments on commit d9d1c72

Please sign in to comment.