Class: CrudComponents::Presenters::Filter
- Defined in:
- lib/crud_components/presenters/filter.rb
Overview
The single filter local of the standalone filter form partial.
Renders the fieldset's filterable fields (including its filters:
extension); never auto-submits — users compose several filters here.
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
Attributes inherited from Base
Instance Method Summary collapse
- #fields ⇒ Object
- #form_path ⇒ Object
-
#initialize(view:, model:, fieldset: nil, query: nil, param_prefix: nil, extra_columns: nil, sort: false) ⇒ Filter
constructor
A new instance of Filter.
- #param_name(key) ⇒ Object
-
#preserved_params ⇒ Object
Keep foreign params; our own controls resubmit themselves.
- #reset_path ⇒ Object
- #searchable? ⇒ Boolean
-
#sort_control? ⇒ Boolean
── sorting (headerless surfaces) ────────────────────────────────────── Whether to render the sort picker: asked for, and there's something to sort by.
-
#sort_field_choices ⇒ Object
The fields offered in the sort picker, as [human_name, name] pairs.
-
#sort_state ⇒ Object
[field_name, direction] currently in effect, or [nil, 'asc'] when unsorted.
- #value(key) ⇒ Object
Methods inherited from Base
#ability, #config, #css, #permission_context, #render_cell, #render_filter_control
Constructor Details
#initialize(view:, model:, fieldset: nil, query: nil, param_prefix: nil, extra_columns: nil, sort: false) ⇒ Filter
Returns a new instance of Filter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/crud_components/presenters/filter.rb', line 9 def initialize(view:, model:, fieldset: nil, query: nil, param_prefix: nil, extra_columns: nil, sort: false) super(view: view) @model = model.is_a?(Class) ? model : model.klass @structure = Structure.for(@model) @sort = sort # Dynamic columns become extra query fields, so their filters appear in the # form exactly like a declared field's — mirrors crud_collection's # extra_columns: (issue #22). A prebuilt query already carries its own. dynamic_fields = Array(extra_columns).map { |c| c.to_field(@model) } @query = if query.is_a?(Query) query else Query.new(@model, view.request.query_parameters, fieldset: @structure.fieldset(fieldset || :index), ability: ability, param_prefix: param_prefix, extra_fields: dynamic_fields) end end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/crud_components/presenters/filter.rb', line 7 def model @model end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/crud_components/presenters/filter.rb', line 7 def query @query end |
#structure ⇒ Object (readonly)
Returns the value of attribute structure.
7 8 9 |
# File 'lib/crud_components/presenters/filter.rb', line 7 def structure @structure end |
Instance Method Details
#fields ⇒ Object
27 |
# File 'lib/crud_components/presenters/filter.rb', line 27 def fields = query.filter_fields |
#form_path ⇒ Object
29 |
# File 'lib/crud_components/presenters/filter.rb', line 29 def form_path = view.request.path |
#param_name(key) ⇒ Object
32 |
# File 'lib/crud_components/presenters/filter.rb', line 32 def param_name(key) = query.param_name(key) |
#preserved_params ⇒ Object
Keep foreign params; our own controls resubmit themselves. Sort/dir are kept as hidden inputs (to preserve the current sort across an Apply) unless the sort picker renders them — then they'd duplicate, so we drop them.
54 55 56 57 58 59 |
# File 'lib/crud_components/presenters/filter.rb', line 54 def preserved_params own = fields.flat_map { |f| [param_name(f.name.to_s), param_name("#{f.name}_geq"), param_name("#{f.name}_leq")] } own += [param_name('q'), param_name('page'), param_name('per')] own += [param_name('sort'), param_name('dir')] if sort_control? view.request.query_parameters.reject { |key, _| own.include?(key) } end |
#reset_path ⇒ Object
30 |
# File 'lib/crud_components/presenters/filter.rb', line 30 def reset_path = view.request.path |
#searchable? ⇒ Boolean
28 |
# File 'lib/crud_components/presenters/filter.rb', line 28 def searchable? = query.searchable? |
#sort_control? ⇒ Boolean
── sorting (headerless surfaces) ────────────────────────────────────── Whether to render the sort picker: asked for, and there's something to sort by. A table carries its own header sort links, so this is opt-in.
38 |
# File 'lib/crud_components/presenters/filter.rb', line 38 def sort_control? = @sort && query.sortable_fields.any? |
#sort_field_choices ⇒ Object
The fields offered in the sort picker, as [human_name, name] pairs.
41 42 43 |
# File 'lib/crud_components/presenters/filter.rb', line 41 def sort_field_choices query.sortable_fields.map { |f| [f.human_name, f.name.to_s] } end |
#sort_state ⇒ Object
[field_name, direction] currently in effect, or [nil, 'asc'] when unsorted.
46 47 48 49 |
# File 'lib/crud_components/presenters/filter.rb', line 46 def sort_state current, dir = query.sort_state [current, dir || 'asc'] end |
#value(key) ⇒ Object
33 |
# File 'lib/crud_components/presenters/filter.rb', line 33 def value(key) = query.value(key) |