Class: Filterrific::ParamSet
- Inherits:
-
Object
- Object
- Filterrific::ParamSet
- Defined in:
- lib/filterrific/param_set.rb
Overview
FilterParamSet is a container to store FilterParams
Instance Attribute Summary collapse
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#select_options ⇒ Object
Returns the value of attribute select_options.
Instance Method Summary collapse
-
#find ⇒ Object
A shortcut to run the ActiveRecord query on model_class.
-
#has_filters_applied? ⇒ Boolean
Returns true if this instance of Filterrific has params other than the defaults.
-
#initialize(a_model_class, filterrific_params = {}) ⇒ Filterrific::ParamSet
constructor
Initializes a new Filterrific::ParamSet.
-
#to_hash ⇒ Hash
Returns Filterrific::ParamSet as hash (used for URL params and serialization).
-
#to_json ⇒ String
Returns params as JSON string.
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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/filterrific/param_set.rb', line 20 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) filterrific_params = filterrific_params.permit(model_class.filterrific_available_filters).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_class ⇒ Object
Returns the value of attribute model_class.
11 12 13 |
# File 'lib/filterrific/param_set.rb', line 11 def model_class @model_class end |
#select_options ⇒ Object
Returns the value of attribute select_options.
12 13 14 |
# File 'lib/filterrific/param_set.rb', line 12 def @select_options end |
Instance Method Details
#find ⇒ Object
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)`
44 45 46 |
# File 'lib/filterrific/param_set.rb', line 44 def find model_class.filterrific_find(self) end |
#has_filters_applied? ⇒ Boolean
Returns true if this instance of Filterrific has params other than the defaults.
51 52 53 |
# File 'lib/filterrific/param_set.rb', line 51 def has_filters_applied? sdf end |
#to_hash ⇒ Hash
Returns Filterrific::ParamSet as hash (used for URL params and serialization)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/filterrific/param_set.rb', line 57 def to_hash {}.tap { |h| model_class.filterrific_available_filters.each do |filter_name| param_value = self.send(filter_name) case when param_value.blank? # do nothing when param_value.is_a?(Proc) # evaluate Proc so it can be serialized h[filter_name] = param_value.call when 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_json ⇒ String
Returns params as JSON string.
79 80 81 |
# File 'lib/filterrific/param_set.rb', line 79 def to_json to_hash.to_json end |