class RiceCooker::Filter::FilterEngine

Public Class Methods

action() click to toggle source
# File lib/rice_cooker/filter.rb, line 11
def self.action
  :filtering
end
get_future_lambda(db_field) click to toggle source
# File lib/rice_cooker/filter.rb, line 54
def self.get_future_lambda(db_field)
  lambda do |value|
    value.first == 'true' ? where("#{db_field} >= ?", Time.zone.now) : where("#{db_field} < ?", Time.zone.now)
  end
end
get_named_lambda(fi) click to toggle source
# File lib/rice_cooker/filter.rb, line 60
def self.get_named_lambda(fi)
  lambda do |value|
    value.first == 'true' ? where.not(fi => nil) : where(fi => nil)
  end
end
new(unformated_params, model) click to toggle source
Calls superclass method RiceCooker::Base::new
# File lib/rice_cooker/filter.rb, line 15
def initialize(unformated_params, model)
  super
  @allowed_keys = (filterable_fields_for(@model) + @params.keys)
end

Public Instance Methods

parse_bool(fi) click to toggle source
# File lib/rice_cooker/filter.rb, line 27
def parse_bool(fi)
  if fi.to_sym == :begin_at
    parse_future_bool fi
  else
    parse_named_bool fi
  end
end
parse_future_bool(fi) click to toggle source
# File lib/rice_cooker/filter.rb, line 35
def parse_future_bool(fi)
  @params[:future] = {
    proc: FilterEngine.get_future_lambda("#{@model.quoted_table_name}.\"#{fi}\""),
    all: %w(true false),
    description: "Return only #{@model.to_s.underscore.humanize.downcase.pluralize} which begins in the future"
  }
  @allowed_keys << :future
end
parse_named_bool(fi) click to toggle source
# File lib/rice_cooker/filter.rb, line 44
def parse_named_bool(fi)
  name = fi.to_s.gsub(/_at$/, '')
  @params[name.to_sym] = {
    proc: FilterEngine.get_named_lambda(fi),
    all: %w(true false),
    description: "Return only #{name} #{@model.to_s.underscore.humanize.downcase.pluralize}"
  }
  @allowed_keys << name
end
process(value, scope, custom, filter) click to toggle source
# File lib/rice_cooker/filter.rb, line 66
def process(value, scope, custom, filter)
  params = parse_filtering_param(value, filter)
  apply_filter_to_collection(scope, params, custom)
end
register_bools() click to toggle source
# File lib/rice_cooker/filter.rb, line 20
def register_bools
  additional = (filterable_fields_for(@model) - [:created_at, :updated_at])
               .select { |e| e =~ /_at$/ }
               .select { |e| @params[e.to_s.gsub(/_at$/, '')].nil? }
  additional.each { |fi| parse_bool(fi) }
end