Skip to content

Instantly share code, notes, and snippets.

@nazarhussain
Created April 6, 2011 07:11
Show Gist options
  • Save nazarhussain/905258 to your computer and use it in GitHub Desktop.
Save nazarhussain/905258 to your computer and use it in GitHub Desktop.
DataListHelper
module DataListHelper
def list_headers(args=[])
args = Array.new(args)
columns = []
args.map { |o| columns << content_tag(:li, o.split(":").first, :style=>"width:#{o.split(":").second}px;") }
content_tag(:ul, columns.join(" ").html_safe, :class=>"list-headers")
end
def data_list_for(object, headers=[], &block)
if object.is_a? Array
if object.length == 0
list_headers(headers).concat(content_tag(:strong, "<br />No records found".html_safe))
else
arr = []
object.each do |o|
arr << content_tag(:li, content_tag(:ul, capture(DataListBuilder.new(o), &block), :id=>o.id, :class=>"list-row #{cycle('odd', 'even')}"))
end
data_list_total_records(object).concat(content_tag(:ol, content_tag(:li, list_headers(headers)) + arr.join().html_safe, :class=>"data-list")).concat(data_list_pagination(object)).html_safe
end
else
list_headers(headers).concat(content_tag(:strong, " <br />Not available."))
end
end
def data_list_pagination(array)
will_paginate array
end
def data_list_total_records(array)
content_tag(:div, page_entries_info(array).html_safe, :class=>"total-records")
end
class DataListBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::UrlHelper
attr_accessor :object, :output_buffer
def initialize(object)
@object, @output_buffer = object, nil
end
def column (&block)
if block_given?
content_tag(:li, block.call(self))
else
content_tag(:li, "")
end
end
def options_column(&link_block)
if block_given?
content_tag(:li, content_tag(:dl, "<dt><a href='#'>&nbsp;</a></dt><dd><ul>#{link_block.call(self)}</ul></dd>".html_safe, :class=>'options'))
else
content_tag(:li, "")
end
end
def link_item(title, url, options={})
content_tag :li, link_to(title, url, options)
end
end
end
<%= data_list_for @leads, [" :10", "Age:30", "Contact:140", "Phone:140", "Email:180", "Company:100", ""] do |l| %>
<%= l.column { |c| link_to "&nbsp;".html_safe, "leads/details/#{c.object.id}", :class=>:plus, :remote=>true } %>
<%= l.column { |c| c.object.age } %>
<%= l.column { |c| c.object.contact.complete_name } %>
<%= l.column { |c| c.object.contact.phones.blank? ? "-" : c.object.contact.phones.first.phone_number } %>
<%= l.column { |c| c.object.contact.emails.blank? ? "-" : c.object.contact.emails.first.email } %>
<%= l.column { |c| c.object.company.title } %>
<%= l.options_column do |c| %>
<%= c.link_item 'Show', lead_path(c.object) %>
<%= c.link_item 'Edit', edit_lead_path(c.object) %>
<%= c.link_item 'New Note', "leads/#{c.object.id}/notes/new", :class=>"display-newxdoc", :id=>c.object.id %>
<%= c.link_item 'Create Opportunity', new_lead_opportunity_path(c.object) %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment