Class: Filternator::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/filternator/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filter

Returns a new instance of Filter.



6
7
8
9
10
11
12
# File 'lib/filternator/filter.rb', line 6

def initialize(options = {})
  @scope          = options.fetch(:scope)
  @scope_name     = options.fetch(:scope_name)     { derive_scope_name }
  @presenter      = options.fetch(:presenter)      { null_presenter }
  @all_filters    = options.fetch(:all_filters)    { scope.all_filters }
  @default_filter = options.fetch(:default_filter) { "all" }
end

Instance Attribute Details

#all_filtersObject (readonly)

Returns the value of attribute all_filters.



4
5
6
# File 'lib/filternator/filter.rb', line 4

def all_filters
  @all_filters
end

#default_filterObject (readonly)

Returns the value of attribute default_filter.



4
5
6
# File 'lib/filternator/filter.rb', line 4

def default_filter
  @default_filter
end

#presenterObject (readonly)

Returns the value of attribute presenter.



4
5
6
# File 'lib/filternator/filter.rb', line 4

def presenter
  @presenter
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/filternator/filter.rb', line 4

def scope
  @scope
end

#scope_nameObject (readonly)

Returns the value of attribute scope_name.



4
5
6
# File 'lib/filternator/filter.rb', line 4

def scope_name
  @scope_name
end

Instance Method Details

#apply(params) ⇒ Object



23
24
25
# File 'lib/filternator/filter.rb', line 23

def apply(params)
  ApplyFilter.new(self, params)
end

#statsObject



14
15
16
17
18
19
20
21
# File 'lib/filternator/filter.rb', line 14

def stats
  pairs = all_filters.map { |filter|
    result = apply(filter: filter).count
    result = result.values.inject(:+) if result.is_a?(Hash) # ActiveRecord `group by` fix
    [ filter, result ]
  }
  Hash[pairs]
end