diff --git a/.gitignore b/.gitignore index 10c6c4905..4cb9810f9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ coverage/* vendor/bundle .idea +SOLUTION.md diff --git a/lib/ransack/nodes/value.rb b/lib/ransack/nodes/value.rb index 6313defb7..669d55c26 100644 --- a/lib/ransack/nodes/value.rb +++ b/lib/ransack/nodes/value.rb @@ -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) diff --git a/spec/ransack/nodes/value_spec.rb b/spec/ransack/nodes/value_spec.rb index 1b5b7b251..99524301e 100644 --- a/spec/ransack/nodes/value_spec.rb +++ b/spec/ransack/nodes/value_spec.rb @@ -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 diff --git a/spec/ransack/predicate_spec.rb b/spec/ransack/predicate_spec.rb index cf84e200d..4ae0e5d32 100644 --- a/spec/ransack/predicate_spec.rb +++ b/spec/ransack/predicate_spec.rb @@ -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