public
Created

DataListHelper

  • Download Gist
data_list_helper.rb
Ruby
1 2 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
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_helper_use.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<%= 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 %>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.