Class: Filterer::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#metaObject

Returns the value of attribute meta.



3
4
5
# File 'lib/filterer/base.rb', line 3

def meta
  @meta
end

#optsObject

Returns the value of attribute opts.



3
4
5
# File 'lib/filterer/base.rb', line 3

def opts
  @opts
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/filterer/base.rb', line 3

def params
  @params
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/filterer/base.rb', line 3

def results
  @results
end

#sortString

Returns the key for the applied sort option.

Returns:

  • (String)

    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

Returns:

  • (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

Returns:

  • (ActiveRecord::Association)


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

Returns:

  • (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.sort_options += [{
    key: key,
    string_or_proc: string_or_proc,
    opts: opts
  }]
end

Instance Method Details

#defaultsObject



84
85
86
# File 'lib/filterer/base.rb', line 84

def defaults
  {}
end

#directionObject



92
93
94
# File 'lib/filterer/base.rb', line 92

def direction
  params[:direction].try(:downcase) == 'desc' ? 'desc' : 'asc'
end

#starting_queryObject



88
89
90
# File 'lib/filterer/base.rb', line 88

def starting_query
  raise 'You must override this method!'
end