class SuperTable::Builder

Attributes

table[RW]
view_context[RW]

Public Class Methods

new(table, view_context) click to toggle source
# File lib/super_table/builder.rb, line 11
def initialize(table, view_context)
  self.view_context = view_context
  self.table = table
end

Public Instance Methods

column(key=nil, options={}, &block) click to toggle source
# File lib/super_table/builder.rb, line 84
def column(key=nil, options={}, &block)
  if block_given?
    options = key
    return td(options, &block)
  end

  content = case key
  when Symbol
    @rows.last.send(key)
  else
    key
  end
  td(content, options)
end
columns(*args) { |record| ... } click to toggle source
# File lib/super_table/builder.rb, line 61
def columns(*args, &block)
  @rows = []
  options = args.extract_options!
  wrap_options = options.delete(:wrap) || {}
  if collection.empty?
    return placeholder_row
  end
  collection.each do |record|
    @rows << record
    html = wrap_tr(options, wrap_options) do
      if block_given?
        yield record
      else
        args.each do |key|
          concat td(record.send(key))
        end
      end
    end
    concat html
  end
  nil
end
head(key=nil, options={}, &block) click to toggle source
# File lib/super_table/builder.rb, line 34
def head(key=nil, options={}, &block)
  if block_given?
    options = key
    return th(options, &block)
  end

  case key
  when Symbol
    column_options = table.columns[key].clone
    title, desc = column_options.delete(:title), column_options.delete(:desc)
  else
    column_options = {}
    title = key
  end

  classes = []
  classes << options.delete(:class) if options[:class]
  classes << column_options.delete(:class) if column_options[:class]
  column_options.merge!(options)
  column_options.merge!(class: classes.join(" ")) if classes.present?

  th(column_options) do
    concat(title)
    concat content_tag(:div, desc, class: 'small text-primary') if desc
  end
end
heads(*args, &block) click to toggle source
# File lib/super_table/builder.rb, line 16
def heads(*args, &block)
  options = args.extract_options!

  classes = ['table-secondary']
  classes << options[:class] if options[:class]
  options[:class] = classes.join(" ")

  if block_given?
    tr(options, &block)
  else
    tr(options) do
      args.each do |key|
        concat head(key)
      end
    end
  end
end
placeholder_row() click to toggle source
# File lib/super_table/builder.rb, line 99
def placeholder_row
  tr do
    content = table.placeholder || "尚無資料"
    td(content, colspan: table.columns.length, class: 'text-center')
  end
end

Protected Instance Methods

wrap_tr(options, wrap_options={}, &block) click to toggle source
# File lib/super_table/builder.rb, line 108
def wrap_tr(options, wrap_options={}, &block)
  if wrap_options == false
    block.()
  else
    tr(options, &block)
  end
end