2011-03-31 00:31:39 +00:00
|
|
|
require 'ransack/constants'
|
|
|
|
require 'ransack/predicate'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Configuration
|
|
|
|
|
2012-03-07 19:31:13 +00:00
|
|
|
mattr_accessor :predicates, :options
|
2011-03-31 00:31:39 +00:00
|
|
|
self.predicates = {}
|
2012-03-07 19:31:13 +00:00
|
|
|
self.options = {
|
2014-05-01 13:55:39 +00:00
|
|
|
:search_key => :q
|
2012-03-07 19:31:13 +00:00
|
|
|
}
|
2011-03-31 00:31:39 +00:00
|
|
|
|
|
|
|
def configure
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_predicate(name, opts = {})
|
|
|
|
name = name.to_s
|
|
|
|
opts[:name] = name
|
|
|
|
compounds = opts.delete(:compounds)
|
|
|
|
compounds = true if compounds.nil?
|
2012-05-02 06:54:12 +00:00
|
|
|
compounds = false if opts[:wants_array]
|
2011-03-31 00:31:39 +00:00
|
|
|
|
|
|
|
self.predicates[name] = Predicate.new(opts)
|
|
|
|
|
|
|
|
['_any', '_all'].each do |suffix|
|
|
|
|
self.predicates[name + suffix] = Predicate.new(
|
|
|
|
opts.merge(
|
2014-05-01 13:55:39 +00:00
|
|
|
:name => name + suffix,
|
2014-05-12 14:53:37 +00:00
|
|
|
:arel_predicate => arel_predicate_with_suffix(opts[:arel_predicate], suffix),
|
2014-05-01 13:55:39 +00:00
|
|
|
:compound => true
|
2011-03-31 00:31:39 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end if compounds
|
|
|
|
end
|
|
|
|
|
2012-03-07 19:31:13 +00:00
|
|
|
# default search_key that, it can be overridden on sort_link level
|
2012-04-11 18:36:16 +00:00
|
|
|
def search_key=(name)
|
2012-03-07 19:31:13 +00:00
|
|
|
self.options[:search_key] = name
|
|
|
|
end
|
|
|
|
|
2014-05-12 14:53:37 +00:00
|
|
|
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
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
2012-04-11 18:36:16 +00:00
|
|
|
end
|