Class: ActiveAdmin::Views::AttributesTable

Inherits:
Component
  • Object
show all
Defined in:
lib/active_admin/views/components/attributes_table.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) build(obj, *attrs)



7
8
9
10
11
12
13
14
15
16
# File 'lib/active_admin/views/components/attributes_table.rb', line 7

def build(obj, *attrs)
  @collection     = obj.respond_to?(:each) && !obj.is_a?(Hash) ? obj : [obj]
  @resource_class = @collection.first.class
  options = { }
  options[:for] = @collection.first if single_record?
  super(options)
  @table = table
  build_colgroups
  rows(*attrs)
end

- (Object) build_colgroups (protected)

Build Colgroups

Colgroups are only necessary for a collection of records; not a single record.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_admin/views/components/attributes_table.rb', line 55

def build_colgroups
  return if single_record?
  reset_cycle(self.class.to_s)
  within @table do
    col # column for row headers
    @collection.each do |record|
      classes = Arbre::HTML::ClassList.new
      classes << cycle(:even, :odd, name: self.class.to_s)
      classes << dom_class_name_for(record)
      col(id: dom_id_for(record), class: classes)
    end
  end
end

- (Object) content_for(record, attr) (protected)



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

def content_for(record, attr)
  previous = current_arbre_element.to_s
  value    = pretty_format find_attr_value(record, attr)
  value.blank? && previous == current_arbre_element.to_s ? empty_value : value
end

- (Object) default_id_for_prefix (protected)



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

def default_id_for_prefix
  'attributes_table'
end

- (Object) empty_value (protected)



77
78
79
# File 'lib/active_admin/views/components/attributes_table.rb', line 77

def empty_value
  span I18n.t('active_admin.empty'), class: "empty"
end

- (Object) find_attr_value(record, attr) (protected)



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

def find_attr_value(record, attr)
  if attr.is_a?(Proc)
    attr.call(record)
  elsif attr.to_s[/\A(.+)_id\z/] && record.respond_to?($1)
    record.send($1)
  elsif record.respond_to? attr
    record.send(attr)
  elsif record.respond_to? :[]
    record[attr]
  end
end

- (Object) header_content_for(attr) (protected)



69
70
71
72
73
74
75
# File 'lib/active_admin/views/components/attributes_table.rb', line 69

def header_content_for(attr)
  if @resource_class.respond_to?(:human_attribute_name)
    @resource_class.human_attribute_name(attr, default: attr.to_s.titleize)
  else
    attr.to_s.titleize
  end
end

- (Object) row(*args, &block)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_admin/views/components/attributes_table.rb', line 22

def row(*args, &block)
  title   = args[0]
  options = args.extract_options!
  classes = [:row]
  if options[:class]
    classes << options[:class]
  elsif title.present?
    classes << "row-#{title.to_s.parameterize('_')}"
  end
  options[:class] = classes.join(' ')

  @table << tr(options) do
    th do
      header_content_for(title)
    end
    @collection.each do |record|
      td do
        content_for(record, block || title)
      end
    end
  end
end

- (Object) rows(*attrs)



18
19
20
# File 'lib/active_admin/views/components/attributes_table.rb', line 18

def rows(*attrs)
  attrs.each {|attr| row(attr) }
end

- (Boolean) single_record? (protected)

Returns:

  • (Boolean)


99
100
101
# File 'lib/active_admin/views/components/attributes_table.rb', line 99

def single_record?
  @single_record ||= @collection.size == 1
end