Class: Filterrific::ParamSet

Inherits:
Object
  • Object
show all
Defined in:
lib/filterrific/param_set.rb

Overview

FilterParamSet is a container to store FilterParams

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_model_class, filterrific_params = {}) ⇒ Filterrific::ParamSet

Initializes a new Filterrific::ParamSet. This is the core of Filterrific where all the action happens.

Parameters:

  • a_model_class (Class)

    the class you want to filter records of.

  • filterrific_params (Hash, optional) (defaults to: {})

    the filter params, falls back to model_class’ default_settings.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/filterrific/param_set.rb', line 16

def initialize(a_model_class, filterrific_params = {})
  self.model_class = a_model_class
  @select_options = {}

  # Use either passed in filterrific_params or resource class' default_settings.
  # Don't merge the hashes. This causes trouble if an option is set to nil
  # by the user, then it will be overriden by default_settings.
  # You might wonder "what if I want to change only one thing from the defaults?"
  # Persistence, baby. By the time you submit changes to one filter, all the others
  # will be already initialized with the defaults.
  filterrific_params = model_class.filterrific_default_filter_params if filterrific_params.blank?
  if defined?(ActionController::Parameters) && filterrific_params.is_a?(ActionController::Parameters)
    permissible_filter_params = []
    model_class.filterrific_available_filters.each do |p|
      permissible_filter_params << if filterrific_params[p].is_a?(ActionController::Parameters)
        {p => filterrific_params[p].keys}
      elsif filterrific_params[p].is_a?(Array)
        {p => []}
      else
        p
      end
    end
    filterrific_params = filterrific_params.permit(permissible_filter_params).to_h.stringify_keys
  else
    filterrific_params.stringify_keys!
  end
  filterrific_params = condition_filterrific_params(filterrific_params)
  define_and_assign_attr_accessors_for_each_filter(filterrific_params)
end

Instance Attribute Details

#model_classObject

Returns the value of attribute model_class.



7
8
9
# File 'lib/filterrific/param_set.rb', line 7

def model_class
  @model_class
end

#select_optionsObject

Returns the value of attribute select_options.



8
9
10
# File 'lib/filterrific/param_set.rb', line 8

def select_options
  @select_options
end

Instance Method Details

#findObject

A shortcut to run the ActiveRecord query on model_class. Use this if you want to start with the model_class, and not an existing ActiveRecord::Relation. Allows ‘@filterrific.find` in controller instead of `ModelClass.filterrific_find(@filterrific)`



50
51
52
# File 'lib/filterrific/param_set.rb', line 50

def find
  model_class.filterrific_find(self)
end

#to_hashHash

Returns Filterrific::ParamSet as hash (used for URL params and serialization)

Returns:

  • (Hash)

    with stringified keys



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/filterrific/param_set.rb', line 56

def to_hash
  {}.tap { |h|
    model_class.filterrific_available_filters.each do |filter_name|
      param_value = send(filter_name)
      if param_value.blank?
        # do nothing
      elsif param_value.is_a?(Proc)
        # evaluate Proc so it can be serialized
        h[filter_name] = param_value.call
      elsif param_value.is_a?(OpenStruct)
        # convert OpenStruct to hash
        h[filter_name] = param_value.marshal_dump
      else
        h[filter_name] = param_value
      end
    end
  }
end

#to_jsonString

Returns params as JSON string.

Returns:

  • (String)


77
78
79
# File 'lib/filterrific/param_set.rb', line 77

def to_json
  to_hash.to_json
end