class ActiveAdmin::Views::TableFor

Public Instance Methods

build(collection, options = {}) click to toggle source
Calls superclass method
# File lib/active_admin/views/components/table_for.rb, line 10
def build(collection, options = {})
  @sortable = options.delete(:sortable)
  @resource_class = options.delete(:i18n)
  @collection = collection
  @columns = []
  build_table
  super(options)
end
column(*args, &block) click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 19
def column(*args, &block)
  options = default_options.merge(args.extract_options!)
  title = args[0]
  data  = args[1] || args[0]

  col = Column.new(title, data, @resource_class, options, &block)
  @columns << col

  # Build our header item
  within @header_row do
    build_table_header(col)
  end

  # Add a table cell for each item
  @collection.each_with_index do |item, i|
    within @tbody.children[i] do
      build_table_cell(col, item)
    end
  end
end
sortable?() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 40
def sortable?
  @sortable
end
tag_name() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 6
def tag_name
  'table'
end
visible_columns() click to toggle source

Returns the columns to display based on the conditional block

# File lib/active_admin/views/components/table_for.rb, line 45
def visible_columns
  @visible_columns ||= @columns.select{|col| col.display_column? }
end

Protected Instance Methods

build_table() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 51
def build_table
  build_table_head
  build_table_body
end
build_table_body() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 79
def build_table_body
  @tbody = tbody do
    # Build enough rows for our collection
    @collection.each{|elem| tr(:class => cycle('odd', 'even'), :id => dom_id(elem)) }
  end
end
build_table_cell(col, item) click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 86
def build_table_cell(col, item)
  td(:class =>  col.html_class) do
    rvalue = call_method_or_proc_on(item, col.data, :exec => false)
    if col.data.is_a?(Symbol)
      rvalue = pretty_format(rvalue)
    end
    rvalue
  end
end
build_table_head() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 56
def build_table_head
  @thead = thead do
    @header_row = tr
  end
end
build_table_header(col) click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 62
def build_table_header(col)
  classes = Arbre::HTML::ClassList.new
  sort_key = sortable? && col.sortable? && col.sort_key

  classes << 'sortable'                         if sort_key
  classes << "sorted-#{current_sort[1]}"        if sort_key && current_sort[0] == sort_key
  classes << col.html_class

  if sort_key
    th :class => classes do
      link_to(col.pretty_title, params.merge(:order => "#{sort_key}_#{order_for_sort_key(sort_key)}").except(:page))
    end
  else
    th(col.pretty_title, :class => classes)
  end
end
current_sort() click to toggle source

Returns an array for the current sort order

current_sort[0] #=> sort_key
current_sort[1] #=> asc | desc
# File lib/active_admin/views/components/table_for.rb, line 99
def current_sort
  @current_sort ||= if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
    [$1,$2]
  else
    []
  end
end
default_options() click to toggle source
# File lib/active_admin/views/components/table_for.rb, line 117
def default_options
  {
    :i18n => @resource_class
  }
end
order_for_sort_key(sort_key) click to toggle source

Returns the order to use for a given sort key

Default is to use 'desc'. If the current sort key is 'desc' it will return 'asc'

# File lib/active_admin/views/components/table_for.rb, line 111
def order_for_sort_key(sort_key)
  current_key, current_order = current_sort
  return 'desc' unless current_key == sort_key
  current_order == 'desc' ? 'asc' : 'desc'
end