html_tables 0.0.3 → 0.0.4

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.0.4
2
+ -----
3
+ * Added support for groups.
4
+
1
5
  0.0.2
2
6
  -----
3
7
  * Added `to_label` method for `NilClass` and `ActiveRecord::Base`.
data/README.md CHANGED
@@ -21,6 +21,8 @@ Or install it yourself as:
21
21
  HAML example:
22
22
 
23
23
  = data_table_for(@products, name: :products_table) do |t|
24
+ - t.group_by(:category) do |cat|
25
+ %em= cat.name
24
26
  - t.column(:code, align: :center, width: '9em')
25
27
  - t.column(:description) { |p| p.descriptions.first }
26
28
  - t.column(:pricing)
@@ -72,6 +72,18 @@ module HtmlTables
72
72
  h
73
73
  end
74
74
 
75
+ def group_by(column_or_lambda, &block)
76
+ options[:group] = { block: block, proc: case column_or_lambda
77
+ when String, Symbol
78
+ ->(obj) { obj.public_send(column_or_lambda) }
79
+ when Proc
80
+ column_or_lambda
81
+ else
82
+ raise ArgumentError.new "group_by first argument must be a String, Symbol or Proc"
83
+ end }
84
+ self
85
+ end
86
+
75
87
  def test(item)
76
88
  yield item
77
89
  end
@@ -35,13 +35,18 @@ module HtmlTables
35
35
  end +
36
36
  content_tag(:tbody) do
37
37
  b = ''.html_safe
38
- if collection.each do |item|
39
- b << content_tag(:tr, t.row_options_for(item)) do
40
- t.columns.map do |name, opts|
41
- render_td(item, name, opts)
42
- end.join.html_safe
38
+ rows = if options[:group]
39
+ collection.group_by {|i| options[:group][:proc].call(i) }.each do |g, l|
40
+ b << content_tag(:tr, class: 'group') do
41
+ content_tag(:th, capture(g, &options[:group][:block]), colspan: t.columns.size)
42
+ end
43
+ render_data_rows(b, l, t)
43
44
  end
44
- end.size == 0 then
45
+ collection
46
+ else
47
+ render_data_rows(b, collection, t)
48
+ end
49
+ if rows.size == 0 then
45
50
  b << content_tag(:tr, class: 'nodata') do
46
51
  content_tag(:td, colspan: t.columns.size) { t.nodata_message }
47
52
  end unless t.nodata_message.nil?
@@ -51,6 +56,16 @@ module HtmlTables
51
56
  end
52
57
  end
53
58
 
59
+ def render_data_rows(b, collection, t)
60
+ collection.each do |item|
61
+ b << content_tag(:tr, t.row_options_for(item)) do
62
+ t.columns.map do |name, opts|
63
+ render_td(item, name, opts)
64
+ end.join.html_safe
65
+ end
66
+ end
67
+ end
68
+
54
69
  def render_td(item, column, opts)
55
70
  td_options = {}
56
71
 
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module HtmlTables
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
@@ -27,6 +27,11 @@ module HtmlTables
27
27
  nil
28
28
  end
29
29
 
30
+ def group_by(column_or_lambda, &block)
31
+ t.group_by column_or_lambda, &block
32
+ self
33
+ end
34
+
30
35
  # Sets the 'no-data' message. If not set, no message will be displayed when there's no records to show.
31
36
  def nodata(msg)
32
37
  t.nodata_message = msg
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-16 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n