Skip to content

Fix datetime H:M truncation #55

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 1 commit into from
Jun 21, 2019
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ group :test do
gem 'capybara'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
gem 'byebug'
end
11 changes: 8 additions & 3 deletions lib/active_admin_datetimepicker/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ def input_html_options(input_name = nil, placeholder = nil)
options[:class] = [self.options[:class], html_class].compact.join(' ')
options[:data] ||= input_html_data
options[:data].merge!(datepicker_options: datetime_picker_options)
options[:value] ||= input_value(input_name)
options[:value] = input_value(input_name)
options[:maxlength] = 19
options[:placeholder] = placeholder unless placeholder.nil?
end
end

def input_value(input_name = nil)
val = object.public_send(input_name || method)
return DateTime.new(val.year, val.month, val.day, val.hour, val.min, val.sec).strftime(format) if val.is_a?(Time)
val.to_s
if val.nil?
val
elsif column.type == :date
val
else
DateTime.new(val.year, val.month, val.day, val.hour, val.min, val.sec).strftime(format)
end
end

def datetime_picker_options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ def input_html_options(input_name = gt_input_name, placeholder = gt_input_placeh
end
end

def gt_input_name
"#{method}_gteq"
end

def lt_input_name
"#{method}_lteq"
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/filters_and_edit_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,28 @@

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"]')
end

it 'submit filter form' do
page.find('#q_created_at_gteq_datetime').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('#sidebar input[type=submit]').click
expect(page).to have_css('h4', text: 'Current filters:')

# should contain Hours:Minutes, as selected before submit
expect(page.find('#q_created_at_gteq_datetime').value).to match(/\A\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}\z/)

# birthday 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


Expand Down
1 change: 1 addition & 0 deletions spec/support/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def add_author_resource(options = {}, &block)
config.filters = true

filter :birthday, as: :date_time_range
filter :created_at, as: :date_time_range

form do |f|
f.semantic_errors *f.object.errors.keys
Expand Down