module Filterameter::DeclarativeFilters
Declarative Filters
¶ ↑
module DeclarativeFilters
provides a controller DSL to declare filters along with any validations.
Public Instance Methods
# File lib/filterameter/declarative_filters.rb, line 63 def controller_filters @controller_filters ||= Filterameter::ControllerFilters.new(controller_name, controller_path) end
Declares a filter that can be read from the parameters and applied to the ActiveRecord query. The name
identifies the name of the parameter and is the default value to determine the criteria to be applied. The name can be either an attribute or a scope.
Options
¶ ↑
- :name
-
Specify the attribute or scope name if the parameter name is not the same. The default value is the parameter name, so if the two match this can be left out.
- :association
-
Specify the name of the association if the attribute or scope is nested.
- :validates
-
Specify a validation if the parameter value should be validated. This uses ActiveModel validations; please review those for types of validations and usage.
- :partial
-
Specify the partial option if the filter should do a partial search (SQL's `LIKE`). The partial option accepts a hash to specify the search behavior. Here are the available options:
-
match: anywhere (default), from_start, dynamic
-
case_sensitive: true, false (default)
There are two shortcuts: : the partial option can be declared with `true`, which just uses the defaults; or the partial option can be declared with the match option directly, such as `partial: :from_start`.
-
- :range
-
Specify a range option if the filter also allows ranges to be searched. The range option accepts the following options:
-
true: enables two additional parameters with attribute name plus suffixes
_min
and_max
-
:min_only: enables additional parameter with attribute name plus suffix
_min
-
:max_only: enables additional parameter with attribute name plus suffix
_max
-
# File lib/filterameter/declarative_filters.rb, line 55 def filter(name, options = {}) controller_filters.add_filter(name, options) end
# File lib/filterameter/declarative_filters.rb, line 13 def filter_model(model_class, query_var_name = nil) controller_filters.model_class = model_class filter_query_var_name(query_var_name) if query_var_name.present? end
# File lib/filterameter/declarative_filters.rb, line 18 def filter_query_var_name(query_variable_name) controller_filters.query_variable_name = query_variable_name end
# File lib/filterameter/declarative_filters.rb, line 59 def filters(*names) names.each { |name| filter(name) } end
Private Instance Methods
# File lib/filterameter/declarative_filters.rb, line 70 def build_filtered_query var_name = "@#{self.class.controller_filters.query_variable_name}" instance_variable_set( var_name, self.class.controller_filters.build_query(params.to_unsafe_h.fetch(:filter, {}), instance_variable_get(var_name)) ) end