easytable 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +31 -14
- data/lib/easytable/active_record.rb +1 -2
- data/lib/easytable/version.rb +1 -1
- data/lib/easytable/view_helpers/action_view.rb +18 -8
- data/spec/view_helpers/action_view_spec.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86d47a1e71a31c961fa7c5d752ff10a497022612
|
4
|
+
data.tar.gz: 6f820b7818a529c0b005a878ea2a7fb2d35f67f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9b416c54070256d6901c6c5348aaed4bd93cbbb262170abfe259b04b9483fdd51ec7d1fa65361e7800113926088e419d1ff901252e55b158df3b1273167a3b9
|
7
|
+
data.tar.gz: 68214b0a459ddf391f47acba7b3d5c5be81174768c5d9ee96c035ac921289831e4d443e9074a34e36c4853b7fec2058743e291292037555e1a77c1d3fd5fea33
|
data/README.md
CHANGED
@@ -1,29 +1,46 @@
|
|
1
|
-
|
1
|
+
Gem add the ability quickly create table <code>render_table_for</code>.
|
2
|
+
Using in your helpers or views.
|
2
3
|
|
3
|
-
|
4
|
+
== Installation
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
6
|
+
1 Add this line to your application's Gemfile:
|
8
7
|
|
9
8
|
gem 'easytable'
|
10
9
|
|
11
|
-
And then execute:
|
10
|
+
2 And then execute:
|
12
11
|
|
13
12
|
$ bundle
|
14
13
|
|
15
|
-
|
14
|
+
== When to use it
|
15
|
+
|
16
|
+
If you too lazy to work with content tags or you hate a lot of html in your views, you can use just one line to generate simple table.
|
17
|
+
|
18
|
+
== Usage Examples
|
19
|
+
|
20
|
+
render_table_for(columns: [['Bob', 'bob@email.com'], ['Ben', 'ben@email.com']])
|
21
|
+
|
22
|
+
or
|
23
|
+
|
24
|
+
render_table_for(header: ['First name', 'Last name'], columns: @columns)
|
25
|
+
|
26
|
+
or
|
27
|
+
|
28
|
+
render_table_for(header: [:id, :email], columns: User.last(10))
|
29
|
+
|
30
|
+
or
|
16
31
|
|
17
|
-
|
32
|
+
render_table_for(User.last(10))
|
33
|
+
|
34
|
+
Also you can specify table class name and id, using <code>class</code> and <code>id</code>:
|
18
35
|
|
19
|
-
|
36
|
+
render_table_for(header: [], columns: [], class: 'custom-class', id: 'table_id')
|
20
37
|
|
21
|
-
|
38
|
+
default class name is <code>easy-table</code>
|
22
39
|
|
23
|
-
|
40
|
+
== Contributing
|
24
41
|
|
25
42
|
1. Fork it
|
26
|
-
2. Create your feature branch (
|
27
|
-
3. Commit your changes (
|
28
|
-
4. Push to the branch (
|
43
|
+
2. Create your feature branch (<code>git checkout -b my-new-feature</code>)
|
44
|
+
3. Commit your changes (<code>git commit -am 'Added some feature'</code>)
|
45
|
+
4. Push to the branch (<code>git push origin my-new-feature</code>)
|
29
46
|
5. Create new Pull Request
|
@@ -3,9 +3,8 @@ require 'active_record'
|
|
3
3
|
module CustomHelpers
|
4
4
|
def to_array_by_keys(keys)
|
5
5
|
values = []
|
6
|
-
obj_keys = self.attributes.keys
|
7
6
|
keys.each do |key|
|
8
|
-
values << self.send(key) if
|
7
|
+
values << self.send(key) if keys.include?(key.to_s)
|
9
8
|
end
|
10
9
|
values
|
11
10
|
end
|
data/lib/easytable/version.rb
CHANGED
@@ -3,14 +3,11 @@ require 'easytable/view_helpers'
|
|
3
3
|
module Easytable
|
4
4
|
module ActionView
|
5
5
|
|
6
|
-
def render_table_for(
|
7
|
-
if columns.is_a?(ActiveRecord::Relation)
|
8
|
-
columns = columns.map { |record| record.to_array_by_keys(table_header) }
|
9
|
-
table_header = table_header.map!(&:to_s).map!(&:humanize)
|
10
|
-
end
|
6
|
+
def render_table_for(header: [], columns: [], **opts)
|
7
|
+
header, columns = modify_data_for(header, columns) if columns.is_a?(ActiveRecord::Relation)
|
11
8
|
table_class = opts[:class] || :'easy-table'
|
12
9
|
content_tag(:table, id: opts[:id], class: table_class) {
|
13
|
-
thead(
|
10
|
+
thead(header).concat(tbody(columns))
|
14
11
|
}
|
15
12
|
end
|
16
13
|
|
@@ -18,10 +15,23 @@ module Easytable
|
|
18
15
|
|
19
16
|
private
|
20
17
|
|
21
|
-
def
|
18
|
+
def modify_data_for(header, columns)
|
19
|
+
table_header = valid_header_keys(header: header, record: columns.first)
|
20
|
+
columns = columns.map { |record| record.to_array_by_keys(table_header) }
|
21
|
+
table_header.map!(&:humanize)
|
22
|
+
[table_header, columns]
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_header_keys(header: [], record: nil)
|
26
|
+
record_keys = record.respond_to?(:attributes) ? record.attributes.keys : header
|
27
|
+
valid_keys = header.map { |key| key.to_s.downcase } & record_keys
|
28
|
+
valid_keys.any? ? valid_keys : record_keys
|
29
|
+
end
|
30
|
+
|
31
|
+
def thead(header)
|
22
32
|
content_tag :thead do
|
23
33
|
content_tag :tr do
|
24
|
-
|
34
|
+
header.collect { |title| concat content_tag(:th, title) }.join()
|
25
35
|
end
|
26
36
|
end
|
27
37
|
end
|
@@ -9,7 +9,7 @@ describe Easytable::ActionView do
|
|
9
9
|
|
10
10
|
let(:header) { [] }
|
11
11
|
let(:columns) { [] }
|
12
|
-
subject { render_table_for(
|
12
|
+
subject { render_table_for(header: header, columns: columns) }
|
13
13
|
|
14
14
|
it 'should render table tag' do
|
15
15
|
expect(subject).to include('table', '/table')
|
@@ -27,7 +27,7 @@ describe Easytable::ActionView do
|
|
27
27
|
describe 'Table with header' do
|
28
28
|
let(:header) { ['Title', 'Description'] }
|
29
29
|
let(:columns) { [] }
|
30
|
-
subject { render_table_for(
|
30
|
+
subject { render_table_for(header: header, columns: columns) }
|
31
31
|
|
32
32
|
it 'should render header data' do
|
33
33
|
expect(subject).to include('<thead><tr><th>Title</th><th>Description</th></tr></thead>')
|
@@ -37,7 +37,7 @@ describe Easytable::ActionView do
|
|
37
37
|
describe 'Table with columns' do
|
38
38
|
let(:header) { [] }
|
39
39
|
let(:columns) { [[1, 'first line'], [2, 'second line']] }
|
40
|
-
subject { render_table_for(
|
40
|
+
subject { render_table_for(header: header, columns: columns) }
|
41
41
|
|
42
42
|
it 'should render columns data' do
|
43
43
|
expect(subject).to include('<tbody><tr><td>1</td><td>first line</td></tr><tr><td>2</td><td>second line</td></tr></tbody>')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easytable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Zimin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|