Class: Katalyst::Tables::Collection::Filter

Inherits:
Object
  • Object
show all
Includes:
Core, Pagination, Sorting
Defined in:
app/models/katalyst/tables/collection/filter.rb

Overview

Entry point for creating a collection for use with table components where filter params are nested, e.g. ?filters=query

This class is intended to be subclassed, i.e.:

class ApplicationController < ActionController::Base

class Collection < Katalyst::Tables::Collection::Base
  ...
end

end

In the context of a controller action, construct a collection, apply it to a model, then pass the result to the view component: “‘ collection = Collection.new.with_params(params).apply(People.all) table = Katalyst::TableComponent.new(collection: collection) render table ““

Constant Summary

Constants included from Sorting

Sorting::DIRECTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sorting

#default_sort?, #sort=, #sort_status, #sortable?, #toggle_sort

Methods included from Pagination

#paginate?, #paginate_options

Methods included from Core

#filter, #filtered?, #filters, #model, #searchable?, #sortable?

Constructor Details

#initialize(param_key: :filters) ⇒ Filter

Returns a new instance of Filter.



42
43
44
45
46
# File 'app/models/katalyst/tables/collection/filter.rb', line 42

def initialize(param_key: :filters, **)
  super(**)

  @param_key = param_key.to_sym
end

Instance Attribute Details

#param_keyObject (readonly)

Returns the value of attribute param_key.



40
41
42
# File 'app/models/katalyst/tables/collection/filter.rb', line 40

def param_key
  @param_key
end

Class Method Details

.permitted_paramsObject



36
37
38
# File 'app/models/katalyst/tables/collection/filter.rb', line 36

def self.permitted_params
  super.excluding("sort", "page")
end

.with_params(params) ⇒ Object



32
33
34
# File 'app/models/katalyst/tables/collection/filter.rb', line 32

def self.with_params(params)
  new.with_params(params)
end

Instance Method Details

#apply(items) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'app/models/katalyst/tables/collection/filter.rb', line 72

def apply(items)
  if items.is_a?(Class) && items < ActiveRecord::Base
    super(items.all)
  elsif items.is_a?(ActiveRecord::Relation)
    super
  else
    raise ArgumentError, "Collection requires an ActiveRecord scope, given #{items.class}"
  end
end

#inspectObject



82
83
84
85
# File 'app/models/katalyst/tables/collection/filter.rb', line 82

def inspect
  "#<#{self.class.name} @param_key=#{param_key.inspect} " +
    "@attributes=#{attributes.inspect} @model=\"#{model}\" @count=#{items&.count}>"
end

#model_nameObject



48
49
50
51
52
# File 'app/models/katalyst/tables/collection/filter.rb', line 48

def model_name
  @model_name ||= items.model_name.tap do |name|
    name.param_key = param_key
  end
end

#to_paramsObject



64
65
66
67
68
69
70
# File 'app/models/katalyst/tables/collection/filter.rb', line 64

def to_params
  if filtered?
    { param_key.to_s => super.except("sort", "page") }.merge(super.slice("sort", "page"))
  else
    super.slice("sort", "page")
  end
end

#with_params(params) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'app/models/katalyst/tables/collection/filter.rb', line 54

def with_params(params)
  params  = params.permit(:sort, :page, param_key => self.class.permitted_params)
  filters = params[param_key]

  self.attributes = filters if filters.present?
  self.attributes = params.slice("page", "sort")

  self
end