Sha256: 0148b3ab0b94841232edf43b4bb2f93b66f7b6b1ecd18d994a2778ccc460fb49

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 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
              string << "<td>#{link_to link.to_s.humanize, link_array + [link]}</td>" 
            end
          end
    
        string << "</tr>"
      end

      "</tbody>
    </table>"
    else
      string << "<p>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

1 entries across 1 versions & 1 rubygems

Version Path
nice_admin-0.0.2 app/helpers/nice_admin_helper.rb