Module: NiceAdminHelper

Defined in:
app/helpers/nice_admin_helper.rb

Instance Method Summary collapse

Instance Method Details

#nice_admin_table_for(model, hash) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/nice_admin_helper.rb', line 3

def nice_admin_table_for(model, hash)
  string = ""
  if model.all.count > 0

    # The following renders the table header
    string << "<table id='#{model.klass.to_s.tableize}'><thead><tr>"
    
    hash[:fields].each do |header|
      string << "<th>#{header.to_s.titlecase}</th>"
    end
    
    hash[:links].each do |blank_header|
      string << "<th></th>"
    end
    
    string << "</tr></thead><tbody>"
    
    for model_item in model.all
      string << "<tr>"
        hash[:fields].each do |field|
          attribute = model_item.try(field)
          if attribute.class == ActiveSupport::TimeWithZone
            string << "<td>#{attribute.to_s(:long)}</td>"
          elsif attribute.class == FalseClass
            string << "<td>#{attribute ? "Yes" : "No"}</td>"
          else
            string << "<td>#{attribute}</td>"
          end
        end
        
        hash[:links].each do |link|
          link_array = []
          if hash[:namespace] != nil
              link_array << hash[:namespace]
              link_array << model_item
              
          else
            link_array << model_item
          end
          
          link_array.flatten!

          case link.to_s
          when 'edit'
           edit_array = [:edit]
           string << "<td>#{link_to "Edit", edit_array + link_array}</td>" 
          when 'show'
            string << "<td>#{link_to "View", link_array}</td>"
          when 'destroy'
            string << "<td>#{link_to "Delete", link_array, :method => :delete, :confirm => "Are you sure?"}</td>"
          else
            begin
              string << "<td>#{link_to link.to_s.humanize, link_array + [link]}</td>" 
            rescue
               string << "<td>#{link_to link.to_s.humanize, [link] + link_array }</td>" 
            end
          end
        end
  
      string << "</tr>"
    end

    "</tbody>
  </table>"
  else
    string << "<p class='blank_table_text'>There are no #{model.klass.to_s.tableize.gsub('_', ' ')} to display</p>"
  end

  begin
    string << paginate(model)
  rescue
  end
  
  return raw string
end