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

62 lines
1.5 KiB
Ruby
Raw Normal View History

2011-03-30 20:31:39 -04:00
require 'ransack/constants'
require 'ransack/predicate'
module Ransack
module Configuration
mattr_accessor :predicates, :options
2011-03-30 20:31:39 -04:00
self.predicates = {}
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?
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
),
:compound => true
2011-03-30 20:31:39 -04:00
)
)
end if compounds
end
# default search_key that, it can be overridden on sort_link level
def search_key=(name)
self.options[:search_key] = name
end
# raise an error if an unknown predicate, condition or attribute is passed
# into a search
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}" }
else
2014-05-13 06:10:47 -04:00
"#{arel_predicate}#{suffix}"
end
end
2014-05-13 06:10:47 -04:00
2011-03-30 20:31:39 -04:00
end
end