1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

arel_predicate can be a proc...

...accepting typecasted, but yet unformatted, value
This commit is contained in:
Artem Baguinski 2014-05-12 16:53:37 +02:00
parent 993ffb1917
commit 99840fcc28
2 changed files with 21 additions and 3 deletions

View file

@ -20,7 +20,6 @@ module Ransack
compounds = opts.delete(:compounds)
compounds = true if compounds.nil?
compounds = false if opts[:wants_array]
opts[:arel_predicate] = opts[:arel_predicate].to_s
self.predicates[name] = Predicate.new(opts)
@ -28,7 +27,7 @@ module Ransack
self.predicates[name + suffix] = Predicate.new(
opts.merge(
:name => name + suffix,
:arel_predicate => opts[:arel_predicate] + suffix,
:arel_predicate => arel_predicate_with_suffix(opts[:arel_predicate], suffix),
:compound => true
)
)
@ -40,5 +39,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 }
else
arel_predicate.to_s + suffix
end
end
end
end

View file

@ -169,7 +169,7 @@ module Ransack
def arel_predicate
predicates = attributes.map do |attr|
attr.attr.send(
predicate.arel_predicate, formatted_values_for_attribute(attr)
arel_predicate_for_attribute(attr), formatted_values_for_attribute(attr)
)
end
@ -203,6 +203,17 @@ module Ransack
predicate.wants_array ? formatted : formatted.first
end
def arel_predicate_for_attribute attr
case predicate.arel_predicate
when Proc
values = casted_values_for_attribute(attr)
predicate.arel_predicate.call(predicate.wants_array ? values : values.first)
else
predicate.arel_predicate
end
end
def default_type
predicate.type || (attributes.first && attributes.first.type)
end