Class: Filterer::Base
- Inherits:
-
Object
- Object
- Filterer::Base
- Defined in:
- lib/filterer/base.rb
Instance Attribute Summary collapse
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#params ⇒ Object
Returns the value of attribute params.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sort ⇒ String
The key for the applied sort option.
Class Method Summary collapse
- .chain(params = {}, opts = {}) ⇒ ActiveRecord::Association
-
.filter(params = {}, opts = {}) ⇒ ActiveRecord::Association
Public API.
- .filter_without_pagination(params = {}, opts = {}) ⇒ ActiveRecord::Association
-
.sort_option(key, string_or_proc = nil, opts = {}) ⇒ Object
Macro for adding sort options.
Instance Method Summary collapse
- #defaults ⇒ Object
- #direction ⇒ Object
-
#initialize(params = {}, opts = {}) ⇒ Base
constructor
A new instance of Base.
- #starting_query ⇒ Object
Constructor Details
#initialize(params = {}, opts = {}) ⇒ Base
Returns a new instance of Base.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/filterer/base.rb', line 73 def initialize(params = {}, opts = {}) self.params = defaults.merge(params).with_indifferent_access self.opts = opts self.results = opts[:starting_query] || starting_query self.results = apply_default_filters || results add_params_to_query order_results unless opts[:skip_ordering] paginate_results unless opts[:skip_pagination] extend_active_record_relation end |
Instance Attribute Details
#meta ⇒ Object
Returns the value of attribute meta.
3 4 5 |
# File 'lib/filterer/base.rb', line 3 def @meta end |
#opts ⇒ Object
Returns the value of attribute opts.
3 4 5 |
# File 'lib/filterer/base.rb', line 3 def opts @opts end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/filterer/base.rb', line 3 def params @params end |
#results ⇒ Object
Returns the value of attribute results.
3 4 5 |
# File 'lib/filterer/base.rb', line 3 def results @results end |
#sort ⇒ String
Returns the key for the applied sort option.
97 98 99 |
# File 'lib/filterer/base.rb', line 97 def sort @sort end |
Class Method Details
.chain(params = {}, opts = {}) ⇒ ActiveRecord::Association
65 66 67 68 69 70 |
# File 'lib/filterer/base.rb', line 65 def chain(params = {}, opts = {}) new(params, opts.merge( skip_ordering: true, skip_pagination: true )).results end |
.filter(params = {}, opts = {}) ⇒ ActiveRecord::Association
Public API
53 54 55 |
# File 'lib/filterer/base.rb', line 53 def filter(params = {}, opts = {}) new(params, opts).results end |
.filter_without_pagination(params = {}, opts = {}) ⇒ ActiveRecord::Association
58 59 60 61 62 |
# File 'lib/filterer/base.rb', line 58 def filter_without_pagination(params = {}, opts = {}) new(params, opts.merge( skip_pagination: true )).results end |
.sort_option(key, string_or_proc = nil, opts = {}) ⇒ Object
Macro for adding sort options
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/filterer/base.rb', line 23 def sort_option(key, string_or_proc = nil, opts = {}) if string_or_proc.is_a?(Hash) opts, string_or_proc = string_or_proc.clone, nil end if !string_or_proc if key.is_a?(String) string_or_proc = key else raise 'Please provide a query string or a proc.' end end if key.is_a?(Regexp) && opts[:default] raise "Default sort option can't have a Regexp key." end if string_or_proc.is_a?(Proc) && opts[:tiebreaker] raise "Tiebreaker can't be a proc." end self. += [{ key: key, string_or_proc: string_or_proc, opts: opts }] end |
Instance Method Details
#defaults ⇒ Object
84 85 86 |
# File 'lib/filterer/base.rb', line 84 def defaults {} end |
#direction ⇒ Object
92 93 94 |
# File 'lib/filterer/base.rb', line 92 def direction params[:direction].try(:downcase) == 'desc' ? 'desc' : 'asc' end |
#starting_query ⇒ Object
88 89 90 |
# File 'lib/filterer/base.rb', line 88 def starting_query raise 'You must override this method!' end |