Skip to content

Commit bd577c5

Browse files
authored
Fix wrong value for lteq_datetime predicate (#63)
1 parent 62443e6 commit bd577c5

File tree

7 files changed

+137
-88
lines changed

7 files changed

+137
-88
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Gemfile.lock
22
coverage/
3-
spec/rails/rails-5.2.1/
3+
spec/rails/rails-*
44
pkg
5+
.bundle
6+
vendor/bundl

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ group :test do
1010
gem 'rails', "~> #{ENV['RAILS'] || default_rails_version}"
1111
gem 'activeadmin', "~> #{ENV['AA'] || default_activeadmin_version}"
1212

13+
gem 'sprockets-rails', '3.0.4'
1314
gem 'rspec-rails'
1415
gem 'coveralls', require: false # Test coverage website. Go to https://coveralls.io
1516
gem 'sass-rails'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
ActiveAdmin.setup do |config|
22
config.register_stylesheet 'jquery.xdan.datetimepicker.css'
33
end
4+
5+
Ransack.configure do |config|
6+
config.add_predicate 'gteq_datetime_picker',
7+
arel_predicate: 'gteq'
8+
9+
config.add_predicate 'lteq_datetime_picker',
10+
arel_predicate: 'lt'
11+
end

lib/active_admin_datetimepicker/inputs/filters/date_time_range_input.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeh
1111
end
1212
end
1313

14+
def gt_input_name
15+
column && column.type == :date ? super : "#{method}_gteq_datetime_picker"
16+
end
17+
18+
def lt_input_name
19+
column && column.type == :date ? super : "#{method}_lteq_datetime_picker"
20+
end
21+
1422
end
1523
end
1624
end

spec/edit_form_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe 'authors index', type: :feature, js: true do
4+
before do
5+
add_author_resource
6+
end
7+
8+
context 'edit form' do
9+
before do
10+
visit '/admin/authors/new'
11+
end
12+
13+
before do
14+
page.find('#author_birthday').click
15+
16+
page.find('.xdsoft_datetimepicker', visible: true)
17+
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
18+
page.find('.xdsoft_datetimepicker', visible: true)
19+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
20+
end
21+
22+
it 'can set birthday' do
23+
date_birthday = Date.today.beginning_of_month.strftime("%Y-%m-%d")
24+
expect(page.find('#author_birthday').value).to start_with(date_birthday)
25+
expect(page).to have_css('#author_birthday[placeholder="Formtastic placeholder"]')
26+
end
27+
end
28+
end

spec/filter_form_spec.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require 'spec_helper'
2+
3+
describe 'authors index', type: :feature, js: true do
4+
before do
5+
add_author_resource
6+
end
7+
8+
context 'index filters' do
9+
before do
10+
visit '/admin/authors'
11+
end
12+
13+
context 'filter by Date column' do
14+
before do
15+
page.find('input#q_birthday_gteq').click
16+
17+
page.find('.xdsoft_datetimepicker', visible: true)
18+
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
19+
page.find('.xdsoft_datetimepicker', visible: true)
20+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
21+
22+
page.find('input#q_birthday_lteq').click
23+
24+
page.find('.xdsoft_datetimepicker', visible: true)
25+
.find('.xdsoft_calendar td.xdsoft_date[data-date="20"]').click
26+
page.find('.xdsoft_datetimepicker', visible: true)
27+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
28+
end
29+
30+
it 'can set date from/to' do
31+
date_from = Date.today.beginning_of_month.strftime("%Y-%m-%d")
32+
date_to = (Date.today.beginning_of_month + 19.days).strftime("%Y-%m-%d")
33+
34+
expect(page.find('input#q_birthday_gteq').value).to start_with(date_from)
35+
expect(page.find('input#q_birthday_lteq').value).to start_with(date_to)
36+
37+
expect(page).to have_css('input#q_birthday_gteq[placeholder="From"]')
38+
expect(page).to have_css('input#q_birthday_lteq[placeholder="To"]')
39+
40+
page.find('#sidebar input[type=submit]').click
41+
page.has_css?('h4', text: 'Current filters:')
42+
43+
# birthday(Date type) is a Date column, should not contain H:M
44+
expect(page.find('#q_birthday_gteq').value).to match(/\A\d{4}-\d{2}-\d{2}\z/)
45+
end
46+
end
47+
48+
context 'filter by DateTime/Timestamp column' do
49+
before do
50+
Author.create!(name: "Ren",
51+
last_name: "from-20-day-of-month",
52+
created_at: (Time.now.change(day: 20) - 1.hour).to_s(:db))
53+
54+
Author.create!(name: "Rey",
55+
last_name: "from-the-future",
56+
created_at: (Time.now.change(day: 20) + 2.hours).to_s(:db))
57+
58+
# chose 01 and 20 day of the current month
59+
60+
page.find('input#q_created_at_gteq_datetime_picker').click
61+
62+
page.find('.xdsoft_datetimepicker', visible: true)
63+
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
64+
page.find('.xdsoft_datetimepicker', visible: true)
65+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
66+
67+
page.find('input#q_created_at_lteq_datetime_picker').click
68+
69+
page.find('.xdsoft_datetimepicker', visible: true)
70+
.find('.xdsoft_calendar td.xdsoft_date[data-date="20"]').click
71+
page.find('.xdsoft_datetimepicker', visible: true)
72+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
73+
74+
page.find('#sidebar input[type=submit]').click
75+
page.has_css?('h4', text: 'Current filters:')
76+
end
77+
78+
it 'q_created_at_lteq_datetime send correct value to SQL' do
79+
expect(page).to have_text('from-20-day-of-month')
80+
expect(page).not_to have_text('from-the-future')
81+
end
82+
83+
it 'submit filter form' do
84+
# created_at(Timestamp type) should contain Hours:Minutes, as selected before submit
85+
expect(page.find('#q_created_at_gteq_datetime_picker').value).to match(/\A\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}\z/)
86+
end
87+
end
88+
end
89+
end

spec/filters_and_edit_form_spec.rb

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)