Class: Filterameter::ControllerFilters

Inherits:
Object
  • Object
show all
Defined in:
lib/filterameter/controller_filters.rb

Overview

Controller Filters

Class ControllerFilters stores the configuration declared via class-level method calls such as the list of filters and the optionally declared model class. Each controller will have one instance of the controller declarations stored as a class variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_name, controller_path) ⇒ ControllerFilters

Returns a new instance of ControllerFilters.



24
25
26
27
28
29
30
# File 'lib/filterameter/controller_filters.rb', line 24

def initialize(controller_name, controller_path)
  @controller_name = controller_name
  @controller_path = controller_path
  @declarations = {}
  @ranges = {}
  @filters = Hash.new { |hash, key| hash[key] = filter_factory.build(@declarations[key]) }
end

Instance Attribute Details

#query_variable_nameObject



43
44
45
# File 'lib/filterameter/controller_filters.rb', line 43

def query_variable_name
  @query_variable_name ||= model_class.model_name.plural
end

Instance Method Details

#add_filter(parameter_name, options) ⇒ Object



36
37
38
39
40
41
# File 'lib/filterameter/controller_filters.rb', line 36

def add_filter(parameter_name, options)
  @declarations[parameter_name.to_s] =
    Filterameter::FilterDeclaration.new(parameter_name, options).tap do |fd|
      add_declarations_for_range(fd, options, parameter_name) if fd.range_enabled?
    end
end

#build_query(filter_params, starting_query) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/filterameter/controller_filters.rb', line 47

def build_query(filter_params, starting_query)
  valid_filters(filter_params)
    .tap { |parameters| convert_min_and_max_to_range(parameters) }
    .reduce(starting_query || model_class.all) do |query, (name, value)|
    @filters[name].apply(query, value)
  end
end

#model_class=(model_class) ⇒ Object



32
33
34
# File 'lib/filterameter/controller_filters.rb', line 32

def model_class=(model_class)
  @model_class = model_class.is_a?(String) ? model_class.constantize : model_class
end