Skip to content

Commit 092de0d

Browse files
authored
Merge pull request #75 from Skumring/Fix-parsing-datetime-input-values
Fix parsing datetime input values
2 parents 1b6c81c + 715e365 commit 092de0d

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

lib/active_admin_datetimepicker/base.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def input_html_options(input_name = nil, placeholder = nil)
2626

2727
def input_value(input_name = nil)
2828
val = object.public_send(input_name || method)
29-
if val.nil?
30-
val
31-
elsif column.type == :date
32-
val
33-
else
34-
DateTime.new(val.year, val.month, val.day, val.hour, val.min, val.sec).strftime(format)
35-
end
29+
val.is_a?(Date) ? val : parse_datetime(val)
30+
end
31+
32+
def parse_datetime(val)
33+
DateTime.parse(val.to_s).strftime(format)
34+
rescue ArgumentError
35+
nil
3636
end
3737

3838
def datetime_picker_options

spec/filter_form_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,45 @@
9292
expect(page).to have_css('#q_created_at_lteq_datetime_picker[placeholder=To]')
9393
end
9494
end
95+
96+
context 'filter by virtual attribute last_seen_at - without column&type properties (search by updated_at)' do
97+
let!(:first_author) { Author.create!(name: 'Ren', last_name: 'current', updated_at: Time.now.to_s(:db)) }
98+
let!(:second_author) { Author.create!(name: 'Rey', last_name: 'future', updated_at: 21.days.from_now.to_s(:db)) }
99+
100+
before do
101+
# chose 01 and 20 day of the current month
102+
page.find('input#q_last_seen_at_gteq_datetime_picker').click
103+
page.find('.xdsoft_datetimepicker', visible: true)
104+
.find('.xdsoft_calendar td.xdsoft_date[data-date="1"]').click
105+
page.find('.xdsoft_datetimepicker', visible: true)
106+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
107+
108+
page.find('input#q_last_seen_at_lteq_datetime_picker').click
109+
page.find('.xdsoft_datetimepicker', visible: true)
110+
.find('.xdsoft_calendar td.xdsoft_date[data-date="20"]').click
111+
page.find('.xdsoft_datetimepicker', visible: true)
112+
.find('.xdsoft_timepicker.active .xdsoft_time.xdsoft_current').click
113+
114+
@value_from = page.find('#q_last_seen_at_gteq_datetime_picker').value
115+
@value_to = page.find('#q_last_seen_at_lteq_datetime_picker').value
116+
117+
page.find('#sidebar input[type=submit]').click
118+
page.has_css?('h4', text: 'Current filters:')
119+
end
120+
121+
it 'should filter records properly' do
122+
expect(page).to have_text(first_author.name)
123+
expect(page).not_to have_text(second_author.name)
124+
end
125+
126+
it 'input#value and placeholder is the same as before form submit' do
127+
# last_seen_at (without typecasting just a string) should contain Hours:Minutes, as selected before submit
128+
expect(page.find('#q_last_seen_at_gteq_datetime_picker').value).to match(@value_from)
129+
expect(page.find('#q_last_seen_at_lteq_datetime_picker').value).to match(@value_to)
130+
131+
expect(page).to have_css('#q_last_seen_at_gteq_datetime_picker[placeholder=From]')
132+
expect(page).to have_css('#q_last_seen_at_lteq_datetime_picker[placeholder=To]')
133+
end
134+
end
95135
end
96136
end

spec/support/admin.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def add_author_resource(options = {}, &block)
55

66
filter :birthday, as: :date_time_range
77
filter :created_at, as: :date_time_range
8+
filter :last_seen_at, as: :date_time_range
89

910
form do |f|
1011
f.semantic_errors *f.object.errors.keys

spec/support/rails_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
generate :model, 'post title:string:uniq body:text author:references'
55

66
#Add validation
7-
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n", after: "Base\n"
7+
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n\n attr_accessor :last_seen_at\n ransacker :last_seen_at do\n Arel.sql('updated_at')\n end\n", after: "ApplicationRecord\n"
88
inject_into_file "app/models/post.rb", " validates_presence_of :author\n", after: ":author\n"
99

1010
# Configure default_url_options in test environment

0 commit comments

Comments
 (0)