easy_table 0.0.9 → 0.0.10

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: 6162558a23ee392fde530fb336a4eacea249cf34
4
- data.tar.gz: 393f00005cb26d539ecb048e587d874265f2e053
3
+ metadata.gz: 13ab471adc9ff8d6f3a1ffaeadb7c488362def91
4
+ data.tar.gz: d4d2c6a3e3984ce4ff5a62f10742442c4876e2a7
5
5
  SHA512:
6
- metadata.gz: 2c69e4ee289a8c17bf46d39e42fe09d9fef23ebff60958022933919bae03a692db443b883f7898eb0bec458258b91efd841418f0b21ad7a8d3ac484c12e733c8
7
- data.tar.gz: 623d48a187589ab8d9b84b5019aa826d6c47ddaeef17da0745c73947054db93e6ad9da904933a4d9ac651d555702e1a245e7ddb7fa5b246265bda6f9a8e2bfa8
6
+ metadata.gz: d5b69ce31b9462e492973e702ab81c32b190311c89ffb34c487759918bc036a93f67cf7da794eb66e2e850667569e7a0f3ca534b8abeb5ee36085f1f741c2a4c
7
+ data.tar.gz: 77f05c9b82a004e3aaf26d3f64f19f11f9e9e26b6e6c3bcf41356cefac923b9ce17b2869d00927737e440e1341015da727bff2fae1b87e6210d9eca3cf00c0d7
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # EasyTable
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/easy_table.svg)](https://badge.fury.io/rb/easy_table)
3
4
  [![Build Status](https://travis-ci.org/cthulhu666/easy_table.png?branch=master)](https://travis-ci.org/cthulhu666/easy_table)
4
5
  [![endorse](https://api.coderwall.com/cthulhu666/endorsecount.png)](https://coderwall.com/cthulhu666)
5
6
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- easy_table (0.0.9)
4
+ easy_table (0.0.10)
5
5
  actionpack (> 4.2)
6
6
  activemodel (> 4.2)
7
7
  rubytree (~> 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- easy_table (0.0.9)
4
+ easy_table (0.0.10)
5
5
  actionpack (> 4.2)
6
6
  activemodel (> 4.2)
7
7
  rubytree (~> 1.0)
@@ -14,10 +14,9 @@ module EasyTable
14
14
  @opts = opts
15
15
  header_opts = @opts.select { |k, _v| k =~ /^header_.*/ }
16
16
  header_opts.each { |k, _v| @opts.delete(k) }
17
- @header_opts = header_opts.inject({}) do |h, e|
17
+ @header_opts = header_opts.each_with_object({}) do |e, h|
18
18
  k, v = *e
19
19
  h[k[7..-1]] = v
20
- h
21
20
  end
22
21
  end
23
22
 
@@ -46,7 +45,7 @@ module EasyTable
46
45
  end
47
46
 
48
47
  def html_opts(record)
49
- @opts.inject({}) do |h, e|
48
+ @opts.each_with_object({}) do |e, h|
50
49
  k, v = *e
51
50
  h[k] = case v
52
51
  when Proc
@@ -54,7 +53,6 @@ module EasyTable
54
53
  else
55
54
  v
56
55
  end
57
- h
58
56
  end
59
57
  end
60
58
  end
@@ -5,17 +5,24 @@ module EasyTable
5
5
 
6
6
  delegate :tag, :content_tag, to: :@template
7
7
 
8
- def initialize(collection, template, options)
9
- @collection = collection
10
- @options = options
11
- tr_opts = @options.select { |k, _v| k =~ /^tr_.*/ }
12
- tr_opts.each { |k, _v| @options.delete(k) }
13
- @tr_opts = tr_opts.inject({}) do |h, e|
14
- k, v = *e
15
- h[k[3..-1]] = v
16
- h
8
+ # NOTE: old versions of ActiveSupport don't have `transform_values!`
9
+ unless Hash.method_defined? :transform_values!
10
+ module M
11
+ refine Hash do
12
+ def transform_values!
13
+ return enum_for(:transform_values!) unless block_given?
14
+ each do |key, value|
15
+ self[key] = yield(value)
16
+ end
17
+ end
18
+ end
17
19
  end
20
+ using M
21
+ end
18
22
 
23
+ def initialize(collection, template, options)
24
+ @collection = collection
25
+ @options, @tr_opts = parse_options(options)
19
26
  @template = template
20
27
  @node = Tree::TreeNode.new('root')
21
28
  end
@@ -39,11 +46,15 @@ module EasyTable
39
46
 
40
47
  private
41
48
 
49
+ def parse_options(options)
50
+ tr_opts = options.select { |k, _v| k =~ /^tr_.*/ }
51
+ [options.except(*tr_opts.keys), tr_opts.transform_keys { |k| k[3..-1] }]
52
+ end
53
+
42
54
  def thead
43
- rows = node.inject([]) do |arr, n|
55
+ rows = node.each_with_object([]) do |n, arr|
44
56
  arr[n.level] ||= []
45
57
  arr[n.level] << n
46
- arr
47
58
  end
48
59
  rows.shift
49
60
  rows.each do |row|
@@ -56,31 +67,25 @@ module EasyTable
56
67
  end
57
68
 
58
69
  def tr_opts(record)
59
- tr_opts = @tr_opts.inject({}) do |h, e|
60
- k, v = *e
61
- h[k] = case v
62
- when Proc
63
- v.call(record)
64
- else
65
- v
66
- end
67
- h
68
- end
70
+ tr_opts = eval_procs(@tr_opts, record)
69
71
 
70
72
  id = "#{record.class.model_name.to_s.parameterize}-#{record.to_param}" if record.class.respond_to?(:model_name)
71
73
  id ||= "#{record.class.name.to_s.parameterize}-#{record.id}" if record.respond_to?(:id)
72
74
 
73
- id.present? ?
74
- tr_opts.merge(id: id) : tr_opts
75
+ id.present? ? tr_opts.merge(id: id) : tr_opts
76
+ end
77
+
78
+ def eval_procs(h, record)
79
+ return h if h.empty?
80
+ procs = h.select { |_k, v| v.is_a?(Proc) }
81
+ .transform_values! { |v| v.call(record) }
82
+ return h if procs.empty?
83
+ h.merge(procs)
75
84
  end
76
85
 
77
86
  def concat(tag)
78
87
  @template.safe_concat(tag)
79
88
  ''
80
89
  end
81
-
82
- def options_from_hash(args)
83
- args.last.is_a?(Hash) ? args.pop : {}
84
- end
85
90
  end
86
91
  end
@@ -1,3 +1,3 @@
1
1
  module EasyTable
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.0.10'.freeze
3
3
  end
@@ -1,56 +1,61 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TableHelperTest < ActionView::TestCase
4
-
5
- context "a table" do
4
+ context 'a table' do
6
5
  setup do
7
- @collection = ['foo', 'bar', 'buzz']
8
- concat(table_for(@collection, class: 'easy') do |t|
9
- t.column :downcase
10
- t.column(:upcase) { |e| e.upcase }
11
- end)
6
+ @collection = %w[foo bar buzz]
7
+ concat(table_for(@collection, class: 'easy', tr_class: 'fun',
8
+ tr_foo: ->(e) { e.length > 3 }) do |t|
9
+ t.column :downcase
10
+ t.column(:upcase) { |e| e.upcase } # rubocop:disable Style/SymbolProc
11
+ end)
12
12
  end
13
13
 
14
14
  should "have 'easy' class" do
15
15
  assert_select 'table.easy'
16
16
  end
17
17
 
18
- should "have 3 rows" do
18
+ should 'have 3 rows' do
19
19
  rows = css_select 'table tbody tr'
20
20
  assert_equal 3, rows.size
21
21
  end
22
22
 
23
- should "have 2 columns" do
23
+ should "have rows with 'fun' class" do
24
+ rows = css_select 'table tbody tr.fun'
25
+ assert_equal 3, rows.size
26
+ end
27
+
28
+ should 'have 2 columns' do
24
29
  cols = css_select 'table tbody tr:first-child td'
25
30
  assert_equal 2, cols.size
26
31
  end
27
32
 
28
- should "have proper headers" do
33
+ should 'have proper headers' do
29
34
  headers = css_select 'table thead tr th'
30
35
  assert_equal '<th>downcase</th>', headers[0].to_s.strip
31
36
  assert_equal '<th>upcase</th>', headers[1].to_s
32
37
  end
33
38
  end
34
39
 
35
- context "a complex table" do
40
+ context 'a complex table' do
36
41
  setup do
37
42
  Person = Struct.new(:id, :name, :surname, :email, :phone) unless defined?(Person)
38
43
  @collection = []
39
- @collection << Person.new(1, "John", "Doe", "jdoe@gmail.com", "500600700")
40
- @collection << Person.new(2, "Barack", "Obama", "bobama@polskieobozy.com", "501601701")
44
+ @collection << Person.new(1, 'John', 'Doe', 'jdoe@gmail.com', '500600700')
45
+ @collection << Person.new(2, 'Barack', 'Obama', 'bobama@polskieobozy.com', '501601701')
41
46
  concat(table_for(@collection) do |t|
42
47
  t.span(:names) do |s|
43
48
  s.column :name, class: 'name', header_class: 'name_head'
44
49
  s.column(:surname) { |person| content_tag(:span, person.surname.capitalize) }
45
50
  end
46
51
  t.span(:contact_data) do |s|
47
- s.column :email, class: lambda { |person| ['foo', 'bar', person.email =~ /gmail/ && 'gmail'].compact }
52
+ s.column :email, class: ->(person) { ['foo', 'bar', person.email =~ /gmail/ && 'gmail'].compact }
48
53
  s.column :phone
49
54
  end
50
55
  end)
51
56
  end
52
57
 
53
- should "have rows with proper id attributes" do
58
+ should 'have rows with proper id attributes' do
54
59
  assert_select 'table tbody tr#tablehelpertest-person-1'
55
60
  assert_select 'table tbody tr#tablehelpertest-person-2'
56
61
  end
@@ -65,66 +70,66 @@ class TableHelperTest < ActionView::TestCase
65
70
  assert_equal 4, row.size
66
71
  end
67
72
 
68
- should "have proper body content" do
69
- assert_select 'table tbody tr:first-child td:first-child', {count: 1, text: 'John'}
70
- assert_select 'table tbody tr:first-child td:nth-child(2) span', {count: 1, text: 'Doe'}
73
+ should 'have proper body content' do
74
+ assert_select 'table tbody tr:first-child td:first-child', count: 1, text: 'John'
75
+ assert_select 'table tbody tr:first-child td:nth-child(2) span', count: 1, text: 'Doe'
71
76
  end
72
77
 
73
- should "have proper td class in name column" do
78
+ should 'have proper td class in name column' do
74
79
  td = css_select('table tbody tr:first-child td:first-child').first
75
80
  assert_equal 'name', td.attributes['class'].value
76
81
  end
77
82
 
78
- should "have proper th class in name column" do
83
+ should 'have proper th class in name column' do
79
84
  td = css_select('table thead tr:nth-child(2) th:first-child').first
80
85
  assert_equal 'name_head', td.attributes['class'].value
81
86
  end
82
87
 
83
- should "have proper td class in first row in email column" do
88
+ should 'have proper td class in first row in email column' do
84
89
  td = css_select('table tbody tr:nth-child(1) td:nth-child(3)').first
85
90
  assert_equal 'foo bar gmail', td.attributes['class'].value
86
91
  end
87
92
 
88
- should "have proper td class in second row in email column" do
93
+ should 'have proper td class in second row in email column' do
89
94
  td = css_select('table tbody tr:nth-child(2) td:nth-child(3)').first
90
95
  assert_equal 'foo bar', td.attributes['class'].value
91
96
  end
92
97
  end
93
98
 
94
- context "table with nested spans" do
99
+ context 'table with nested spans' do
95
100
  setup do
96
101
  @collection = []
97
102
  concat(
98
- table_for(@collection) do |t|
99
- t.span :span1 do |s1|
100
- s1.span :span2 do |s2|
101
- s2.column :col1
102
- s2.column :col2
103
- end
104
- s1.span :span3 do |s3|
105
- s3.column :col3
106
- s3.column :col4
107
- end
103
+ table_for(@collection) do |t|
104
+ t.span :span1 do |s1|
105
+ s1.span :span2 do |s2|
106
+ s2.column :col1
107
+ s2.column :col2
108
108
  end
109
- t.span :span4 do |s4|
110
- s4.column :col5
111
- s4.column :col6
109
+ s1.span :span3 do |s3|
110
+ s3.column :col3
111
+ s3.column :col4
112
112
  end
113
- t.column :col7, header_rowspan: 3
114
113
  end
114
+ t.span :span4 do |s4|
115
+ s4.column :col5
116
+ s4.column :col6
117
+ end
118
+ t.column :col7, header_rowspan: 3
119
+ end
115
120
  )
116
121
  end
117
122
 
118
- should "have 3 rows in thead" do
123
+ should 'have 3 rows in thead' do
119
124
  assert_select 'table thead tr', 3
120
125
  end
121
126
 
122
- should "have rowspan=3 in last th of first tr" do
127
+ should 'have rowspan=3 in last th of first tr' do
123
128
  th = css_select('table thead tr:first-child th:last-child').first
124
129
  assert_equal '3', th.attributes['rowspan'].value
125
130
  end
126
131
 
127
- should "have 11 th elements in thead" do
132
+ should 'have 11 th elements in thead' do
128
133
  assert_select 'table thead th', count: 11
129
134
  end
130
135
  end
@@ -7,29 +7,27 @@ class SpansTest < ActionView::TestCase
7
7
  @node = Tree::TreeNode.new('root')
8
8
  end
9
9
 
10
- should "create a Span given title, a hash and a block" do
10
+ should 'create a Span given title, a hash and a block' do
11
11
  span('title', class: 'klazz') { |s| }
12
- s = node.children.first.content
12
+ s = node.children.first.content
13
13
  assert_equal 'title', s.instance_variable_get(:@title)
14
14
  end
15
15
 
16
- should "create a Span given title, label, a hash and a block" do
16
+ should 'create a Span given title, label, a hash and a block' do
17
17
  span('title', 'label', class: 'klazz') { |s| }
18
- s = node.children.first.content
18
+ s = node.children.first.content
19
19
  assert_equal 'title', s.instance_variable_get(:@title)
20
20
  end
21
21
 
22
- should "create a Span given a hash and a block" do
22
+ should 'create a Span given a hash and a block' do
23
23
  span(class: 'klazz') { |s| }
24
- s = node.children.first.content
24
+ s = node.children.first.content
25
25
  assert_nil s.instance_variable_get(:@title)
26
26
  end
27
27
 
28
- should "create a Span fiven only a block" do
28
+ should 'create a Span fiven only a block' do
29
29
  span { |s| }
30
30
  s = node.children.first.content
31
31
  assert_nil s.instance_variable_get(:@title)
32
32
  end
33
-
34
-
35
- end
33
+ end
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TableBuilderTest < ActionView::TestCase
4
-
5
- end
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Głuszecki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-11 00:00:00.000000000 Z
11
+ date: 2018-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel