Class: ActiveAdmin::Views::TableFor

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

Direct Known Subclasses

IndexAsTable::IndexTableFor

Defined Under Namespace

Classes: Column

Instance Method Summary collapse

Instance Method Details

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



10
11
12
13
14
15
16
17
# File 'lib/active_admin/views/components/table_for.rb', line 10

def build(obj, options = {})
  @sortable       = options.delete(:sortable)
  @resource_class = options.delete(:i18n)
  @collection     = obj.respond_to?(:each) && !obj.is_a?(Hash) ? obj : [obj]
  @columns        = []
  build_table
  super(options)
end

#build_tableObject (protected)



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

def build_table
  build_table_head
  build_table_body
end

#build_table_bodyObject (protected)



75
76
77
78
79
80
# File 'lib/active_admin/views/components/table_for.rb', line 75

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

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



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

def build_table_cell(col, item)
  td class: col.html_class do
    render_data col.data, item
  end
end

#build_table_headObject (protected)



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

def build_table_head
  @thead = thead do
    @header_row = tr
  end
end

#build_table_header(col) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_admin/views/components/table_for.rb', line 57

def build_table_header(col)
  classes  = Arbre::HTML::ClassList.new
  sort_key = sortable? && col.sortable? && col.sort_key
  params   = request.query_parameters.except :page, :order, :commit, :format

  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: params, order: "#{sort_key}_#{order_for_sort_key(sort_key)}"
    end
  else
    th col.pretty_title, class: classes
  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.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

#current_sortObject (protected)

Returns an array for the current sort order

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


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_admin/views/components/table_for.rb', line 110

def current_sort
  @current_sort ||= begin
    order_clause = OrderClause.new params[:order]
    
    if order_clause.valid?
      [order_clause.field, order_clause.order]
    else
      []
    end
  end
end

#default_optionsObject (protected)



132
133
134
135
136
# File 'lib/active_admin/views/components/table_for.rb', line 132

def default_options
  {
    i18n: @resource_class
  }
end

#is_boolean?(data, item) ⇒ Boolean (protected)

Returns:

  • (Boolean)


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

def is_boolean?(data, item)
  if item.respond_to? :column_for_attribute
    attr = item.column_for_attribute(data) and attr.type == :boolean 
  end
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’



126
127
128
129
130
# File 'lib/active_admin/views/components/table_for.rb', line 126

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

#render_data(data, item) ⇒ Object (protected)



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

def render_data(data, item)
  value = if data.is_a? Proc
    data.call item
  elsif item.respond_to? data
    item.send data
  elsif item.respond_to? :[]
    item[data]
  end
  value = pretty_format(value) if data.is_a?(Symbol)
  value = status_tag value     if is_boolean? data, item
  value
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