bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Range datepicker Filter doesn't work properly when the selected range it's the same day #262

Closed helenatxu closed 5 years ago

helenatxu commented 5 years ago

Hello again :)

I have another grid, with regular columns and filters, I have also one column like this:

 column(:date_sent,
         header: 'Sent Date',
         order: proc do |scope|
           scope.reorder('sample_shippings.date_sent')
         end) do |record|
    if record.sample_shippings.any? && !record.sample_shippings.first.date_sent.blank?
      record.sample_shippings.first.date_sent, format: :grid
    else
      '-'
    end
  end

I've discovered that when the user selects same date for first date and last date in the range datepicker filter, it doesn't work properly, it shows 0 when it should show the number of entries that match that date.

I think it's related with how Rails Activerecord works when selecting dates, it doesn't check the whole time range in a day, but only the "beginning" of the day. This is what I mean.

I use the .all_day fix to get that full time range in a day, but I'm not sure how to use that in this situation, or maybe it's a bug in the datepicker filter as it should check that date when the date range lasts only one day.

bogdan commented 5 years ago

You pasted a source code for a column, not a filter, so I suppose it is a typo.

Datagrid "knows" about beginning_of_day and end_of_day caveat: https://github.com/bogdan/datagrid/blob/master/lib/datagrid/filters/date_filter.rb#L9

However, if you pass a block into range filter, you would need to convert date to timestamp manually.

helenatxu commented 5 years ago

Sorry, it was a typo yes, I've already corrected it.

The filter has nothing special in it, it works fine but it just doesn't show any result when you select the same date for the first and the last day in the datepicker.

filter(:date_sent, :date, range: true, header: 'Date')

bogdan commented 5 years ago

Track the SQL query that is generated in the console and compare it to what you would expect.

bogdan commented 5 years ago

Closing by inactivity. Feel free to follow up.