Class: ActiveAdmin::Views::TableFor

Inherits:
Arbre::HTML::Table show all
Defined in:
lib/active_admin/views/components/table_for.rb

Direct Known Subclasses

IndexAsTable::IndexTableFor

Defined Under Namespace

Classes: Column

Constant Summary

Constants included from Arbre::HTML

Arbre::HTML::AUTO_BUILD_ELEMENTS, Arbre::HTML::HTML5_ELEMENTS

Instance Attribute Summary

Attributes inherited from Arbre::HTML::Tag

#attributes

Attributes inherited from Arbre::HTML::Element

#children, #parent

Instance Method Summary collapse

Methods inherited from Arbre::HTML::Table

#initialize, #set_table_tag_defaults

Methods inherited from Arbre::HTML::Tag

#add_class, #class_list, #class_names, #get_attribute, #has_attribute?, #id, #id!, #id=, #initialize, #remove_attribute, #remove_class, #set_attribute, #to_html

Methods inherited from Arbre::HTML::Element

#+, #<<, #add_child, #assigns, builder_method, #content, #content=, #document, #each, #get_elements_by_tag_name, #helpers, #html_safe, #indent_level, #initialize, #parent?, #remove_child, #to_ary, #to_html, #to_s, #to_str

Methods included from Arbre::HTML::BuilderMethods

#build_tag, #current_dom_context, #insert_tag, #insert_text_node_if_string, #with_current_dom_context

Methods included from Arbre::HTML

#current_dom_context, #helpers, #method_missing

Constructor Details

This class inherits a constructor from Arbre::HTML::Table

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Arbre::HTML

Instance Method Details

#build(collection, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
# 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

#build_sortable_header_for(title, sort_key) ⇒ Object (protected)



70
71
72
73
74
75
76
77
78
79
# File 'lib/active_admin/views/components/table_for.rb', line 70

def build_sortable_header_for(title, sort_key)
  classes = Arbre::HTML::ClassList.new(["sortable"])
  if current_sort[0] == sort_key
    classes << "sorted-#{current_sort[1]}"
  end

  th :class => classes do
    link_to(title, params.merge(:order => "#{sort_key}_#{order_for_sort_key(sort_key)}").except(:page))
  end
end

#build_tableObject (protected)



51
52
53
54
# File 'lib/active_admin/views/components/table_for.rb', line 51

def build_table
  build_table_head
  build_table_body
end

#build_table_bodyObject (protected)



81
82
83
84
85
86
# File 'lib/active_admin/views/components/table_for.rb', line 81

def build_table_body
  @tbody = tbody do
    # Build enough rows for our collection
    @collection.each{|_| tr(:class => cycle('odd', 'even')) }
  end
end

#build_table_cell(col, item) ⇒ Object (protected)



88
89
90
91
92
93
94
95
96
# File 'lib/active_admin/views/components/table_for.rb', line 88

def build_table_cell(col, item)
  td 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_headObject (protected)



56
57
58
59
60
# 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) ⇒ Object (protected)



62
63
64
65
66
67
68
# File 'lib/active_admin/views/components/table_for.rb', line 62

def build_table_header(col)
  if sortable? && col.sortable?
    build_sortable_header_for(col.title, col.sort_key)
  else
    th(col.title)
  end
end

#column(*args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_admin/views/components/table_for.rb', line 19

def column(*args, &block)
  options = default_options.merge(args.last.is_a?(::Hash) ? args.pop : {})
  title = args[0]
  data  = args[1] || args[0]

  col = Column.new(title, data, 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

#current_sortObject (protected)

Returns an array for the current sort order

current_sort[0] #=> sort_key
current_sort[1] #=> asc | desc


101
102
103
104
105
106
107
# File 'lib/active_admin/views/components/table_for.rb', line 101

def current_sort
  @current_sort ||= if params[:order] && params[:order] =~ /^([\w\_\.]+)_(desc|asc)$/
    [$1,$2]
  else
    []
  end
end

#default_optionsObject (protected)



119
120
121
122
123
# File 'lib/active_admin/views/components/table_for.rb', line 119

def default_options
  {
    :i18n => @resource_class
  }
end

#order_for_sort_key(sort_key) ⇒ Object (protected)

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’



113
114
115
116
117
# File 'lib/active_admin/views/components/table_for.rb', line 113

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

#sortable?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/active_admin/views/components/table_for.rb', line 40

def sortable?
  @sortable
end

#tag_nameObject



6
7
8
# File 'lib/active_admin/views/components/table_for.rb', line 6

def tag_name
  'table'
end

#visible_columnsObject

Returns the columns to display based on the conditional block



45
46
47
# File 'lib/active_admin/views/components/table_for.rb', line 45

def visible_columns
  @visible_columns ||= @columns.select{|col| col.display_column? }
end