Permalink
Browse files

Added specs for Mongoid adapter

  • Loading branch information...
mshibuya committed Mar 2, 2012
1 parent 82858c7 commit bacfe92ee38e4481b1d7d4adbf49cadea1ebb0e4
@@ -188,10 +188,10 @@ def build_statement(column, type, value, operator)
[1.week.ago.to_date.beginning_of_week.beginning_of_day, 1.week.ago.to_date.end_of_week.end_of_day]
when 'less_than'
return if value.blank?
- return ["(#{column} > ?)", value.to_i.days.ago]
+ return ["(#{column} >= ?)", value.to_i.days.ago]
when 'more_than'
return if value.blank?
- return ["(#{column} < ?)", value.to_i.days.ago]
+ return ["(#{column} <= ?)", value.to_i.days.ago]
when 'mmddyyyy'
return if (value.blank? || value.match(/([0-9]{8})/).nil?)
[Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').beginning_of_day, Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').end_of_day]
@@ -31,7 +31,7 @@ def all(options = {},scope=nil)
scope ||= self.scoped
scope = scope.includes(options[:include]) if options[:include]
scope = scope.limit(options[:limit]) if options[:limit]
- scope = scope.all_in(:_id => options[:bulk_ids]) if options[:bulk_ids]
+ scope = scope.any_in(:_id => options[:bulk_ids]) if options[:bulk_ids]
scope = scope.where(query_conditions(options[:query])) if options[:query]
scope = scope.where(filter_conditions(options[:filters])) if options[:filters]
scope = scope.page(options[:page]).per(options[:per]) if options[:page] && options[:per]
@@ -220,6 +220,8 @@ def build_statement(column, type, value, operator)
Regexp.compile("#{Regexp.escape(value)}$")
when 'is', '='
value.to_s
+ else
+ return
end
{ column => value }
when :datetime, :timestamp, :date
@@ -235,20 +237,20 @@ def build_statement(column, type, value, operator)
[1.week.ago.to_date.beginning_of_week.beginning_of_day, 1.week.ago.to_date.end_of_week.end_of_day]
when 'less_than'
return if value.blank?
- [value.to_i.days.ago, DateTime.now]
+ return { column => { '$gte' => value.to_i.days.ago } }
when 'more_than'
return if value.blank?
- [2000.years.ago, value.to_i.days.ago]
+ return { column => { '$lte' => value.to_i.days.ago } }
when 'mmddyyyy'
return if (value.blank? || value.match(/([0-9]{8})/).nil?)
[Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').beginning_of_day, Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').end_of_day]
+ else
+ return
end
{ column => { '$gte' => values[0], '$lte' => values[1] } }
when :enum
return if value.blank?
- { column => [value].flatten }
- else
- { column => value }
+ { column => { "$in" => Array.wrap(value) } }
end
end
View
@@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'paperclip'
gem.add_development_dependency 'rspec-rails'
gem.add_development_dependency 'simplecov'
+ gem.add_development_dependency 'timecop'
gem.authors = ["Erik Michaels-Ober", "Bogdan Gaza", "Petteri Kaapa", "Benoit Benezech"]
gem.description = %q{RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data.}
gem.email = ['sferik@gmail.com', 'bogdan@cadmio.org', 'petteri.kaapa@gmail.com']
@@ -5,6 +5,7 @@ class MongoidFieldTest
field :title, :type => String
field :subject, :type => String
field :description, :type => String
+ field :short_text, :type => String
field :array_field, :type => Array
field :big_decimal_field, :type => BigDecimal
field :boolean_field, :type => Boolean
@@ -16,4 +17,6 @@ class MongoidFieldTest
field :integer_field, :type => Integer
field :time_field, :type => Time
field :object_field, :type => Object
+
+ validates :short_text, :length => {:maximum => 255}
end
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'timecop'
require 'rails_admin/adapters/active_record'
@@ -314,19 +315,21 @@ class ARComment < ActiveRecord::Base
it "supports #{type} query" do
@abstract_model.send(:build_statement, :field, type, "", "default").should be_nil
@abstract_model.send(:build_statement, :field, type, "", "is").should be_nil
- @abstract_model.send(:build_statement, :field, type, "", "today").should ==
- ["(field BETWEEN ? AND ?)", Date.today.beginning_of_day, Date.today.end_of_day]
- @abstract_model.send(:build_statement, :field, type, "", "yesterday").should ==
- ["(field BETWEEN ? AND ?)", Date.yesterday.beginning_of_day, Date.yesterday.end_of_day]
- @abstract_model.send(:build_statement, :field, type, "", "this_week").should ==
- ["(field BETWEEN ? AND ?)", Date.today.beginning_of_week.beginning_of_day, Date.today.end_of_week.end_of_day]
- @abstract_model.send(:build_statement, :field, type, "", "last_week").should ==
- ["(field BETWEEN ? AND ?)", 1.week.ago.to_date.beginning_of_week.beginning_of_day, 1.week.ago.to_date.end_of_week.end_of_day]
- @abstract_model.send(:build_statement, :field, type, "", "less_than").should be_nil
- @abstract_model.send(:build_statement, :field, type, "1", "less_than").
- should satisfy{|e| e.first == "(field > ?)" && (Time.now - e.last).between?(1.day.to_i, 2.day.to_i-1) }
- @abstract_model.send(:build_statement, :field, type, "1", "more_than").
- should satisfy{|e| e.first == "(field < ?)" && (Time.now - e.last).between?(1.day.to_i, 2.day.to_i-1) }
+ Timecop.freeze(Time.utc(2012,1,15,12,0,0)) do
+ @abstract_model.send(:build_statement, :field, type, "", "today").to_s.should ==
+ '["(field BETWEEN ? AND ?)", Sun, 15 Jan 2012 00:00:00 UTC +00:00, Sun, 15 Jan 2012 23:59:59 UTC +00:00]'
+ @abstract_model.send(:build_statement, :field, type, "", "yesterday").to_s.should ==
+ '["(field BETWEEN ? AND ?)", Sat, 14 Jan 2012 00:00:00 UTC +00:00, Sat, 14 Jan 2012 23:59:59 UTC +00:00]'
+ @abstract_model.send(:build_statement, :field, type, "", "this_week").to_s.should ==
+ '["(field BETWEEN ? AND ?)", Mon, 09 Jan 2012 00:00:00 UTC +00:00, Sun, 15 Jan 2012 23:59:59 UTC +00:00]'
+ @abstract_model.send(:build_statement, :field, type, "", "last_week").to_s.should ==
+ '["(field BETWEEN ? AND ?)", Mon, 02 Jan 2012 00:00:00 UTC +00:00, Sun, 08 Jan 2012 23:59:59 UTC +00:00]'
+ @abstract_model.send(:build_statement, :field, type, "", "less_than").should be_nil
+ @abstract_model.send(:build_statement, :field, type, "1", "less_than").to_s.should ==
+ '["(field >= ?)", Sat, 14 Jan 2012 12:00:00 UTC +00:00]'
+ @abstract_model.send(:build_statement, :field, type, "1", "more_than").to_s.should ==
+ '["(field <= ?)", Sat, 14 Jan 2012 12:00:00 UTC +00:00]'
+ end
@abstract_model.send(:build_statement, :field, type, "", "mmddyyyy").should be_nil
@abstract_model.send(:build_statement, :field, type, "201105", "mmddyyyy").should be_nil
@abstract_model.send(:build_statement, :field, type, "12312011", "mmddyyyy").to_s.should ==
Oops, something went wrong.

0 comments on commit bacfe92

Please sign in to comment.