Module: Trestle::TableHelper

Defined in:
app/helpers/trestle/table_helper.rb

Instance Method Summary collapse

Instance Method Details

#pagination(collection:, entry_name: nil, **options) ⇒ Object

Renders the pagination controls for a collection.

collection - The paginated Kaminari collection to render controls for (required). options - Hash of options that will be passed to the Kaminari #paginate method (default: {}):

Examples

<%= pagination collection: Account.page(params[:page]), remote: true %>

Returns the HTML representation of the pagination controls as a HTML-safe String.



61
62
63
64
# File 'app/helpers/trestle/table_helper.rb', line 61

def pagination(collection:, entry_name: nil, **options)
  collection = collection.call if collection.respond_to?(:call)
  render "trestle/table/pagination", collection: collection, entry_name: entry_name, options: options
end

#table(name = nil, options = {}, &block) ⇒ Object

Renders an existing named table or builds and renders a custom table if a block is provided.

name - The (optional) name of the table to render (as a Symbol), or the actual Trestle::Table instance itself. options - Hash of options that will be passed to the table builder (default: {}):

:collection - The collection that should be rendered within the table. It should be an
              Array-like object, but will most likely be an ActiveRecord scope. It can
              also be a callable object (i.e. a Proc) in which case the result of calling
              the block will be used as the collection.
See Trestle::Table::Builder for additional options.

block - An optional block that is passed to Trestle::Table::Builder to define a custom table.

One of either the name or block must be provided, but not both.

Examples

<%= table collection: -> { Account.all }, admin: :accounts do %>
  <% column(:name) %>
  <% column(:balance) { |account| account.balance.format } %>
  <% column(:created_at, align: :center)
<% end %>

<%= table :accounts %>

Returns the HTML representation of the table as a HTML-safe String.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/trestle/table_helper.rb', line 26

def table(name=nil, options={}, &block)
  if block_given?
    if name.is_a?(Hash)
      options = name
    else
      collection = name
    end

    table = Table::Builder.build(options, &block)
  else
    if name.is_a?(Trestle::Table)
      table = name
    else
      table = admin.tables.fetch(name) { raise ArgumentError, "Unable to find table named #{name.inspect}" }
    end

    table = table.with_options(options.reverse_merge(sortable: false))
  end

  collection ||= options[:collection] || table.options[:collection]
  collection = collection.call if collection.respond_to?(:call)

  render "trestle/table/table", table: table, collection: collection
end