class HtmlTable
Manage HTML table
Constants
- DEFAULT_COLOR
Constants
- DEFAULT_PAGINATION_OPTIONS
Attributes
attributes[RW]
Virtual attributes
base_url[RW]
Virtual attributes
pagination_options[RW]
Virtual attributes
primary_color[RW]
Virtual attributes
resources[RW]
Virtual attributes
resources_class[RW]
Virtual attributes
resources_count[RW]
Virtual attributes
secondary_color[RW]
Virtual attributes
show_resource_link[RW]
Virtual attributes
table_id[RW]
Virtual attributes
Public Class Methods
new(resources_class, resources, attributes, params = {})
click to toggle source
Initialize class
# File lib/html_table.rb, line 26 def initialize(resources_class, resources, attributes, params = {}) @attributes = attributes&.to_a @resources = resources&.to_a @resources_class = resources_class @resources_count = resources.size @base_url = params[:base_url] @table_id = params[:table_id] || "table#{SecureRandom.hex}" @show_resource_link = params[:show_resource_link] != false && resources_class.method_defined?('id') @pagination_options = params[:pagination_options] || DEFAULT_PAGINATION_OPTIONS @primary_color = params[:primary_color] || DEFAULT_COLOR @secondary_color = params[:secondary_color] || DEFAULT_COLOR end
Public Instance Methods
generate!()
click to toggle source
Generate HTML table
# File lib/html_table.rb, line 42 def generate! return '' if invalid_data complete_css + complete_div + complete_js end
Private Instance Methods
body_cell_value(resource, attribute)
click to toggle source
Body cell value
# File lib/html_table.rb, line 157 def body_cell_value(resource, attribute) return '-' if resource.blank? || attribute.blank? result = if resource.respond_to?('locale_value') resource.send('locale_value', attribute) else resource.send(attribute) end result.present? ? result.to_s : '-' end
complete_css()
click to toggle source
Complete table css
# File lib/html_table.rb, line 53 def complete_css "<style>#{css_file}</style>" end
complete_div()
click to toggle source
Complete table divs
# File lib/html_table.rb, line 60 def complete_div "<div class='table-wrapper'> #{pagination_header} <table class='fl-table' id='#{table_id}'> <thead> <tr>#{table_head}</tr> </thead> <tbody> #{table_body} <tbody> </table> </div> #{pagination_footer}" end
complete_js()
click to toggle source
Complete table JS
# File lib/html_table.rb, line 78 def complete_js "<script>#{js_file}</script>" end
css_file()
click to toggle source
Parsed CSS file
# File lib/html_table.rb, line 191 def css_file File.open("#{__dir__}/assets/css/table_template.css") .read .gsub('/*PRIMARY_COLOR*/', "background: #{primary_color};") .gsub('/*SECONDARY_COLOR*/', "background: #{secondary_color};") end
head_cell_value(attribute)
click to toggle source
Head cell value
# File lib/html_table.rb, line 127 def head_cell_value(attribute) return '-' if attribute.blank? result = if resources_class.respond_to?('locale_key') resources_class.locale_key(attribute.to_s) else attribute end (result.present? ? result : '-').to_s.titleize end
invalid_data()
click to toggle source
Check if all parameters is valid
# File lib/html_table.rb, line 212 def invalid_data if !resources.is_a?(Array) || !attributes.is_a?(Array) true elsif [resources_class, resources, attributes, table_id].any?(&:blank?) || attributes.any?(&:blank?) true end false end
js_file()
click to toggle source
Parsed JS file
# File lib/html_table.rb, line 201 def js_file js_file = File.open("#{__dir__}/assets/js/table_template.js").read js_file.gsub('table-id', table_id) .gsub('pagination', "#{table_id}pagination") .gsub('maxRows', "#{table_id}maxRows") .gsub('5000', resources_count.to_s) end
pagination_header()
click to toggle source
Pagination header
# File lib/html_table.rb, line 85 def pagination_header "<div class='pagination_header'>Registros por página: <select name='state' id='#{table_id}maxRows'> <option value='#{resources_count}'>Mostrar Todos</option> #{ pagination_options.map do |pagination_option| "<option value='#{pagination_option}'>#{pagination_option}</option>" end } </select> </div>" end
resource_link(resource, size = 0.4)
click to toggle source
Generate link to resource
# File lib/html_table.rb, line 171 def resource_link(resource, size = 0.4) block = String.new block << "<td width='#{146 * size}px'>" block << "<a href='#{base_url}/admin/#{resource.class.name.underscore}/#{resource.id}' target='_blank'>" block << "<img width='#{73 * size}px' height='#{43 * size}px' src='#{resource_link_icon}'>" block << '</a></td>' block end
resource_link_icon()
click to toggle source
Open show icon
# File lib/html_table.rb, line 184 def resource_link_icon File.open("#{__dir__}/assets/icons/show.icon").read end
table_body()
click to toggle source
Table body
# File lib/html_table.rb, line 141 def table_body block = String.new resources.each do |resource| block << '<tr>' attributes.each do |attribute| block << '<td>' << body_cell_value(resource, attribute) << '</td>' end block << resource_link(resource) if show_resource_link block << '</tr>' end block end
table_head()
click to toggle source
Table head
# File lib/html_table.rb, line 115 def table_head block = String.new attributes.each do |attribute| block << '<th>' << head_cell_value(attribute) << '</th>' end block << "<th id='#{attributes.size.odd? ? 'fl-table-odd' : 'fl-table-even'}'></th>" if show_resource_link block end