From c16ab7a4b32fd2be697fab9a7f51fa0d22fc3492 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 4 Jan 2015 13:28:10 +0100 Subject: [PATCH] Follow up to #486: Extract to private methods, document the reason behind this patch and add a FIX ME comment to encourage PRs to improve it. --- .../active_record/ransack/nodes/condition.rb | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb index df5c2ac3e..c109e108f 100644 --- a/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +++ b/lib/ransack/adapters/active_record/ransack/nodes/condition.rb @@ -18,11 +18,30 @@ def arel_predicate predicates.inject(&:or) end else - predicates.first.right[0] = predicates.first.right[0].val if defined?(Arel::Nodes::Casted) && predicates.first.class == Arel::Nodes::In && predicates.first.right.is_a?(Array) && predicates.first.right[0].class == Arel::Nodes::Casted - predicates.first + return_predicate(predicates.first) end end - end # Condition + private + + # FIXME: Improve this edge case patch for Arel >= 6.0 (Rails >= 4.2) + # that adds several conditionals to handle changing Arel API. + # Related to Ransack github issue #472 and pull request #486. + # + def return_predicate(predicate) + if casted_array_with_in_predicate?(predicate) + predicate.right[0] = predicate.right[0].val + end + predicate + end + # + def casted_array_with_in_predicate?(predicate) + return unless defined?(Arel::Nodes::Casted) + predicate.class == Arel::Nodes::In && + predicate.right.is_a?(Array) && + predicate.right[0].class == Arel::Nodes::Casted + end + + end end end