Class: Toller::Filter
- Inherits:
-
Object
- Object
- Toller::Filter
- Defined in:
- lib/toller/filter.rb
Overview
Filter
Instance Attribute Summary collapse
-
#parameter ⇒ Symbol
readonly
The public filter param name.
-
#properties ⇒ Hash
readonly
The filter's resolved options (:field, :default, :scope_name, etc.).
-
#type ⇒ Symbol
readonly
The filter type, e.g.
Instance Method Summary collapse
-
#apply!(collection, value) ⇒ ActiveRecord::Relation
Applies this filter to
collection, dispatching to the scope or where handler based ontype. -
#default ⇒ Boolean
Whether this filter applies automatically when no filter params were sent at all.
-
#initialize(parameter, type, options) ⇒ Toller::Filter
constructor
A new instance of Filter.
Constructor Details
#initialize(parameter, type, options) ⇒ Toller::Filter
Returns a new instance of Filter.
24 25 26 27 28 29 30 31 32 |
# File 'lib/toller/filter.rb', line 24 def initialize(parameter, type, ) @parameter = parameter @type = type @properties = .reverse_merge( field: parameter, default: false, scope_name: nil ) end |
Instance Attribute Details
#parameter ⇒ Symbol (readonly)
Returns the public filter param name.
8 9 10 |
# File 'lib/toller/filter.rb', line 8 def parameter @parameter end |
#properties ⇒ Hash (readonly)
Returns the filter's resolved options (:field, :default, :scope_name, etc.).
11 12 13 |
# File 'lib/toller/filter.rb', line 11 def properties @properties end |
#type ⇒ Symbol (readonly)
Returns the filter type, e.g. :string, :integer, :scope.
14 15 16 |
# File 'lib/toller/filter.rb', line 14 def type @type end |
Instance Method Details
#apply!(collection, value) ⇒ ActiveRecord::Relation
Applies this filter to collection, dispatching to the scope or
where handler based on type.
41 42 43 44 45 46 47 |
# File 'lib/toller/filter.rb', line 41 def apply!(collection, value) if type == :scope Filters::ScopeHandler.new.call(collection, value, properties) else Filters::WhereHandler.new.call(collection, type, value, properties) end end |
#default ⇒ Boolean
Returns whether this filter applies automatically when no filter params were sent at all.
51 52 53 |
# File 'lib/toller/filter.rb', line 51 def default properties[:default] end |