class TableHelp::TableFor

Constants

Column

Attributes

collection[R]
columns[R]
context[R]
options[R]
q[R]
strategies[R]

Public Class Methods

new(collection, context, options = {}) click to toggle source
# File lib/table_help/table_for.rb, line 8
def initialize(collection, context, options = {})
  @collection   = collection
  @context      = context
  @options      = default_options.merge(options)
  @columns      = []
  @strategies   = []
  @q            = @options.delete(:q)
end

Public Instance Methods

column(name = nil, method_name = nil, &block) click to toggle source
# File lib/table_help/table_for.rb, line 17
def column(name = nil, method_name = nil, &block)
  columns << Column.new(name, Formatter.format_attribute_name(name, collection))
  strategies << Strategy.new(name, block_given? ? block : method_name)
end
to_html() click to toggle source
# File lib/table_help/table_for.rb, line 22
def to_html
  return if collection.empty?

  tag.table(options) do
    concat thead
    concat tbody
  end
end

Private Instance Methods

default_options() click to toggle source
# File lib/table_help/table_for.rb, line 65
def default_options
  TableHelp.config.default_options[:table_for] || {}
end
sortable?() click to toggle source
# File lib/table_help/table_for.rb, line 47
def sortable?
  !q.nil? && respond_to?(:sort_link)
end
tbody() click to toggle source
# File lib/table_help/table_for.rb, line 51
def tbody
  tag.tbody do
    collection.each do |record|
      concat(
        tag.tr(class: "table-row-#{record.model_name.singular}#{record.to_param}") do
          strategies.each do |strategy|
            concat tag.td(Formatter.format_value(strategy.name, strategy.to_value(record, context)), class: "col-#{strategy.name}")
          end
        end,
      )
    end
  end
end
thead() click to toggle source
# File lib/table_help/table_for.rb, line 33
def thead
  tag.thead do
    tag.tr do
      columns.each do |column|
        if sortable?
          concat tag.th(sort_link(q, column.name, column.human_name))
        else
          concat tag.th(column.human_name)
        end
      end
    end
  end
end