Description
We encountered a problem where the date time range filter would show up with both fields having the placeholder "From". After submission the the lteq field would be set to the gteq field.
Here's the filter. start
is a datetime
column.
filter :start,
as: :date_time_range,
label: "Date range"
After submission, To is set to From, but the original inputs were understood.
Upon investigation we noticed the IDs are q_field_gteq
and q_field_lteq
for a Date rather than q_field_gteq_datetimepicker
and q_field_lteq_datetimepicker
for a DateTime.
I believe this is because ActiveAdmin::Inputs::Filters::DateTimeRangeInput inherits ActiveAdmin::Inputs::Filters::DateRangeInput#input_html_options which casts the value to a Date. This was added a few years ago.
This results in DateTimeRangeInput#gt_input_name
and DateTimeRangeInput#lt_input_name
both using the name from their superclass DateRangeInput.
The following monkey patch fixes the issue for us.
class ActiveAdmin::Inputs::Filters::DateTimeRangeInput
def input_html_options_for(input_name, placeholder)
current_value = begin
#cast value to Time object before rendering input
@object.public_send(input_name).to_s.to_datetime
rescue
nil
end
return input_html_options.merge(
placeholder: placeholder,
value: current_value&.strftime("%Y-%m-%d %H:%M:%S")
)
end
end
Here's our relevant Gemfile.lock.
active_admin_datetimepicker (0.7.3)
activeadmin (>= 1.1, < 3.a)
coffee-rails
xdan-datetimepicker-rails (~> 2.5.4)
active_admin_scoped_collection_actions (0.4.0)
activeadmin (>= 1.1, < 3.a)
activeadmin (2.6.0)
arbre (~> 1.2, >= 1.2.1)
formtastic (~> 3.1)
formtastic_i18n (~> 0.4)
inherited_resources (~> 1.7)
jquery-rails (~> 4.2)
kaminari (~> 1.0, >= 1.0.1)
railties (>= 5.2, < 6.1)
ransack (~> 2.1, >= 2.1.1)
sassc-rails (~> 2.1)
sprockets (>= 3.0, < 4.1)
activeadmin-ajax_filter (0.4.4)
activeadmin (>= 1.0)
coffee-rails (>= 4.1.0)
has_scope (>= 0.6.0)
rails (>= 4.2.11)
selectize-rails (>= 0.12.6)