html_tables 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e0bae9b4cbf7907485ab3be3a13d8ca01682a9b
4
- data.tar.gz: e525363a740bf4af8a7e8b865a4c3a37ae1c3d0a
3
+ metadata.gz: 2b67f1b4e5bf53dd37f9abd90e0263ba0fa4360c
4
+ data.tar.gz: c9466435747a950ed58afacc6555b83c94d64cb4
5
5
  SHA512:
6
- metadata.gz: 5f69ca9e6d5df648a5bbfe1482424714d6971364cb050afa59f106275ae107201f893d5334c741c126eacda5375aa6022594fa833df34d3a11a2e985b49d44c9
7
- data.tar.gz: c8af413fa4c711a9e16f235392ce79d0b72f2633f45f9014003e0d7bbcc98e928d7a2af3897107ac8cc973262d8f3ad4a6c497a50bf4b7a7192afd25b9c10e67
6
+ metadata.gz: 4f4a0899ab44d8f12dfb971d60e221997bb0e247f819e56afa9912b54915c808222f799a4149396d1c229f6aa68a808778617dbbf2f2732476b299030298cdc7
7
+ data.tar.gz: db2503a835c7d40c4bd1e10290734b227df2782e7873cc23511e7d7435195ec9e831f349d039fd80b458727d2bf1baa608334e4dc3e38077bcff71077d38fdfd
data/README.md CHANGED
@@ -18,21 +18,24 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- HAML example:
21
+ Slim example:
22
22
 
23
23
  = data_table_for(@products, name: :products_table, html: { class: 'custom_table' }) do |t|
24
24
  - t.group_by(:category) do |cat|
25
- %em= cat.name
25
+ em= cat.name
26
26
  - t.column(:code, align: :center, width: '9em')
27
27
  - t.column(:description) { |p| p.descriptions.first }
28
- - t.column(:pricing)
28
+ - t.column(:pricing, footer: :sum) do |item, total_in_footer|
29
+ - if item
30
+ = number_to_currency(item.pricing)
31
+ - else
32
+ blink = number_to_currency(total_in_footer)
29
33
  - t.column(:quantity, width: '9em') do |p|
30
34
  - if p.quantity == 0
31
- %span.label.label-important.stack-right(title = 'Out of stock')
32
- %i.icon-flag.icon-white
35
+ span title='Out of stock' *
33
36
  - else
34
- .c= number_with_delimiter p.quantity
35
- - t.column(:situation, align: :center, width: '9em')
37
+ = number_with_delimiter p.quantity
38
+ - t.column(:status, align: :center, width: '3em', header: { text: 'St.', class: 'status-header' })
36
39
  - t.nodata 'No product found.'
37
40
 
38
41
  ## Contributing
data/html_tables.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
  require File.expand_path('../lib/html_tables/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Fábio David Batista"]
6
- gem.email = ["fabio@elementarsistemas.com.br"]
5
+ gem.authors = ['Fábio David Batista']
6
+ gem.email = ['fabio@elementarsistemas.com.br']
7
7
  gem.description = %q{Simple DSL for HTML data tables}
8
8
  gem.summary = %q{This gem was extracted from some projects on my company. Everyone is welcome to use and improve upon.}
9
- gem.homepage = "http://elementarsistemas.com.br/"
9
+ gem.homepage = 'http://elementarsistemas.com.br/'
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
13
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "html_tables"
15
- gem.require_paths = ["lib"]
14
+ gem.name = 'html_tables'
15
+ gem.require_paths = ['lib']
16
16
  gem.version = HtmlTables::VERSION
17
17
 
18
18
  gem.add_dependency 'i18n'
@@ -49,7 +49,6 @@ module HtmlTables
49
49
 
50
50
  def header_for(column_id)
51
51
  return nil if column_id.nil?
52
- return columns[column_id][:header] if columns[column_id][:header]
53
52
  return @builder.content_tag(:i, nil, class: 'icon-check') if columns[column_id][:checkbox]
54
53
  v ||= I18n.t(column_id, scope: [:tables, options[:name] || :default], raise: true) rescue nil
55
54
  v ||= I18n.t(column_id, scope: [:tables, :default], raise: true) rescue nil
@@ -5,124 +5,14 @@ module HtmlTables
5
5
  def data_table_for(collection, options = {})
6
6
  config = HtmlTables::Configuration.instance
7
7
 
8
- t = DataTable.new(self, collection, options)
8
+ table = DataTable.new(self, collection, options)
9
9
  if block_given?
10
- yield t.object_to_yield
10
+ yield table.object_to_yield
11
11
  else
12
- t.auto_generate_columns!
12
+ table.auto_generate_columns!
13
13
  end
14
14
 
15
- cls = config.default_table_classes.dup
16
- cls << 'table-condensed' if options[:condensed]
17
- cls << options[:class] if options[:class]
18
- table_html_options = { class: cls }
19
- table_html_options.merge!(options[:html]) { |_, v1, v2| [v1, v2].flatten } if options[:html]
20
- content_tag(:table, table_html_options) do
21
- content_tag(:caption, options[:caption] || controller_name, class: ('hidden' unless options[:caption])) +
22
- content_tag(:colgroup) do
23
- b = ''.html_safe
24
- t.columns.each do |_, opts|
25
- col_opts = { }
26
- col_opts[:style] = "width: #{opts[:width]}" if opts[:width]
27
- b << content_tag(:col, col_opts) { }
28
- end
29
- b
30
- end +
31
- content_tag(:thead) do
32
- content_tag(:tr) do
33
- t.columns.map do |name, opts|
34
- header_opts = {}
35
- header_opts[:class] = 'check' if opts[:checkbox]
36
- header_opts[:header_title] = opts[:header_title] if opts[:header_title]
37
- content_tag(:th, t.header_for(name), header_opts)
38
- end.join.html_safe
39
- end
40
- end +
41
- content_tag(:tbody) do
42
- b = ''.html_safe
43
- rows = if options[:group]
44
- collection.group_by {|i| options[:group][:proc].call(i) }.each do |g, l|
45
- b << content_tag(:tr, class: 'group') do
46
- content_tag(:th, capture(g, &options[:group][:block]), colspan: t.columns.size)
47
- end
48
- render_data_rows(b, l, t)
49
- end
50
- collection
51
- else
52
- render_data_rows(b, collection, t)
53
- end
54
- if rows.size == 0 then
55
- b << content_tag(:tr, class: 'nodata') do
56
- content_tag(:td, colspan: t.columns.size) { t.nodata_message }
57
- end unless t.nodata_message.nil?
58
- end
59
- b
60
- end
61
- end
62
- end
63
-
64
- def render_data_rows(b, collection, t)
65
- collection.each do |item|
66
- b << content_tag(:tr, t.row_options_for(item)) do
67
- t.columns.map do |name, opts|
68
- render_td(item, name, opts)
69
- end.join.html_safe
70
- end
71
- end
72
- end
73
-
74
- def render_td(item, column, opts)
75
- td_options = {}
76
-
77
- if opts[:align] == :center
78
- td_options[:class] = 'c'
79
- end
80
-
81
- if opts[:title]
82
- td_options[:title] = if opts[:title].respond_to?(:call)
83
- opts[:title].call(item)
84
- else
85
- opts[:title].to_s
86
- end
87
- end
88
-
89
- v = if opts[:checkbox]
90
- checked = opts[:checked]
91
- checked = opts[:block].call(item) if opts[:block]
92
- check_box_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id)), checked, extract_check_box_tag_options(opts)
93
- elsif opts[:radio]
94
- radio_button_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id))
95
- elsif opts[:block]
96
- capture(item, &opts[:block])
97
- else
98
- tmp = item.public_send(column)
99
- tmp = item.public_send("#{column}_text") rescue tmp if tmp.is_a?(Symbol)
100
- tmp
101
- end
102
-
103
- if config.use_entity_shortcuts && v.is_a?(ActiveRecord::Base)
104
- btn = content_tag(:div, class: 'entity-shortcut') do
105
- link_to(config.url_generator_proc.(v), class: 'btn btn-small') do
106
- content_tag(:i, nil, class: 'icon-share')
107
- end
108
- end rescue nil
109
- end
110
-
111
- v = if v.is_a?(Enumerable)
112
- v.inject(''.html_safe) { |b, i| b << ', ' unless b.blank?; b << i.format_for_output }
113
- else
114
- v.format_for_output
115
- end
116
- v = v.to_s unless v.nil? || v.is_a?(String)
117
-
118
- v = ''.html_safe << btn << v if btn
119
-
120
- content_tag(:td, v, td_options)
121
- end
122
-
123
- def extract_check_box_tag_options(opts)
124
- valid_options = [:disabled]
125
- opts.select { |k, _| valid_options.include?(k) }
15
+ Renderer.new(self, table, collection, options).to_html
126
16
  end
127
17
  end
128
18
  end
@@ -0,0 +1,178 @@
1
+ module HtmlTables
2
+ class Renderer
3
+ def initialize(helper, table, collection, options = {})
4
+ @helper = helper
5
+ @t = table
6
+ @collection = collection
7
+ @options = options
8
+ @config = HtmlTables::Configuration.instance
9
+ end
10
+
11
+ def to_html
12
+ cls = config.default_table_classes.dup
13
+ cls << 'table-condensed' if options[:condensed]
14
+ cls << options[:class] if options[:class]
15
+ table_html_options = { class: cls }
16
+ table_html_options.merge!(options[:html]) { |_, v1, v2| [v1, v2].flatten } if options[:html]
17
+
18
+ content_tag(:table, table_html_options) do
19
+ content = ''.html_safe
20
+ content << caption << colgroup << thead << tfoot << tbody
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :t, :collection, :options, :config
27
+ delegate :content_tag, :check_box_tag, :capture, to: :@helper
28
+
29
+ def caption
30
+ return ''.html_safe unless options.has_key?(:caption)
31
+ content_tag(:caption, options[:caption])
32
+ end
33
+
34
+ def colgroup
35
+ content_tag(:colgroup) do
36
+ b = ''.html_safe
37
+ t.columns.each do |_, opts|
38
+ col_opts = { }
39
+ col_opts[:style] = "width: #{opts[:width]}" if opts[:width]
40
+ b << content_tag(:col, col_opts) { }
41
+ end
42
+ b
43
+ end
44
+ end
45
+
46
+ def thead
47
+ content_tag(:thead) do
48
+ content_tag(:tr) do
49
+ t.columns.map do |name, opts|
50
+ header_opts = opts.fetch(:header, {})
51
+ header_opts = { text: header_opts } unless header_opts.is_a?(Hash)
52
+ header_opts[:text] ||= opts[:header_title] if opts[:header_title]
53
+ (header_opts[:class] ||= '') << ' check' if opts[:checkbox]
54
+
55
+ header_text = header_opts.delete(:text) { t.header_for(name) }
56
+ content_tag(:th, header_text, header_opts)
57
+ end.join.html_safe
58
+ end
59
+ end
60
+ end
61
+
62
+ def tfoot
63
+ return ''.html_safe unless t.columns.any? { |_, c| c.has_key?(:footer) }
64
+
65
+ content_tag(:tfoot) do
66
+ content_tag(:tr) do
67
+ t.columns.map do |id, opts|
68
+ if opts.has_key?(:footer)
69
+ value =
70
+ case opts[:footer]
71
+ when Symbol
72
+ collection.map(&id.to_proc).public_send(opts[:footer])
73
+ when Proc
74
+ opts[:footer].call(collection)
75
+ end
76
+ content = opts[:block] ? capture(nil, value, &opts[:block]) : value.to_s
77
+ content_tag(:th, content, extract_td_options(opts, nil))
78
+ else
79
+ content_tag(:th)
80
+ end
81
+ end.join.html_safe
82
+ end
83
+ end
84
+ end
85
+
86
+ def tbody
87
+ content_tag(:tbody) do
88
+ b = ''.html_safe
89
+ rows = if options[:group]
90
+ collection.group_by {|i| options[:group][:proc].call(i) }.each do |g, l|
91
+ b << content_tag(:tr, class: 'group') do
92
+ content_tag(:th, capture(g, &options[:group][:block]), colspan: t.columns.size)
93
+ end
94
+ render_data_rows(b, l, t)
95
+ end
96
+ collection
97
+ else
98
+ render_data_rows(b, collection, t)
99
+ end
100
+ b << no_data if rows.size.zero?
101
+ b
102
+ end
103
+ end
104
+
105
+ def no_data
106
+ return '' if t.nodata_message.nil?
107
+ content_tag(:tr, class: 'nodata') do
108
+ content_tag(:td, colspan: t.columns.size) { t.nodata_message }
109
+ end
110
+ end
111
+
112
+ def render_data_rows(b, collection, t)
113
+ collection.each do |item|
114
+ b << content_tag(:tr, t.row_options_for(item)) do
115
+ t.columns.map do |name, opts|
116
+ render_td(item, name, opts)
117
+ end.join.html_safe
118
+ end
119
+ end
120
+ end
121
+
122
+ def render_td(item, column, opts)
123
+ v = if opts[:checkbox]
124
+ checked = opts[:checked]
125
+ checked = opts[:block].call(item) if opts[:block]
126
+ check_box_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id)), checked, extract_check_box_tag_options(opts)
127
+ elsif opts[:radio]
128
+ radio_button_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id))
129
+ elsif opts[:block]
130
+ args = [item, nil].first(opts[:block].arity)
131
+ capture(*args, &opts[:block])
132
+ else
133
+ tmp = item.public_send(column)
134
+ tmp = item.public_send("#{column}_text") rescue tmp if tmp.is_a?(Symbol)
135
+ tmp
136
+ end
137
+
138
+ if config.use_entity_shortcuts && v.is_a?(ActiveRecord::Base)
139
+ btn = content_tag(:div, class: 'entity-shortcut') do
140
+ link_to(config.url_generator_proc.(v), class: 'btn btn-small') do
141
+ content_tag(:i, nil, class: 'icon-share')
142
+ end
143
+ end rescue nil
144
+ end
145
+
146
+ v = if v.is_a?(Enumerable)
147
+ v.inject(''.html_safe) { |b, i| b << ', ' unless b.blank?; b << i.format_for_output }
148
+ else
149
+ v.format_for_output
150
+ end
151
+ v = v.to_s unless v.nil? || v.is_a?(String)
152
+
153
+ v = ''.html_safe << btn << v if btn
154
+
155
+ content_tag(:td, v, extract_td_options(opts, item))
156
+ end
157
+
158
+ def extract_check_box_tag_options(opts)
159
+ valid_options = [:disabled]
160
+ opts.select { |k, _| valid_options.include?(k) }
161
+ end
162
+
163
+ def extract_td_options(opts, item)
164
+ retval = {}
165
+ retval[:class] = 'c' if opts[:align] == :center
166
+
167
+ if opts[:title]
168
+ retval[:title] = if opts[:title].respond_to?(:call)
169
+ opts[:title].call(item).format_for_output
170
+ else
171
+ opts[:title].format_for_output
172
+ end
173
+ end
174
+
175
+ retval
176
+ end
177
+ end
178
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module HtmlTables
4
- VERSION = '0.0.12'
4
+ VERSION = '0.0.13'
5
5
  end
@@ -27,7 +27,7 @@ module HtmlTables
27
27
  end
28
28
 
29
29
  # Adds a regular column to the DataTable.
30
- def column(id, options = { }, &block)
30
+ def column(id, options = {}, &block)
31
31
  options[:block] = block if block_given?
32
32
  t.columns[id] = options
33
33
  nil
data/lib/html_tables.rb CHANGED
@@ -7,5 +7,6 @@ require 'html_tables/to_label'
7
7
  require 'html_tables/format_for_output'
8
8
  require 'html_tables/yielded_object'
9
9
  require 'html_tables/data_table'
10
+ require 'html_tables/renderer'
10
11
  require 'html_tables/data_table_helper'
11
12
  require 'html_tables/railtie' if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fábio David Batista
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-26 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: railties
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Simple DSL for HTML data tables
@@ -73,7 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
76
+ - ".gitignore"
77
77
  - CHANGELOG
78
78
  - Gemfile
79
79
  - LICENSE
@@ -86,6 +86,7 @@ files:
86
86
  - lib/html_tables/data_table_helper.rb
87
87
  - lib/html_tables/format_for_output.rb
88
88
  - lib/html_tables/railtie.rb
89
+ - lib/html_tables/renderer.rb
89
90
  - lib/html_tables/to_label.rb
90
91
  - lib/html_tables/version.rb
91
92
  - lib/html_tables/yielded_object.rb
@@ -98,17 +99,17 @@ require_paths:
98
99
  - lib
99
100
  required_ruby_version: !ruby/object:Gem::Requirement
100
101
  requirements:
101
- - - '>='
102
+ - - ">="
102
103
  - !ruby/object:Gem::Version
103
104
  version: '0'
104
105
  required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  requirements:
106
- - - '>='
107
+ - - ">="
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project:
111
- rubygems_version: 2.0.3
112
+ rubygems_version: 2.2.1
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: This gem was extracted from some projects on my company. Everyone is welcome