首先,我们重构了您的助手,以便更频繁地使用content_tag
(只是为了了解这段代码中发生了什么^_^)。
接下来我们添加使用output_buffer
它在 helper 中被定义但根本没有使用。
在它之后应该调用的所有方法erb
应该返回nil
所以它们没有出现在 HTML 中。
最后一个语法糖是使用instance_eval
所以你不需要{|c| ...}
样式块。您可以使用以下所有变量DataListBuilder
直接在那里。
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_total_records(array)
content_tag(:div, page_entries_info(array).html_safe, :class=>"total-records")
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
res_obj = data_list_total_records(object)
res_obj << content_tag(:ol, :class=>"data-list") do
res_ol = content_tag(:li) do
res = list_headers(headers)
object.each do |o|
builder = DataListBuilder.new(o)
res << content_tag(:li) do
content_tag(:ul, :id=>o.id, :class=>"list-row #{cycle('odd', 'even')}") do
capture(builder, &block)
builder.output_buffer.html_safe
end
end
end
res
end
res_ol << data_list_pagination(object)
end
res_obj
end
else
list_headers(headers).concat(content_tag(:strong, " <br />Not available."))
end
end
class DataListBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::UrlHelper
include Rails.application.routes.url_helpers
attr_accessor :object, :output_buffer, :controller
def initialize(object)
@object, @output_buffer = object, ''
end
def column (&block)
@output_buffer << if block_given?
content_tag(:li, instance_eval(&block))
else
content_tag(:li, "")
end
nil
end
def options_column(&link_block)
@output_buffer << if block_given?
content_tag(:li) do
content_tag(:dl, :class=>'options') do
res = content_tag(:dt) do
content_tag(:a, ' '.html_safe, :href => '#')
end
res << content_tag(:dd) do
content_tag(:ul) do
instance_eval &link_block
end
end
end
end
else
content_tag(:li, "")
end
nil
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 { link_to " ".html_safe, "leads/details/#{object.id}", :class=>:plus, :remote=>true } %>
<%= l.column { object.age } %>
<%= l.column { object.contact.complete_name } %>
<%= l.column { object.contact.phones.blank? ? "-" : object.contact.phones.first.phone_number } %>
<%= l.column { object.contact.emails.blank? ? "-" : object.contact.emails.first.email } %>
<%= l.column { object.company.title } %>
<%= l.options_column do %>
<%= link_item 'Show', lead_path(object.id) %>
<%= link_item 'Edit', edit_lead_path(object.id) %>
<%= link_item 'New Note', "leads/#{object.id}/notes/new", :class=>"display-newxdoc", :id=>object.id %>
<%= link_item 'Create Opportunity', new_lead_opportunity_path(object.id) %>
<% end %>
<% end %>