Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
This makes Column frozen-string-literal friendly, but Table needs more
work to make that happen.
  • Loading branch information
sixfeetover authored and Ronnie Taylor committed Feb 1, 2017
1 parent 9b56e4a commit 9510c66
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 62 deletions.
19 changes: 9 additions & 10 deletions data-table.gemspec
@@ -1,28 +1,27 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
require "data-table/version"
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'data-table/version'

Gem::Specification.new do |s|
s.name = "data-table"
s.name = 'data-table'
s.version = DataTable::VERSION
s.licenses = ['Nonstandard']
s.authors = ["Steve Erickson", "Jeff Fraser"]
s.email = ["sixfeetover@gmail.com"]
s.homepage = "https://github.com/sixfeetover/data-table"
s.authors = ['Steve Erickson', 'Jeff Fraser']
s.email = ['sixfeetover@gmail.com']
s.homepage = 'https://github.com/sixfeetover/data-table'
s.summary = %(Turn arrays of hashes or models in to an HTML table.)
s.description = %(data-table is a simple gem that provides a DSL for
turning an array of hashes or ActiveRecord objects into an
HTML table.)

s.rubyforge_project = "data-table"
s.rubyforge_project = 'data-table'

s.add_development_dependency 'rake', '~> 11'
s.add_development_dependency 'rake', '~> 12'
s.add_development_dependency 'rspec', '~> 3'
s.add_development_dependency 'guard', '~> 2'
s.add_development_dependency 'guard-rspec', '~> 4'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ["lib"]
s.require_paths = ['lib']
end
9 changes: 5 additions & 4 deletions lib/data-table.rb
@@ -1,7 +1,8 @@
require "data-table/version"
require "data-table/table"
require "data-table/column"
require "data-table/enum"
# frozen_string_literal: true
require 'data-table/version'
require 'data-table/table'
require 'data-table/column'
require 'data-table/enum'

module DataTable
def self.render(collection, &_blk)
Expand Down
26 changes: 13 additions & 13 deletions lib/data-table/column.rb
@@ -1,10 +1,10 @@
# frozen_string_literal: true
module DataTable
class Column

attr_reader :name
attr_accessor :display, :index, :options, :css_class, :attributes

def initialize(name, description = "", opts = {}, &renderer)
def initialize(name, description = '', opts = {}, &renderer)
@name = name
@description = description
@data_type = opts[:data_type]
Expand All @@ -21,34 +21,34 @@ def initialize(name, description = "", opts = {}, &renderer)
def render_cell(cell_data, row = nil, row_index = 0, col_index = 0)
@data_type ||= type(cell_data)

html = ""
if @renderer && row
html << case @renderer.arity
html = []
html << if @renderer && row
case @renderer.arity
when 1 then @renderer.call(cell_data).to_s
when 2 then @renderer.call(cell_data, row).to_s
when 3 then @renderer.call(cell_data, row, row_index).to_s
when 4 then @renderer.call(cell_data, row, row_index, self).to_s
when 5 then @renderer.call(cell_data, row, row_index, self, col_index).to_s
end
else
html << cell_data.to_s
end
else
cell_data.to_s
end

html << "</td>"
html << '</td>'
# Doing this here b/c you can't change @css_class if this is done before the renderer is called
html = "<td class='#{css_class_names}' #{custom_attributes}>" + html
"<td class='#{css_class_names}' #{custom_attributes}>" + html.join
end

def render_column_header
header = "<th class='#{css_class_names}' #{custom_attributes}"
header = ["<th class='#{css_class_names}' #{custom_attributes}"]
header << "title='#{@help_text}' " if @help_text
header << "style='width: #{@width}'" if @width
header << ">#{@description}</th>"
header
header.join
end

def custom_attributes
@attributes.map{|k, v| "#{k}='#{v}'"}.join ' '
@attributes.map { |k, v| "#{k}='#{v}'" }.join ' '
end

def css_class_names
Expand Down
74 changes: 39 additions & 35 deletions lib/data-table/table.rb
Expand Up @@ -48,15 +48,15 @@ def default_options!
@display_header = true
@alternate_rows = true
@alternate_cols = false
@subtotal_title = "Subtotal:"
@total_title = "Total:"
@subtotal_title = 'Subtotal:'
@total_title = 'Total:'
@repeat_headers_for_groups = false
@custom_headers = []
@row_attributes = nil
end

# Define a new column for the table
def column(id, title = "", opts = {}, &b)
def column(id, title = '', opts = {}, &b)
@columns << DataTable::Column.new(id, title, opts, &b)
end

Expand Down Expand Up @@ -84,27 +84,27 @@ def render_data_table
else
html << "<tr><td class='empty_data_table' colspan='#{@columns.size}'>#{@empty_text}</td></tr>"
end
html << "</table>"
html << '</table>'
end

def render_data_table_header
html = "<thead>"
html = '<thead>'

html << render_custom_table_header unless @custom_headers.empty?

html << "<tr>"
html << '<tr>'
@columns.each do |col|
html << col.render_column_header
end
html << "</tr></thead>"
html << '</tr></thead>'
end

def render_custom_table_header
html = "<tr class='custom-header'>"
@custom_headers.each do |h|
html << "<th class=\"#{h[:css]}\" colspan=\"#{h[:colspan]}\">#{h[:text]}</th>"
end
html << "</tr>"
html << '</tr>'
end

def render_data_table_body(collection)
Expand All @@ -116,9 +116,9 @@ def render_data_table_body(collection)
end

def render_rows(collection)
html = ""
html = ''
collection.each_with_index do |row, row_index|
css_class = @alternate_rows && row_index % 2 == 1 ? 'alt ' : ''
css_class = @alternate_rows && row_index.odd? ? 'alt ' : ''
if @row_style && style = @row_style.call(row, row_index)
css_class << style
end
Expand All @@ -129,19 +129,23 @@ def render_rows(collection)
html
end

def render_row(row, row_index, css_class='', row_attributes={})
if row_attributes.nil?
attributes = ''
else
attributes = row_attributes.map { |attr, val| "#{attr}='#{val}'" }.join " "
end
def render_row(row, row_index, css_class = '', row_attributes = {})
attributes = if row_attributes.nil?
''
else
row_attributes.map { |attr, val| "#{attr}='#{val}'" }.join ' '
end

html = "<tr class='row_#{row_index} #{css_class}' #{attributes}>"
@columns.each_with_index do |col, col_index|
cell = row[col.name] rescue nil
cell = begin
row[col.name]
rescue
nil
end
html << col.render_cell(cell, row, row_index, col_index)
end
html << "</tr>"
html << '</tr>'
end

# define a custom block to be used to determine the css class for a row.
Expand All @@ -168,7 +172,7 @@ def row_attributes(&b)
# TODO: allow for group column only, block only and group column and block
def group_by(group_column, level = {level: 0}, &_blk)
if level.nil? && @groupings.count >= 1
raise "a level designation is required when using multiple groupings."
raise 'a level designation is required when using multiple groupings.'
end
@grouped_data = true
@groupings[level ? level[:level] : 0] = group_column
Expand All @@ -185,19 +189,19 @@ def group_data!
end

def render_grouped_data_table_body(collection)
html = ""
html = ''
collection.keys.each do |group_name|
html << render_group(group_name, collection[group_name])
end
html
end

def render_group_header(group_header, index = nil)
css_classes = ["group_header"]
css_classes = ['group_header']
css_classes << ["level_#{index}"] unless index.nil?
html = "<tr class='#{css_classes.join(' ')}'>"
html << "<th colspan='#{@columns.size}'>#{group_header}</th>"
html << "</tr>"
html << '</tr>'
repeat_headers(html) if @repeat_headers_for_groups
html
end
Expand All @@ -207,7 +211,7 @@ def repeat_headers(html)
@columns.each_with_index do |col, _i|
html << col.render_column_header
end
html << "</tr>"
html << '</tr>'
end

def render_group(group_header, group_data)
Expand All @@ -219,11 +223,11 @@ def render_group(group_header, group_data)
elsif group_data.is_a? Hash
html << render_group_recursive(group_data, 1, group_header)
end
html << "</tbody>"
html << '</tbody>'
end

def render_group_recursive(collection, index = 1, group_parent = nil, ancestors = nil)
html = ""
html = ''
ancestors ||= []
collection.each_pair do |group_name, group_data|
ancestors << group_parent unless ancestors[0] == group_parent
Expand All @@ -247,35 +251,35 @@ def render_group_recursive(collection, index = 1, group_parent = nil, ancestors
# TOTALS AND SUBTOTALS
#############
def render_totals
html = "<tfoot>"
html = '<tfoot>'
@total_calculations.each_with_index do |totals_row, index|
html << "<tr class='total index_#{index}'>"
@columns.each do |col|
value = totals_row[col.name] ||= nil
html << col.render_cell(value)
end
html << "</tr>"
html << '</tr>'
end
html << "</tfoot>"
html << '</tfoot>'
end

def render_parent_subtotals(group_array)
html = ""
html = ''
@parent_subtotals[group_array].each_with_index do |group, index|
html << "<tr class='parent_subtotal "
html << "index_#{index} #{group_array.join('_').gsub(/\s/, '_').downcase}'>"
@columns.each do |col|
value = group[col.name] ? group[col.name].values[0] : nil
html << col.render_cell(value)
end
html << "</tr>"
html << '</tr>'
end
html
end

# ancestors should be an array
def render_subtotals(group_header, _group_data = nil, ancestors = nil)
html = ""
html = ''
path = ancestors.nil? ? [] : ancestors.dup
path << group_header
@subtotal_calculations[path].each_with_index do |group, index|
Expand All @@ -284,13 +288,13 @@ def render_subtotals(group_header, _group_data = nil, ancestors = nil)
value = group[col.name] ? group[col.name].values[0] : nil
html << col.render_cell(value)
end
html << "</tr>"
html << '</tr>'
end
html
end

def subtotal(column_name, function = nil, index = 0, &block)
raise "You must supply an index value" if @subtotals.count >= 1 && index.nil?
raise 'You must supply an index value' if @subtotals.count >= 1 && index.nil?
total_row @subtotals, column_name, function, index, &block
end

Expand All @@ -299,7 +303,7 @@ def subtotals?
end

def total(column_name, function = nil, index = 0, &block)
raise "You must supply an index value" if @totals.count >= 1 && index.nil?
raise 'You must supply an index value' if @totals.count >= 1 && index.nil?
total_row @totals, column_name, function, index, &block
end

Expand Down Expand Up @@ -389,7 +393,7 @@ def calculate_array_and_proc(function, data, column = nil, path = nil)
end
end

def calculate_many(function, data, column_name, path = nil)
def calculate_many(function, data, column_name, _path = nil)
function.each do |func|
if func.is_a? Array
send("calculate_#{func[0]}", data, column_name)
Expand Down

0 comments on commit 9510c66

Please sign in to comment.