lib/ransack/configuration.rb in ransack-1.2.3 vs lib/ransack/configuration.rb in ransack-1.3.0

- old
+ new

@@ -5,11 +5,12 @@ module Configuration mattr_accessor :predicates, :options self.predicates = {} self.options = { - search_key: :q + :search_key => :q, + :ignore_unknown_conditions => true } def configure yield self end @@ -18,27 +19,43 @@ name = name.to_s opts[:name] = name 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) ['_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: opts[:arel_predicate] + suffix, - compound: true + :name => compound_name, + :arel_predicate => arel_predicate_with_suffix( + opts[:arel_predicate], suffix + ), + :compound => true ) ) 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 + + def arel_predicate_with_suffix(arel_predicate, suffix) + if arel_predicate === Proc + proc { |v| "#{arel_predicate.call(v)}#{suffix}" } + else + "#{arel_predicate}#{suffix}" + end end end end