Class: CrudComponents::Presenters::Filter

Inherits:
Base
  • Object
show all
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

Base::GEM_VIEW_ROOT

Instance Attribute Summary collapse

Attributes inherited from Base

#view

Instance Method Summary collapse

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) ⇒ Filter

Returns a new instance of Filter.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/crud_components/presenters/filter.rb', line 9

def initialize(view:, model:, fieldset: nil, query: nil, param_prefix: nil)
  super(view: view)
  @model = model.is_a?(Class) ? model : model.klass
  @structure = Structure.for(@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)
           end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/crud_components/presenters/filter.rb', line 7

def model
  @model
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/crud_components/presenters/filter.rb', line 7

def query
  @query
end

#structureObject (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

#fieldsObject



22
# File 'lib/crud_components/presenters/filter.rb', line 22

def fields = query.filter_fields

#form_pathObject



24
# File 'lib/crud_components/presenters/filter.rb', line 24

def form_path = view.request.path

#param_name(key) ⇒ Object



27
# File 'lib/crud_components/presenters/filter.rb', line 27

def param_name(key) = query.param_name(key)

#preserved_paramsObject

Keep sort and foreign params; our own controls resubmit themselves.



31
32
33
34
35
# File 'lib/crud_components/presenters/filter.rb', line 31

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')]
  view.request.query_parameters.reject { |key, _| own.include?(key) }
end

#reset_pathObject



25
# File 'lib/crud_components/presenters/filter.rb', line 25

def reset_path = view.request.path

#searchable?Boolean

Returns:

  • (Boolean)


23
# File 'lib/crud_components/presenters/filter.rb', line 23

def searchable? = query.searchable?

#value(key) ⇒ Object



28
# File 'lib/crud_components/presenters/filter.rb', line 28

def value(key) = query.value(key)