Class: SimpleFilter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_filter/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



29
30
31
# File 'lib/simple_filter/base.rb', line 29

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

The params are the parameters you might have entered in your form.



6
7
8
# File 'lib/simple_filter/base.rb', line 6

def params
  @params
end

Class Method Details

.all(*args) ⇒ Object



25
26
27
# File 'lib/simple_filter/base.rb', line 25

def self.all(*args)
  new(*args).all
end

.config(options = {}) ⇒ Object

possible options:

per_page 10
order_by :id


20
21
22
23
# File 'lib/simple_filter/base.rb', line 20

def self.config(options = {})
  configuration.per_page = options[:per_page] unless options[:per_page].blank?
  configuration.order_by = options[:order_by] unless options[:order_by].blank?
end

.filter(*filters) ⇒ Object

Call filter to define which filters are available. These will all be run in the order you specified



13
14
15
# File 'lib/simple_filter/base.rb', line 13

def self.filter(*filters)
  self.filters = self.filters + filters
end

.inherited(klass) ⇒ Object



82
83
84
85
# File 'lib/simple_filter/base.rb', line 82

def inherited(klass)
  klass.send(:scope, klass.to_s.sub('Filter', '').underscore)
  klass.configuration = self.configuration.dup
end

.scope(model_name) ⇒ Object



76
77
78
79
80
# File 'lib/simple_filter/base.rb', line 76

def scope(model_name)
  define_method(:scope) do
    @scope ||= model_name.to_s.camelize.constantize.scoped
  end
end

Instance Method Details

#allObject

Get all filtered results. This is the public facing method that you’d want to call when getting the results of the filter.



35
36
37
38
# File 'lib/simple_filter/base.rb', line 35

def all
  apply_filters
  scope
end