Class: SimpleFilter::Base
- Inherits:
-
Object
- Object
- SimpleFilter::Base
- Defined in:
- lib/simple_filter/base.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
The params are the parameters you might have entered in your form.
Class Method Summary collapse
- .all(*args) ⇒ Object
-
.config(options = {}) ⇒ Object
possible options: per_page 10 order_by :id.
-
.filter(*filters) ⇒ Object
Call filter to define which filters are available.
- .inherited(klass) ⇒ Object
- .scope(model_name) ⇒ Object
Instance Method Summary collapse
-
#all ⇒ Object
Get all filtered results.
-
#initialize(params) ⇒ Base
constructor
A new instance of Base.
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
#params ⇒ Object (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( = {}) configuration.per_page = [:per_page] unless [:per_page].blank? configuration.order_by = [:order_by] unless [: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
#all ⇒ Object
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 |