class TabulaRasa::Column

Attributes

attribute[R]
base[R]
body_value_option[R]
head_value_option[R]
options[R]

Public Class Methods

new(base, *args) { |self| ... } click to toggle source
# File lib/tabula_rasa/column.rb, line 5
def initialize(base, *args, &block)
  @base = base
  @attribute = args.first
  @options = args.extract_options!
  yield self if block_given?
  massage_options
  ensure_valueable
end

Public Instance Methods

body_content_for(instance) click to toggle source
# File lib/tabula_rasa/column.rb, line 22
def body_content_for(instance)
  content_tag :td, body_value_for(instance), options[:body]
end
head() click to toggle source
# File lib/tabula_rasa/column.rb, line 18
def head
  raise ArgumentError, "There's no need to pass a block for head. It's only evaluated once. Just set the head value via options or attribute [if appropriate]."
end
head_content() click to toggle source
# File lib/tabula_rasa/column.rb, line 14
def head_content
  content_tag :th, head_value, options[:head]
end
value(&block) click to toggle source
# File lib/tabula_rasa/column.rb, line 26
def value(&block)
  @body_value_option = block if block_given?
end

Private Instance Methods

body_value_for(instance) click to toggle source
# File lib/tabula_rasa/column.rb, line 61
def body_value_for(instance)
  value = body_value_option || instance.send(attribute)
  if value.respond_to?(:call)
    value.call instance
  else
    value.to_s
  end
end
ensure_body_valueable() click to toggle source
# File lib/tabula_rasa/column.rb, line 43
def ensure_body_valueable
  return if body_value_option.present? || base.collection.empty?
  unless attribute.present? && base.collection.first.respond_to?(attribute)
    raise ArgumentError, "Body value cannot be determined from arguments. #{base.klass} does not respond to #{attribute.inspect}."
  end
end
ensure_head_valueable() click to toggle source
# File lib/tabula_rasa/column.rb, line 39
def ensure_head_valueable
  raise ArgumentError, 'Head value cannot be determined from arguments' unless head_value_option.present? || attribute.present?
end
ensure_valueable() click to toggle source
# File lib/tabula_rasa/column.rb, line 34
def ensure_valueable
  ensure_head_valueable
  ensure_body_valueable
end
head_value() click to toggle source
# File lib/tabula_rasa/column.rb, line 56
def head_value
  name = head_value_option || attribute
  name.to_s.humanize.titleize
end
massage_options() click to toggle source
# File lib/tabula_rasa/column.rb, line 50
def massage_options
  @head_value_option = options[:head].is_a?(Hash) ? options[:head].delete(:value) : options.delete(:head)
  # Allow for body_value_option having been set by body block. Do not allow overriding this!
  @body_value_option ||= options[:body].is_a?(Hash) ? options[:body].delete(:value) : options.delete(:body)
end