2011-03-30 20:31:39 -04:00
|
|
|
require 'ransack/constants'
|
|
|
|
require 'ransack/predicate'
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Configuration
|
|
|
|
|
2012-03-07 14:31:13 -05:00
|
|
|
mattr_accessor :predicates, :options
|
2011-03-30 20:31:39 -04:00
|
|
|
self.predicates = {}
|
2014-05-14 07:20:03 -04:00
|
|
|
self.options = {
|
|
|
|
:search_key => :q,
|
|
|
|
:ignore_unknown_conditions => true
|
|
|
|
}
|
2011-03-30 20:31:39 -04: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 02:54:12 -04:00
|
|
|
compounds = false if opts[:wants_array]
|
2011-03-30 20:31:39 -04:00
|
|
|
|
|
|
|
self.predicates[name] = Predicate.new(opts)
|
|
|
|
|
2014-11-21 14:03:20 -05:00
|
|
|
Constants::SUFFIXES.each do |suffix|
|
2014-05-13 06:10:47 -04:00
|
|
|
compound_name = name + suffix
|
|
|
|
self.predicates[compound_name] = Predicate.new(
|
2011-03-30 20:31:39 -04:00
|
|
|
opts.merge(
|
2014-05-13 06:10:47 -04:00
|
|
|
:name => compound_name,
|
|
|
|
:arel_predicate => arel_predicate_with_suffix(
|
|
|
|
opts[:arel_predicate], suffix
|
|
|
|
),
|
2014-05-01 09:55:39 -04:00
|
|
|
:compound => true
|
2011-03-30 20:31:39 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end if compounds
|
|
|
|
end
|
|
|
|
|
2012-03-07 14:31:13 -05:00
|
|
|
# default search_key that, it can be overridden on sort_link level
|
2012-04-11 14:36:16 -04:00
|
|
|
def search_key=(name)
|
2012-03-07 14:31:13 -05:00
|
|
|
self.options[:search_key] = name
|
|
|
|
end
|
|
|
|
|
2014-05-15 18:23:06 -04:00
|
|
|
# raise an error if an unknown predicate, condition or attribute is passed
|
|
|
|
# into a search
|
2014-05-14 07:20:03 -04:00
|
|
|
def ignore_unknown_conditions=(boolean)
|
|
|
|
self.options[:ignore_unknown_conditions] = boolean
|
|
|
|
end
|
|
|
|
|
2014-05-13 06:10:47 -04:00
|
|
|
def arel_predicate_with_suffix(arel_predicate, suffix)
|
|
|
|
if arel_predicate === Proc
|
|
|
|
proc { |v| "#{arel_predicate.call(v)}#{suffix}" }
|
2014-05-12 10:53:37 -04:00
|
|
|
else
|
2014-05-13 06:10:47 -04:00
|
|
|
"#{arel_predicate}#{suffix}"
|
2014-05-12 10:53:37 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-13 06:10:47 -04:00
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2012-04-11 14:36:16 -04:00
|
|
|
end
|