class TabulaRasa::Base

Attributes

collection[R]
column_collection[R]
klass[R]
options[R]
view[R]

Public Class Methods

new(collection, view, options = {}) { |self| ... } click to toggle source
# File lib/tabula_rasa/base.rb, line 10
def initialize(collection, view, options = {}, &block)
  raise ArgumentError, 'TabulaRasa only works on ActiveRecord Relation instances' unless collection.is_a?(ActiveRecord::Relation)
  @collection = collection
  @options = options
  @view = view
  @klass = collection.klass
  @column_collection = []
  yield self if block_given?
  ensure_row
end

Public Instance Methods

column(*args, &block) click to toggle source
# File lib/tabula_rasa/base.rb, line 26
def column(*args, &block)
  @column_collection << Column.new(self, *args, &block)
end
columns(*args) click to toggle source
# File lib/tabula_rasa/base.rb, line 30
def columns(*args)
  args.each do |arg|
    @column_collection << Column.new(self, arg)
  end
end
render() click to toggle source
# File lib/tabula_rasa/base.rb, line 21
def render
  return if column_collection.empty?
  content_tag :table, content, table_options
end
row(options = {}, &block) click to toggle source
# File lib/tabula_rasa/base.rb, line 36
def row(options = {}, &block)
  raise ArgumentError, 'Row definition cannot be called more than once' if @row.present?
  @row = Row.new(self, options, &block)
end

Private Instance Methods

collection_body() click to toggle source
# File lib/tabula_rasa/base.rb, line 63
def collection_body
  rows = collection.map do |member|
    content_tag :tr, @row.options_for(member) do
      cells = column_collection.map do |column|
        column.body_content_for member
      end
      safe_join cells
    end
  end
  safe_join rows
end
content() click to toggle source
# File lib/tabula_rasa/base.rb, line 45
def content
  safe_join [thead, tbody]
end
empty_body() click to toggle source
# File lib/tabula_rasa/base.rb, line 75
def empty_body
  content_tag :tr, class: 'empty' do
    content_tag :td, colspan: column_collection.size do
      "No #{klass.table_name.humanize.downcase} present"
    end
  end
end
ensure_row() click to toggle source
# File lib/tabula_rasa/base.rb, line 87
def ensure_row
  @row ||= Row.new(self)
end
table_options() click to toggle source
# File lib/tabula_rasa/base.rb, line 83
def table_options
  options.except :head, :body
end
tbody() click to toggle source
# File lib/tabula_rasa/base.rb, line 57
def tbody
  content_tag :tbody, options[:body] do
    collection.present? ? collection_body : empty_body
  end
end
thead() click to toggle source
# File lib/tabula_rasa/base.rb, line 49
def thead
  content_tag :thead, options[:head] do
    content_tag :tr do
      safe_join column_collection.map(&:head_content)
    end
  end
end