module Mongoid::Filterable::ClassMethods
Public Instance Methods
filter_by(attr, filter=nil)
click to toggle source
Adds attr scope
# File lib/mongoid-filterable/filterable.rb, line 34 def filter_by(attr, filter=nil) if filter scope "filter_with_#{attr}", filter elsif fields[attr.to_s].type == String scope "filter_with_#{attr}", ->(value) { where(attr => Regexp.new(value)) } else scope "filter_with_#{attr}", ->(value) { where(attr => value) } end end
filter_by_normalized(attr)
click to toggle source
Adds attr scope using normalized values (see gem mongoid-normalize-strings)
# File lib/mongoid-filterable/filterable.rb, line 48 def filter_by_normalized(attr) normalized_name = (attr.to_s + '_normalized').to_sym scope "filter_with_#{attr}", lambda { |value| where(normalized_name => Regexp.new(Regexp.escape(I18n.transliterate(value)), 'i')) } end
filtrate(filtering_params, operator='$and')
click to toggle source
Applies params scopes to current scope
# File lib/mongoid-filterable/filterable.rb, line 10 def filtrate(filtering_params, operator='$and') return all unless filtering_params results = all selectors = [] criteria = Mongoid::Criteria.new(self).unscoped filtering_params.each do |key, value| if respond_to?("filter_with_#{key}") # check the number of arguments of filter scope if value.is_a?(Array) && scopes["filter_with_#{key}".to_sym][:scope].arity > 1 selectors.push criteria.public_send("filter_with_#{key}", *value).selector else selectors.push criteria.public_send("filter_with_#{key}", value).selector end end end selectors.empty? ? results : results.where(operator => selectors) end