class Autotable::Builder
Public Class Methods
new(collection, template, options)
click to toggle source
# File lib/autotable/builder.rb, line 5 def initialize(collection, template, options) @collection, @template = collection, template @options = DEFAULT_OPTIONS.merge(options) @columns, @actions = [], [] end
Public Instance Methods
action(title, options = {})
click to toggle source
# File lib/autotable/builder.rb, line 26 def action(title, options = {}) options[:title] = title @actions << options end
column(method, title = nil, &formatter)
click to toggle source
# File lib/autotable/builder.rb, line 11 def column(method, title = nil, &formatter) if method.is_a?(String) title = method method = nil end @columns << { :method => method, :title => title || method.to_s.humanize, :formatter => formatter } nil end
to_html()
click to toggle source
# File lib/autotable/builder.rb, line 31 def to_html html = "<table class='#{table_class}'>\n" html << header_html html << "<tbody>\n" if @collection.empty? html << "<tr class='warning'>\n" html << "<td colspan='#{@columns.size + 1}'>No items found</td>\n" html << "</tr>\n" else @collection.each { |c| html << row_html(c) } end html << "</tbody>\n" html << "</table>\n" html.html_safe end
Private Instance Methods
h(html)
click to toggle source
# File lib/autotable/builder.rb, line 50 def h(html) ::CGI.escapeHTML(html) end
header_html()
click to toggle source
# File lib/autotable/builder.rb, line 63 def header_html html = "<thead>\n" html << "<tr>\n" @columns.each do |c| html << "<th>#{h(c[:title])}</th>\n" end html << "<th class='actions'>Actions</th>\n" html << "</tr>\n" html << "</thead>\n" html end
row_html(object)
click to toggle source
# File lib/autotable/builder.rb, line 77 def row_html(object) html = "<tr>\n" @columns.each do |col| value = col[:method] ? object.send(col[:method]) : nil if col[:formatter] value = col[:formatter].call(value,object) || '' else value = h(value || '') end html << "<td>#{value}</td>\n" end actions = @actions.map { |a| text = a[:title] if a[:icon] text = "<span class='glyphicon glyphicon-#{a[:icon]}'></span> #{text}" end if a[:url].is_a?(String) url = a[:url] elsif a[:url].is_a?(Proc) url = a[:url].call(object) elsif a[:url].is_a?(Hash) url = @template.url_for(a[:url].merge(:id => object)) elsif a[:url].is_a?(Symbol) url = @template.send(a[:url], object) else url = '#' end action_class = @options[:action_class] + ' ' action_class << (a[:class] || 'btn-default') link_options = a.slice(:confirm, :method, :remote, :data) link_options[:class] = action_class @template.link_to(text.html_safe, url, link_options) }.join(' ') html << "<td class='actions'>#{actions}</td>" html << "</tr>\n" end
table_class()
click to toggle source
# File lib/autotable/builder.rb, line 54 def table_class klass = 'table' klass << ' table-striped' if @options[:striped] klass << ' table-bordered' if @options[:bordered] klass << ' table-hover' if @options[:hover] klass << ' table-condensed' if @options[:condensed] klass end