Skip to content
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
14 changes: 7 additions & 7 deletions lib/active_admin_datetimepicker/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def input_html_options(input_name = nil, placeholder = nil)

def input_value(input_name = nil)
val = object.public_send(input_name || method)
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
val.is_a?(Date) ? val : parse_datetime(val)
end

def parse_datetime(val)
DateTime.parse(val.to_s).strftime(format)
rescue ArgumentError
nil
end

def datetime_picker_options
Expand Down
40 changes: 40 additions & 0 deletions spec/filter_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,45 @@
expect(page).to have_css('#q_created_at_lteq_datetime_picker[placeholder=To]')
end
end

context 'filter by virtual attribute last_seen_at - without column&type properties (search by updated_at)' do
let!(:first_author) { Author.create!(name: 'Ren', last_name: 'current', updated_at: Time.now.to_s(:db)) }
let!(:second_author) { Author.create!(name: 'Rey', last_name: 'future', updated_at: 21.days.from_now.to_s(:db)) }

before do
# chose 01 and 20 day of the current month
page.find('input#q_last_seen_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_last_seen_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

@value_from = page.find('#q_last_seen_at_gteq_datetime_picker').value
@value_to = page.find('#q_last_seen_at_lteq_datetime_picker').value

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

it 'should filter records properly' do
expect(page).to have_text(first_author.name)
expect(page).not_to have_text(second_author.name)
end

it 'input#value and placeholder is the same as before form submit' do
# last_seen_at (without typecasting just a string) should contain Hours:Minutes, as selected before submit
expect(page.find('#q_last_seen_at_gteq_datetime_picker').value).to match(@value_from)
expect(page.find('#q_last_seen_at_lteq_datetime_picker').value).to match(@value_to)

expect(page).to have_css('#q_last_seen_at_gteq_datetime_picker[placeholder=From]')
expect(page).to have_css('#q_last_seen_at_lteq_datetime_picker[placeholder=To]')
end
end
end
end
1 change: 1 addition & 0 deletions spec/support/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def add_author_resource(options = {}, &block)

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

form do |f|
f.semantic_errors *f.object.errors.keys
Expand Down
2 changes: 1 addition & 1 deletion spec/support/rails_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
generate :model, 'post title:string:uniq body:text author:references'

#Add validation
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n", after: "Base\n"
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"
inject_into_file "app/models/post.rb", " validates_presence_of :author\n", after: ":author\n"

# Configure default_url_options in test environment
Expand Down