diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 331b044..a8a2d37 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -6,9 +6,7 @@ module Ransack mattr_accessor :predicates, :options self.predicates = {} - self.options = { - :search_key => :q - } + self.options = { :search_key => :q } def configure yield self @@ -24,10 +22,13 @@ module Ransack self.predicates[name] = Predicate.new(opts) ['_any', '_all'].each do |suffix| - self.predicates[name + suffix] = Predicate.new( + compound_name = name + suffix + self.predicates[compound_name] = Predicate.new( opts.merge( - :name => name + suffix, - :arel_predicate => arel_predicate_with_suffix(opts[:arel_predicate], suffix), + :name => compound_name, + :arel_predicate => arel_predicate_with_suffix( + opts[:arel_predicate], suffix + ), :compound => true ) ) @@ -39,13 +40,13 @@ module Ransack self.options[:search_key] = name end - def arel_predicate_with_suffix arel_predicate, suffix - case arel_predicate - when Proc - proc { |v| arel_predicate.call(v).to_s + suffix } + def arel_predicate_with_suffix(arel_predicate, suffix) + if arel_predicate === Proc + proc { |v| "#{arel_predicate.call(v)}#{suffix}" } else - arel_predicate.to_s + suffix + "#{arel_predicate}#{suffix}" end end + end end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 84fd29a..f798f3b 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -20,10 +20,12 @@ module Ransack :v => predicate.wants_array ? Array(values) : [values] ) # TODO: Figure out what to do with multiple types of attributes, - # if anything. - # Tempted to go with "garbage in, garbage out" on this one - predicate.validate(condition.values, condition.default_type) ? - condition : nil + # if anything. Tempted to go with "garbage in, garbage out" here. + if predicate.validate(condition.values, condition.default_type) + condition + else + nil + end end end @@ -169,8 +171,9 @@ module Ransack def arel_predicate predicates = attributes.map do |attr| attr.attr.send( - arel_predicate_for_attribute(attr), formatted_values_for_attribute(attr) - ) + arel_predicate_for_attribute(attr), + formatted_values_for_attribute(attr) + ) end if predicates.size > 1 @@ -203,11 +206,12 @@ module Ransack predicate.wants_array ? formatted : formatted.first end - def arel_predicate_for_attribute attr - case predicate.arel_predicate - when Proc + def arel_predicate_for_attribute(attr) + if predicate.arel_predicate === Proc values = casted_values_for_attribute(attr) - predicate.arel_predicate.call(predicate.wants_array ? values : values.first) + predicate.arel_predicate.call( + predicate.wants_array ? values : values.first + ) else predicate.arel_predicate end