activeadmin: Date range filter doesn't cover the last day

refs #2374 and activerecord-hackery/ransack#458

date filter

With this UI we create a request like ?q[date_lteq]=2014-10-29, put this queries the db with appointments.date <= ‘2014-10-29 00:00:00’, but it should be appointments.date <= '2014-10-29 23:59:59', otherwise it not cover the last date of the range.

We could solve this via a hidden field for date_lteq witch adds the 23:59:59 via JS, like we do it with the predict select.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 25 (20 by maintainers)

Most upvoted comments

@chrp - I like your approach - my previous workaround for this stopped working when I started using master. However, your formatter caused problems for me considering timezones. I ended up using this predicate instead, and it seems to do what I want:

config.add_predicate 'lteqdate',
  arel_predicate: 'lt',
  formatter: -> (v) { v + 1.day }

@timoschilling - here’s our temporary solution for your problem:

Change date_range filter to use a custom ransack predicate:

module ActiveAdmin
  module Inputs
    module Filters
      class DateRangeInput < ::Formtastic::Inputs::StringInput
        def lt_input_name
          "#{method}_lteqdate"
        end
      end
    end
  end
end

And define that predicate in an intializer or next to that monkey patch:

Ransack.configure do |config|
  config.add_predicate 'lteqdate',
    arel_predicate: 'lteq',
    formatter: -> (v) { v.strftime '%Y-%m-%d 23:59:59.9999' }
end

So lteqdate provides a more intuitive lteq for dates.