class RailsTableFor::Elements::Table

Attributes

columns[RW]
output_buffer[RW]
page_size[RW]
record_count[RW]
records[RW]
request_params[RW]
request_path[RW]

Public Class Methods

new(records, **options) click to toggle source
# File lib/rails_table_for/elements/table.rb, line 17
def initialize(records, **options)
  @records = records
  @record_count = records.count
  @columns = []
  options[:columns]&.each { |field| column(field) }
  @page_size = options[:page_size]
  @request_path = options[:request_path]
  @request_params = options[:request_params]
  @table_class = options[:class]
end

Public Instance Methods

column(field = nil, **options, &block) click to toggle source
# File lib/rails_table_for/elements/table.rb, line 28
def column(field = nil, **options, &block)
  if block_given?
    columns << BlockColumn.new(block, options)
  elsif field
    columns << FieldColumn.new(field, options)
  else
    raise 'Must provide either field or block'
  end
end
to_s() click to toggle source
# File lib/rails_table_for/elements/table.rb, line 38
def to_s
  return '' if record_count.zero?
  return '' if columns.empty?

  draw
end

Private Instance Methods

body() click to toggle source
# File lib/rails_table_for/elements/table.rb, line 67
def body
  content_tag :tbody do
    current_page_records.map { |record| body_row(record) }.join.html_safe
  end
end
body_row(record) click to toggle source
# File lib/rails_table_for/elements/table.rb, line 73
def body_row(record)
  content_tag :tr do
    columns.map { |column| column.td(record) }.join.html_safe
  end
end
draw() click to toggle source
# File lib/rails_table_for/elements/table.rb, line 47
def draw
  content_tag :div do
    table + pagination_links
  end
end
head() click to toggle source
# File lib/rails_table_for/elements/table.rb, line 59
def head
  content_tag :thead do
    content_tag :tr do
      columns.map(&:th).join.html_safe
    end
  end
end
table() click to toggle source
# File lib/rails_table_for/elements/table.rb, line 53
def table
  content_tag :table, class: @table_class do
    head + body
  end
end