class Renderer::Column

Public Class Methods

from( name: nil, table_name: nil, col_options: nil, klass: nil, output: nil, &block) click to toggle source
# File lib/tabulatr/renderer/column.rb, line 31
def self.from(
  name: nil,
  table_name: nil,
  col_options: nil,
  klass: nil,
  output: nil,
  &block)
  self.new(
    name: name,
    table_name: table_name,
    col_options: col_options,
    klass: klass,
    output: output,
    block: block
  )
end

Public Instance Methods

action?() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 68
def action?() false end
association?() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 66
def association?() false end
checkbox?() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 67
def checkbox?() false end
coltype() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 63
def coltype() 'column' end
column?() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 65
def column?() true end
determine_appropriate_filter!() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 97
def determine_appropriate_filter!
  typ = self.klass.columns_hash[self.name.to_s].type.to_sym rescue nil
  case typ
  when :integer then self.col_options.filter = filter_type_for_integer
  when :enum then self.col_options.filter = :enum
  when :float, :decimal then self.col_options.filter = :decimal
  when :string, :text then self.col_options.filter = :like
  when :date, :time, :datetime, :timestamp then self.col_options.filter = :date
  when :boolean then self.col_options.filter = :checkbox
  when nil then self.col_options.filter = :exact
  else raise "Unknown filter type for #{self.name}: »#{typ}«"
  end
end
full_name() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 62
def full_name() [table_name, name].compact.join(":") end
human_name() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 50
def human_name()
  h = col_options.header
  if h && h.respond_to?(:call)
    h.()
  elsif h
    h
  else
    klass.human_attribute_name(name)
  end
end
klassname() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 48
def klassname() @_klassname ||= @klass.name.underscore end
principal_value(record, view) click to toggle source
# File lib/tabulatr/renderer/column.rb, line 85
def principal_value(record, view)
  if output
    view.instance_exec(record, &output)
  elsif block
    view.instance_exec(record, &block)
  elsif name
    record.send name
  else
    nil
  end
end
sort_param() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 61
def sort_param() "#{klassname}_sort" end
to_json() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 111
def to_json
  {
    name:        "#{table_name}:#{name}",
    # name:        name,
    header:      human_name,
    # klassname:   klassname,
    # table_name:  table_name,
    filter:      col_options.filter,
    sortable:    col_options.sortable,
    data_html:   col_options.data_html,
    header_html: col_options.header_html,
  }
end
value_for(record, view) click to toggle source
# File lib/tabulatr/renderer/column.rb, line 70
def value_for(record, view)
  val = principal_value(record, view)
  if self.col_options.format.present?
    if val.respond_to?(:to_ary)
      val.map do |v|
        format_value(v, view)
      end
    else
      format_value(val, view)
    end
  else
    val
  end
end

Private Instance Methods

filter_type_for_integer() click to toggle source
# File lib/tabulatr/renderer/column.rb, line 127
def filter_type_for_integer
  if self.klass.respond_to?(:defined_enums) && self.klass.defined_enums.keys.include?(self.name.to_s)
    :enum
  else
    :integer
  end
end
format_value(value, view) click to toggle source
# File lib/tabulatr/renderer/column.rb, line 135
def format_value(value, view)
  case self.col_options.format
  when Symbol then view.send(col_options.format, value)
  when String then col_options.format % value
  when Proc   then col_options.format.(value)
  else value
  end
end