Sha256: 25883542c556d7e3a0061c0a4873d26c9dc03f1adb6c3e09024790a2419c98fc

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

module NiceAdminHelper

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

      # The following renders the table header
      string << "<table><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>"
            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 '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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nice_admin-0.0.4 app/helpers/nice_admin_helper.rb
nice_admin-0.0.3 app/helpers/nice_admin_helper.rb