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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab1191a7e5415effc91360c1b3d40029e1b6b306
4
- data.tar.gz: 919c58134f5dd0cc60091009da614e10cbadc8db
3
+ metadata.gz: 86d47a1e71a31c961fa7c5d752ff10a497022612
4
+ data.tar.gz: 6f820b7818a529c0b005a878ea2a7fb2d35f67f6
5
5
  SHA512:
6
- metadata.gz: 89fcc4a74c9f75527faa7497cb5910120fa25dbdc8b25748d0e8e2a7350c1507dc28d1a6af52a987aedfcf6d525be8f8a457bc478d7ea92a78602d45dc34dfab
7
- data.tar.gz: feb1d98466e17d18ef32244971e977e0d57417f1df86e1a9a67ab938f57fa1f38f27ad442e6a7ff0f17159d5f4c8782e293e7fc0029a1719e55b12b2fbb6e254
6
+ metadata.gz: d9b416c54070256d6901c6c5348aaed4bd93cbbb262170abfe259b04b9483fdd51ec7d1fa65361e7800113926088e419d1ff901252e55b158df3b1273167a3b9
7
+ data.tar.gz: 68214b0a459ddf391f47acba7b3d5c5be81174768c5d9ee96c035ac921289831e4d443e9074a34e36c4853b7fec2058743e291292037555e1a77c1d3fd5fea33
data/README.md CHANGED
@@ -1,29 +1,46 @@
1
- # Easytable
1
+ Gem add the ability quickly create table <code>render_table_for</code>.
2
+ Using in your helpers or views.
2
3
 
3
- TODO: Write a gem description
4
+ == Installation
4
5
 
5
- ## Installation
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
- Or install it yourself as:
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
- $ gem install easytable
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
- ## Usage
36
+ render_table_for(header: [], columns: [], class: 'custom-class', id: 'table_id')
20
37
 
21
- TODO: Write usage instructions here
38
+ default class name is <code>easy-table</code>
22
39
 
23
- ## Contributing
40
+ == Contributing
24
41
 
25
42
  1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
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 obj_keys.include?(key.to_s)
7
+ values << self.send(key) if keys.include?(key.to_s)
9
8
  end
10
9
  values
11
10
  end
@@ -1,3 +1,3 @@
1
1
  module Easytable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -3,14 +3,11 @@ require 'easytable/view_helpers'
3
3
  module Easytable
4
4
  module ActionView
5
5
 
6
- def render_table_for(table_header: [], columns: [], **opts)
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(table_header).concat(tbody(columns))
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 thead(table_header)
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
- table_header.collect { |title| concat content_tag(:th, title) }.join()
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(table_header: header, columns: columns) }
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(table_header: header, columns: columns) }
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(table_header: header, columns: columns) }
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.2
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-06 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler