Skip to content

Fix wrong value for lteq_datetime predicate #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Gemfile.lock
coverage/
spec/rails/rails-5.2.1/
spec/rails/rails-*
pkg
.bundle
vendor/bundl
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ group :test do
gem 'rails', "~> #{ENV['RAILS'] || default_rails_version}"
gem 'activeadmin', "~> #{ENV['AA'] || default_activeadmin_version}"

gem 'sprockets-rails', '3.0.4'
gem 'rspec-rails'
gem 'coveralls', require: false # Test coverage website. Go to https://coveralls.io
gem 'sass-rails'
Expand Down
8 changes: 8 additions & 0 deletions config/initializers/active_admin_datetimepicker.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ActiveAdmin.setup do |config|
config.register_stylesheet 'jquery.xdan.datetimepicker.css'
end

Ransack.configure do |config|
config.add_predicate 'gteq_datetime_picker',
arel_predicate: 'gteq'

config.add_predicate 'lteq_datetime_picker',
arel_predicate: 'lt'
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeh
end
end

def gt_input_name
column && column.type == :date ? super : "#{method}_gteq_datetime_picker"
end

def lt_input_name
column && column.type == :date ? super : "#{method}_lteq_datetime_picker"
end

end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/edit_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe 'authors index', type: :feature, js: true do
before do
add_author_resource
end

context 'edit form' do
before do
visit '/admin/authors/new'
end

before do
page.find('#author_birthday').click

page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
end

it 'can set birthday' do
date_birthday = Date.today.beginning_of_month.strftime("%Y-%m-%d")
expect(page.find('#author_birthday').value).to start_with(date_birthday)
expect(page).to have_css('#author_birthday[placeholder="Formtastic placeholder"]')
end
end
end
89 changes: 89 additions & 0 deletions spec/filter_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'spec_helper'

describe 'authors index', type: :feature, js: true do
before do
add_author_resource
end

context 'index filters' do
before do
visit '/admin/authors'
end

context 'filter by Date column' do
before do
page.find('input#q_birthday_gteq').click

page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click

page.find('input#q_birthday_lteq').click

page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_calendar td.xdsoft_date[data-date="20"]').click
page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
end

it 'can set date from/to' do
date_from = Date.today.beginning_of_month.strftime("%Y-%m-%d")
date_to = (Date.today.beginning_of_month + 19.days).strftime("%Y-%m-%d")

expect(page.find('input#q_birthday_gteq').value).to start_with(date_from)
expect(page.find('input#q_birthday_lteq').value).to start_with(date_to)

expect(page).to have_css('input#q_birthday_gteq[placeholder="From"]')
expect(page).to have_css('input#q_birthday_lteq[placeholder="To"]')

page.find('#sidebar input[type=submit]').click
page.has_css?('h4', text: 'Current filters:')

# birthday(Date type) is a Date column, should not contain H:M
expect(page.find('#q_birthday_gteq').value).to match(/\A\d{4}-\d{2}-\d{2}\z/)
end
end

context 'filter by DateTime/Timestamp column' do
before do
Author.create!(name: "Ren",
last_name: "from-20-day-of-month",
created_at: (Time.now.change(day: 20) - 1.hour).to_s(:db))

Author.create!(name: "Rey",
last_name: "from-the-future",
created_at: (Time.now.change(day: 20) + 2.hours).to_s(:db))

# chose 01 and 20 day of the current month

page.find('input#q_created_at_gteq_datetime_picker').click

page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click

page.find('input#q_created_at_lteq_datetime_picker').click

page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_calendar td.xdsoft_date[data-date="20"]').click
page.find('.xdsoft_datetimepicker', visible: true)
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click

page.find('#sidebar input[type=submit]').click
page.has_css?('h4', text: 'Current filters:')
end

it 'q_created_at_lteq_datetime send correct value to SQL' do
expect(page).to have_text('from-20-day-of-month')
expect(page).not_to have_text('from-the-future')
end

it 'submit filter form' do
# created_at(Timestamp type) should contain Hours:Minutes, as selected before submit
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/)
end
end
end
end
87 changes: 0 additions & 87 deletions spec/filters_and_edit_form_spec.rb

This file was deleted.