Class: Katalyst::Tables::Collection::Base

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

Overview

Entry point for creating a collection for use with table components where filter params are flat, e.g. ?search=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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sorting

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

Methods included from Pagination

#initialize, #paginate?, #paginate_options

Methods included from Core

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

Methods included from HasParams

#to_params

Class Method Details

.with_params(params) ⇒ Object



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

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

Instance Method Details

#apply(items) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/models/katalyst/tables/collection/base.rb', line 51

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



61
62
63
# File 'app/models/katalyst/tables/collection/base.rb', line 61

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

#model_nameObject



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

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

#with_params(params) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/models/katalyst/tables/collection/base.rb', line 42

def with_params(params)
  # test support
  params = ActionController::Parameters.new(params) unless params.is_a?(ActionController::Parameters)

  self.attributes = params.permit(self.class.permitted_params)

  self
end