Permalink
Browse files

Fixed spec failures caused by date boundary issue and postgres ILIKE …

…statement
  • Loading branch information...
1 parent 8c214c9 commit 7d5b7f3581fbdd06d6333d9bb83c55c8f0c14210 @mshibuya committed Mar 10, 2012
@@ -82,25 +82,29 @@ def get_filtering_duration(operator, value)
date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
case operator
when 'between'
- start_date = value[1].present? ? (Date.strptime(value[1], date_format).yesterday.end_of_day rescue false) : false
+ start_date = value[1].present? ? (beginning_of_date(Date.strptime(value[1], date_format)) rescue false) : false
end_date = value[2].present? ? (Date.strptime(value[2], date_format).end_of_day rescue false) : false
when 'today'
- start_date = Date.today.yesterday.end_of_day
+ start_date = beginning_of_date(Date.today)
end_date = Date.today.end_of_day
when 'yesterday'
- start_date = Date.yesterday.yesterday.end_of_day
+ start_date = beginning_of_date(Date.yesterday)
end_date = Date.yesterday.end_of_day
when 'this_week'
- start_date = Date.today.beginning_of_week.yesterday.end_of_day
+ start_date = beginning_of_date(Date.today.beginning_of_week)
end_date = Date.today.end_of_week.end_of_day
when 'last_week'
- start_date = 1.week.ago.to_date.beginning_of_week.yesterday.end_of_day
+ start_date = beginning_of_date(1.week.ago.to_date.beginning_of_week)
end_date = 1.week.ago.to_date.end_of_week.end_of_day
else # default
- start_date = (Date.strptime(Array.wrap(value).first, date_format).yesterday.end_of_day rescue false)
+ start_date = (beginning_of_date(Date.strptime(Array.wrap(value).first, date_format)) rescue false)
end_date = (Date.strptime(Array.wrap(value).first, date_format).end_of_day rescue false)
end
[start_date, end_date]
end
+
+ def beginning_of_date(date)
+ date.beginning_of_day
+ end
end
end
@@ -7,11 +7,6 @@ module ActiveRecord
DISABLED_COLUMN_TYPES = [:tsvector, :blob, :binary, :spatial]
AR_ADAPTER = ::ActiveRecord::Base.configurations[Rails.env]['adapter']
LIKE_OPERATOR = AR_ADAPTER == "postgresql" ? 'ILIKE' : 'LIKE'
- BEGINNING_OF_DAY = if AR_ADAPTER == "postgresql"
- lambda { |date| date.beginning_of_day }
- else
- lambda { |date| date.yesterday.end_of_day }
- end
def new(params = {})
AbstractObject.new(model.new(params))
@@ -181,27 +176,7 @@ def build_statement(column, type, value, operator)
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
- date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
- case operator
- when 'between'
- start_date = value[1].present? ? (Date.strptime(value[1], date_format).instance_eval(&BEGINNING_OF_DAY) rescue false) : false
- end_date = value[2].present? ? (Date.strptime(value[2], date_format).end_of_day rescue false) : false
- when 'today'
- start_date = Date.today.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.today.end_of_day
- when 'yesterday'
- start_date = Date.yesterday.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.yesterday.end_of_day
- when 'this_week'
- start_date = Date.today.beginning_of_week.instance_eval(&BEGINNING_OF_DAY)
- end_date = Date.today.end_of_week.end_of_day
- when 'last_week'
- start_date = 1.week.ago.to_date.beginning_of_week.instance_eval(&BEGINNING_OF_DAY)
- end_date = 1.week.ago.to_date.end_of_week.end_of_day
- else # default
- start_date = (Date.strptime(Array.wrap(value).first, date_format).instance_eval(&BEGINNING_OF_DAY) rescue false)
- end_date = (Date.strptime(Array.wrap(value).first, date_format).end_of_day rescue false)
- end
+ start_date, end_date = get_filtering_duration(operator, value)
if start_date && end_date
["(#{column} BETWEEN ? AND ?)", start_date, end_date]
@@ -216,6 +191,16 @@ def build_statement(column, type, value, operator)
end
end
+ if AR_ADAPTER == "postgresql"
+ def beginning_of_date(date)
+ date.beginning_of_day
+ end
+ else
+ def beginning_of_date(date)
+ date.yesterday.end_of_day
+ end
+ end
+
def association_model_lookup(association)
if association.options[:polymorphic]
RailsAdmin::AbstractModel.polymorphic_parents(:active_record, association.name) || []
View
@@ -69,6 +69,7 @@ def password_digest(password)
FieldTest.delete_all
Author.delete_all
Article.delete_all
+ MongoidFieldTest.delete_all
login_as User.create(
:email => "username@example.com",
:password => "password"
@@ -218,7 +218,7 @@ class ARComment < ActiveRecord::Base
end
it "returns query statement" do
- @abstract_model.send(:query_conditions, "word").should == ["(balls.color LIKE ?) OR (balls.type LIKE ?)", "%word%", "%word%"]
+ @abstract_model.send(:query_conditions, "word").should == ["(balls.color #{@like} ?) OR (balls.type #{@like} ?)", "%word%", "%word%"]
end
end
@@ -238,7 +238,7 @@ class ARComment < ActiveRecord::Base
describe "#build_statement" do
before do
- @abstract_model = RailsAdmin::AbstractModel.new('Team')
+ @abstract_model = RailsAdmin::AbstractModel.new('FieldTest')
end
it "ignores '_discard' operator or value" do
@@ -308,19 +308,20 @@ class ARComment < ActiveRecord::Base
@abstract_model.send(:build_statement, :field, :string, "foo", "is").should == ["(field #{@like} ?)", "foo"]
end
- [:datetime, :timestamp, :date].each do |type|
- it "supports #{type} query" do
- @abstract_model.send(:build_statement, :field, type, "", "default").should be_nil
- 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 ?)", Sat, 14 Jan 2012 23:59:59 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 ?)", Fri, 13 Jan 2012 23:59:59 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 ?)", Sun, 08 Jan 2012 23:59:59 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 ?)", Sun, 01 Jan 2012 23:59:59 UTC +00:00, Sun, 08 Jan 2012 23:59:59 UTC +00:00]'
- end
+ context 'filters on dates' do
+ it 'lists elements within outbound limits' do
+ date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
+
+ FieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
+ FieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
+
end
end
@@ -346,25 +347,4 @@ class ARComment < ActiveRecord::Base
RailsAdmin::AbstractModel.new('User').serialized_attributes.should == ["roles"]
end
end
-
-
- describe '#all' do
- context 'filters on dates' do
- it 'lists elements within outbound limits' do
- date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
-
- FieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
- FieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
- RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
- RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
- RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
- RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
- RailsAdmin.config(FieldTest).abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
-
- end
- end
- end
-
end
@@ -433,7 +433,7 @@ class MongoComment
describe "#build_statement" do
before do
- @abstract_model = RailsAdmin::AbstractModel.new('Article')
+ @abstract_model = RailsAdmin::AbstractModel.new('MongoidFieldTest')
end
it "ignores '_discard' operator or value" do
@@ -503,20 +503,20 @@ class MongoComment
@abstract_model.send(:build_statement, :field, :string, "foo", "is").should == {:field=>'foo'}
end
- [:datetime, :timestamp, :date].each do |type|
- 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
- Timecop.freeze(Time.utc(2012,1,15,12,0,0)) do
- @abstract_model.send(:build_statement, :field, type, "", "today").to_s.should ==
- '{:field=>{"$gte"=>Sat, 14 Jan 2012 23:59:59 UTC +00:00, "$lte"=>Sun, 15 Jan 2012 23:59:59 UTC +00:00}}'
- @abstract_model.send(:build_statement, :field, type, "", "yesterday").to_s.should ==
- '{:field=>{"$gte"=>Fri, 13 Jan 2012 23:59:59 UTC +00:00, "$lte"=>Sat, 14 Jan 2012 23:59:59 UTC +00:00}}'
- @abstract_model.send(:build_statement, :field, type, "", "this_week").to_s.should ==
- '{:field=>{"$gte"=>Sun, 08 Jan 2012 23:59:59 UTC +00:00, "$lte"=>Sun, 15 Jan 2012 23:59:59 UTC +00:00}}'
- @abstract_model.send(:build_statement, :field, type, "", "last_week").to_s.should ==
- '{:field=>{"$gte"=>Sun, 01 Jan 2012 23:59:59 UTC +00:00, "$lte"=>Sun, 08 Jan 2012 23:59:59 UTC +00:00}}'
- end
+ context 'filters on dates' do
+ it 'lists elements within outbound limits' do
+ date_format = I18n.t("admin.misc.filter_date_format", :default => I18n.t("admin.misc.filter_date_format", :locale => :en)).gsub('dd', '%d').gsub('mm', '%m').gsub('yy', '%Y')
+
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/01/2012", date_format))
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/02/2012", date_format))
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/03/2012", date_format))
+ MongoidFieldTest.create!(:date_field => Date.strptime("01/04/2012", date_format))
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/02/2012"], :o => 'between' } } } ).count.should == 1
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "01/03/2012", ""], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["", "", "01/02/2012"], :o => 'between' } } } ).count.should == 2
+ @abstract_model.all(:filters => { "date_field" => { "1" => { :v => ["01/02/2012"], :o => 'default' } } } ).count.should == 1
+
end
end

0 comments on commit 7d5b7f3

Please sign in to comment.