Method: Qadmin::Helper#admin_table

Defined in:
lib/qadmin/helper.rb

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/qadmin/helper.rb', line 77

def admin_table(collection, options = {})
  html = '<table>'
  html << '<tr>'
  attributes = options[:attributes] || self.qadmin_configuration.display_columns
  model_column_types = SuperHash.new
  attributes.each do |attribute_name|
    column = self.qadmin_configuration.model_klass.columns.detect {|c| c.name == attribute_name.to_s }
    model_column_types[attribute_name] = column.type if column
  end
  attributes.each_with_index do |attribute, i|
    html << (i == 0 ? '<th class="first_col">' : '<th>')
    html << sortable_column_header(attribute)
    html << '</th>'
  end
  html << %{
    <th>View</th>
    <th>Edit</th>
    <th>Delete</th>
    </tr>
  }
  collection.each do |instance|
    html << %{<tr id="#{dom_id(instance)}" #{alt_rows}>}
    attributes.each_with_index do |attribute, i|
      raw_value = instance.send(attribute)
      value = case model_column_types[attribute]
      when :boolean
        yes?(raw_value)
      when :text
        truncate_words(raw_value, 10, ". . . #{link_to('More', send("#{model_instance_name}_path", instance))}")
      else
        h(raw_value)
      end
      if i == 0
        html << %{<td class="first_col">#{link_to(value, send("#{model_instance_name}_path", instance))}</td>}
      else
        html << %{<td>#{value}</td>}
      end
    end
    html << %{<td>#{link_to(image_tag('admin/icon_show.png'), send("#{model_instance_name}_path", instance))}</td>}
    html << %{<td>#{link_to(image_tag('admin/icon_edit.png'), send("edit_#{model_instance_name}_path", instance))}</td>}
    html << %{<td>#{link_to(image_tag('admin/icon_destroy.png'), send("#{model_instance_name}_path", instance), :confirm => 'Are you sure?', :method => :delete)}</td>}
    html << '</tr>'
  end
  html << '</table>'
end