Class: CrudComponents::Query
- Inherits:
-
Object
- Object
- CrudComponents::Query
- Defined in:
- lib/crud_components/query.rb
Overview
Applies URL params to a relation: filtering, global search, sorting.
The uniform rule: a param is applied iff it names a filterable field of the fieldset in play that the current user may see (or one of the reserved params q/sort/dir/page/per). Everything else never reaches SQL.
Constant Summary collapse
- SORT_DIRECTIONS =
%w[asc desc].freeze
Instance Attribute Summary collapse
-
#fieldset ⇒ Object
readonly
Returns the value of attribute fieldset.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#param_prefix ⇒ Object
readonly
Returns the value of attribute param_prefix.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #apply(scope) ⇒ Object
- #fieldset_name ⇒ Object
- #filter_fields ⇒ Object
-
#initialize(model, params, fieldset: nil, ability: nil, param_prefix: nil, extra_fields: []) ⇒ Query
constructor
A new instance of Query.
- #param_name(key) ⇒ Object
- #searchable? ⇒ Boolean
-
#sort_state ⇒ Object
- field_name_string, direction_string
-
or nil.
- #sortable_fields ⇒ Object
-
#value(key) ⇒ Object
Current value of a (logical, unprefixed) param — for filter controls.
Constructor Details
#initialize(model, params, fieldset: nil, ability: nil, param_prefix: nil, extra_fields: []) ⇒ Query
Returns a new instance of Query.
12 13 14 15 16 17 18 19 20 |
# File 'lib/crud_components/query.rb', line 12 def initialize(model, params, fieldset: nil, ability: nil, param_prefix: nil, extra_fields: []) @model = model @structure = Structure.for(model) @fieldset = fieldset.is_a?(Fieldset) ? fieldset : @structure.fieldset(fieldset) @params = extract(params) @permission = PermissionContext.new(ability) @param_prefix = param_prefix @extra_fields = extra_fields end |
Instance Attribute Details
#fieldset ⇒ Object (readonly)
Returns the value of attribute fieldset.
10 11 12 |
# File 'lib/crud_components/query.rb', line 10 def fieldset @fieldset end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
10 11 12 |
# File 'lib/crud_components/query.rb', line 10 def model @model end |
#param_prefix ⇒ Object (readonly)
Returns the value of attribute param_prefix.
10 11 12 |
# File 'lib/crud_components/query.rb', line 10 def param_prefix @param_prefix end |
#structure ⇒ Object (readonly)
Returns the value of attribute structure.
10 11 12 |
# File 'lib/crud_components/query.rb', line 10 def structure @structure end |
Instance Method Details
#active? ⇒ Boolean
45 46 47 48 |
# File 'lib/crud_components/query.rb', line 45 def active? keys = filter_fields.flat_map { |f| [f.name.to_s, "#{f.name}_geq", "#{f.name}_leq"] } (keys + ['q']).any? { |key| param(key) } end |
#apply(scope) ⇒ Object
22 23 24 25 26 |
# File 'lib/crud_components/query.rb', line 22 def apply(scope) scope = apply_filters(scope) scope = apply_search(scope) apply_sort(scope) end |
#fieldset_name ⇒ Object
28 |
# File 'lib/crud_components/query.rb', line 28 def fieldset_name = fieldset.name |
#filter_fields ⇒ Object
30 31 32 33 |
# File 'lib/crud_components/query.rb', line 30 def filter_fields (structure.fieldset_filter_fields(fieldset) + @extra_fields.select(&:filterable?)) .select { |f| f.permitted?(@permission) } end |
#param_name(key) ⇒ Object
56 |
# File 'lib/crud_components/query.rb', line 56 def param_name(key) = "#{prefix}#{key}" |
#searchable? ⇒ Boolean
40 |
# File 'lib/crud_components/query.rb', line 40 def searchable? = structure.searchable? |
#sort_state ⇒ Object
- field_name_string, direction_string
-
or nil.
51 52 53 54 |
# File 'lib/crud_components/query.rb', line 51 def sort_state field = current_sort_field field && [field.name.to_s, direction] end |
#sortable_fields ⇒ Object
35 36 37 38 |
# File 'lib/crud_components/query.rb', line 35 def sortable_fields (structure.fieldset_sortable_fields(fieldset) + @extra_fields.select(&:sortable?)) .select { |f| f.permitted?(@permission) } end |
#value(key) ⇒ Object
Current value of a (logical, unprefixed) param — for filter controls.
43 |
# File 'lib/crud_components/query.rb', line 43 def value(key) = param(key) |