Class: CrudComponents::Query

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

Instance Method Summary collapse

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

#fieldsetObject (readonly)

Returns the value of attribute fieldset.



10
11
12
# File 'lib/crud_components/query.rb', line 10

def fieldset
  @fieldset
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/crud_components/query.rb', line 10

def model
  @model
end

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

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

Returns:

  • (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_nameObject



28
# File 'lib/crud_components/query.rb', line 28

def fieldset_name = fieldset.name

#filter_fieldsObject



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

Returns:

  • (Boolean)


40
# File 'lib/crud_components/query.rb', line 40

def searchable? = structure.searchable?

#sort_stateObject

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_fieldsObject



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)