ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Date predicate is formatted as string

Open dcrec1 opened this issue 2 months ago • 3 comments

Hello,

in previous versions, block in this code use to receive a time object:

Ransack.configure do |config|
  config.add_predicate 'lteq_eod',
                       arel_predicate: 'lteq',
                       formatter: proc { |value| value.end_of_day },
                       type: :date
end

After upgrading from 4.3.0 to 4.4.0, the proc block started receiving an string.

dcrec1 avatar Sep 30 '25 07:09 dcrec1

@dcrec1 are you able to test this PR ? https://github.com/activerecord-hackery/ransack/pull/1651

scarroll32 avatar Oct 04 '25 22:10 scarroll32

Hi @scarroll32, @dcrec1

I’m also encountering the same error. I believe the cause is that it no longer casts the value when it’s not an array. This change was introduced in PR #1559. I’ve confirmed that the issue can be avoided with the following monkey patch:

Ransack::Nodes::Condition.prepend(
  Module.new do
    def casted_values_for_attribute(attr)
      validated_values.map { |v| v.cast_array(predicate.type || attr.type) }
    end
  end
)
Ransack::Nodes::Value.prepend(
  Module.new do
    def cast_array(type)
      if value.is_a?(Array)
        cast_to_date(value)
      else
        cast(type)
      end
    end
  end
)

yujideveloper avatar Nov 18 '25 11:11 yujideveloper

PR #1559 was reverted in v4.4.1 via PR #1645. So, I believe this issue was resolved in v4.4.1.

yujideveloper avatar Nov 19 '25 00:11 yujideveloper