Skip to content

Commit 8fd6961

Browse files
committed
Avoid type casting date to timestamp when custom filter block is used
1 parent 7d69f43 commit 8fd6961

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [2.0.8]
44

5+
* Prevent typecasting of date into timestamp when `:date` filter has a block
56
* Rescue StatementInvalid when guessing column type from a query
67

78
## [2.0.7]

lib/datagrid/filters/date_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def default_input_options
1212
end
1313

1414
def apply(grid_object, scope, value)
15-
if !dummy? && grid_object.driver.timestamp_column?(scope, name)
15+
if !dummy? && !block && grid_object.driver.timestamp_column?(scope, name)
1616
value = Datagrid::Utils.format_date_as_timestamp(value)
1717
end
1818
super

spec/datagrid/filters/date_filter_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def not_include_entry(date)
187187
time = Time.utc(2018, 0o1, 0o7, 2, 2)
188188

189189
report = test_grid_filter(:created_at, :date, range: true) do |value|
190-
where(created_at: value)
190+
where(created_at: value.begin&.beginning_of_day..value.end&.end_of_day)
191191
end
192192
report.created_at = date
193193

@@ -287,4 +287,18 @@ class ChildGrid < ParentGrid
287287
test_grid_filter(:created_at, :date, range: true, default: 3.day.ago..Time.now).created_at,
288288
).to eq(3.days.ago.to_date..Date.today)
289289
end
290+
291+
it "doesn't convert to timestamp if block is given" do
292+
grid = test_grid_filter(:created_at, :date) do |value, scope|
293+
scope.where(created_at: (value - 5.minutes)..(value + 5.minutes))
294+
end
295+
296+
time = Time.now
297+
grid.created_at = time
298+
e1 = Entry.create!(created_at: time.beginning_of_day)
299+
e2 = Entry.create!(created_at: time)
300+
301+
expect(grid.assets).to include(e1)
302+
expect(grid.assets).to_not include(e2)
303+
end
290304
end

0 commit comments

Comments
 (0)