Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ coverage/*
vendor/bundle
.idea

SOLUTION.md
3 changes: 3 additions & 0 deletions lib/ransack/nodes/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def hash
end

def cast(type)
# Convert type to symbol if it's a string to ensure proper case matching
type = type.to_sym if type.is_a?(String)

case type
when :date
cast_to_date(value)
Expand Down
8 changes: 8 additions & 0 deletions spec/ransack/nodes/value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module Nodes
expect(result).to eq(Date.parse(raw_value))
end
end

# Test that string type also works (for robustness)
it "should cast 'date' string type correctly" do
result = subject.cast('date')

expect(result).to be_a_kind_of(Date)
expect(result).to eq(Date.parse(raw_value))
end
end

context "with a timestamp value" do
Expand Down
21 changes: 21 additions & 0 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,27 @@ module Ransack
expect(@s.result.to_sql).to match /#{field} NOT IN \('a', 'b'\)/
end
end

describe "with date type and formatter" do
before do
Ransack.configure do |c|
c.add_predicate 'lteq_eod',
arel_predicate: 'lteq',
formatter: proc { |value|
# This should receive a Date object, not a String
raise "Expected Date, got #{value.class}" unless value.is_a?(Date)
value.end_of_day
},
type: :date
end
end

it 'passes a Date object to the formatter, not a String' do
@s.life_start_lteq_eod = '2022-05-23'
# If the formatter receives a Date object, it won't raise an error
expect { @s.result.to_sql }.not_to raise_error
end
end
end

private
Expand Down
Loading