rails_table_for 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7771134d7d3b6bbf57a19ca741f08c113e3f21cedffecf3aded0353ddc444663
4
- data.tar.gz: faaf9c18db501ed6abccbca4d937b9fbd3f2e5343ab0a01a1549654c8970e93d
3
+ metadata.gz: bc39558936421adf2e8f1fe5cc4f79d404f423ad2a48ac96aed9b951de5cfb05
4
+ data.tar.gz: 2459ffa6e9167052e2a2f1e2fe02817c65eb2dafb9267cff706a0b7be9e4dd4b
5
5
  SHA512:
6
- metadata.gz: 7e69b040c738f65d0133e65f6e3dbca8e19b0aed58267235cbfe949469b3fa06db8c8f77cde0935acd8cf875c62cc1694494c8714e21ad66137054db21e9555b
7
- data.tar.gz: d51079e75e4d0c6d28d5c74a4a79cc1319ff4d1a02a01460f09fd0eb119c400d0dd7217a8ff9e0e8cc24e9a9635db71bd9b299f9eb9d20ce336505b039c44c9c
6
+ metadata.gz: 1ae6589f69b463b6d997b2e4a110c63bdbe682554612cdbc7611b0d665344c14c7f703de6b1f834ca3e8477c9ebb7e35eb762d861114153b6a5c242416f36d98
7
+ data.tar.gz: d6f88eb4659eeda945bfda4c9f23cd924c720052651125a7c7f5d2e9d0d44e2af4425d6d9109dc8c8036453616b57b62a8cedd05cf485522f6baf4a083f5e1a2
@@ -1,4 +1,4 @@
1
- name: Run Minitest
1
+ name: Tests
2
2
 
3
3
  on:
4
4
  push:
@@ -17,7 +17,10 @@ jobs:
17
17
  uses: actions/setup-ruby@v1
18
18
  with:
19
19
  ruby-version: 2.6.x
20
- - name: Build and test with Rake
20
+ - name: Install libsqlite3-dev
21
+ run: |
22
+ sudo apt-get install -y libsqlite3-dev
23
+ - name: Build and test
21
24
  run: |
22
25
  gem install bundler
23
26
  bundle install --jobs 4 --retry 3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_table_for (0.2.0)
4
+ rails_table_for (0.2.1)
5
5
  rails (>= 5)
6
6
 
7
7
  GEM
@@ -142,6 +142,7 @@ DEPENDENCIES
142
142
  bundler (~> 2.1, >= 2.1.4)
143
143
  byebug (~> 11.1, >= 11.1.1)
144
144
  minitest (~> 5.14)
145
+ nokogiri (~> 1.10, >= 1.10.9)
145
146
  rails_table_for!
146
147
  rake (~> 13.0, >= 13.0.1)
147
148
  sqlite3 (~> 1.4, >= 1.4.2)
data/docs/index.md CHANGED
@@ -100,4 +100,18 @@ Assuming the route exists to show that record, this will generate HTML like this
100
100
  </tr>
101
101
  </tbody>
102
102
  </table>
103
- ```
103
+ ```
104
+
105
+ # Auto-linking columns
106
+
107
+ Automatically get a link to the record represented by each row like this:
108
+
109
+ ```
110
+ <%=
111
+ table_for @records do |table|
112
+ table.column :id, auto_link: true
113
+ end
114
+ %>
115
+ ```
116
+
117
+ If no route can be found for the record, a `NoMethodError` will be thrown.
@@ -0,0 +1,19 @@
1
+ require 'rails_table_for/elements/table'
2
+
3
+ module TableHelper
4
+ include ActionView::Helpers::TagHelper
5
+
6
+ def table_for(records, **options)
7
+ def table_for(records, **options)
8
+ table = Elements::Table.new(options)
9
+ if block_given?
10
+ yield table
11
+ end
12
+ table.build(records)
13
+ end
14
+ end
15
+ end
16
+
17
+ ActionView::Base.class_eval do
18
+ include TableHelper
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails_table_for/helpers/auto_link'
2
+ require 'rails_table_for/elements/column'
3
+
4
+ module Elements
5
+ class BlockColumn < Column
6
+ include Helpers::AutoLink
7
+ attr_reader :title
8
+
9
+ def initialize(block, **options)
10
+ @block = block
11
+ @title = options[:title]
12
+ @auto_link = options[:auto_link] || false
13
+ end
14
+
15
+ def value_for(record)
16
+ text = @block.call(record)
17
+ @auto_link ? auto_link(record, text) : text
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Elements
2
+ class Column
3
+ def value_for(record)
4
+ raise "Not implemented"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails_table_for/helpers/auto_link'
2
+ require 'rails_table_for/elements/column'
3
+
4
+ module Elements
5
+ class FieldColumn < Column
6
+ include Helpers::AutoLink
7
+ attr_reader :title
8
+
9
+ def initialize(field, **options)
10
+ if field.nil?
11
+ raise ArgumentError.new("Field cannot be nil")
12
+ end
13
+ @field = field
14
+ @title = options[:title] || field.to_s.humanize
15
+ @auto_link = options[:auto_link] || false
16
+ end
17
+
18
+ def value_for(record)
19
+ text = record.send(@field)
20
+ @auto_link ? auto_link(record, text) : text
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ require 'rails_table_for/elements/block_column'
2
+ require 'rails_table_for/elements/field_column'
3
+
4
+ module Elements
5
+ class Table
6
+ include ActionView::Helpers::TagHelper
7
+
8
+ attr_accessor :output_buffer
9
+
10
+ def initialize(**options)
11
+ columns = options[:columns] || []
12
+ @columns = columns.map do |field|
13
+ FieldColumn.new(field)
14
+ end
15
+ @options = options
16
+ end
17
+
18
+ def column(field=nil, **options, &block)
19
+ if field.nil? && !block_given?
20
+ raise 'Must provide either field or block'
21
+ end
22
+
23
+ if block_given?
24
+ @columns << BlockColumn.new(block, options)
25
+ else
26
+ @columns << FieldColumn.new(field, options)
27
+ end
28
+ end
29
+
30
+ def build(records)
31
+ return '' if records.nil? || records.empty?
32
+ return '' if @columns.nil? || @columns.empty?
33
+ table(records)
34
+ end
35
+
36
+ private
37
+ def table(records)
38
+ content_tag :table, class: @options[:class] do
39
+ [head, body(records)].join.html_safe
40
+ end
41
+ end
42
+
43
+ def head
44
+ content_tag :thead do
45
+ content_tag :tr do
46
+ @columns.map {|column| content_tag :th, column.title }.join.html_safe
47
+ end
48
+ end
49
+ end
50
+
51
+ def body(records)
52
+ content_tag :tbody do
53
+ records.map {|record| body_row(record) }.join.html_safe
54
+ end
55
+ end
56
+
57
+ def body_row(record)
58
+ content_tag :tr do
59
+ @columns.map {|column| content_tag :td, column.value_for(record) }.join.html_safe
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,10 @@
1
+ module Helpers
2
+ module AutoLink
3
+ include ActionView::Helpers::UrlHelper
4
+
5
+ def auto_link(record, text)
6
+ path = Rails.application.routes.url_helpers.polymorphic_path(record)
7
+ link_to(text, path)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsTableFor
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'rails_table_for/railtie'
2
- require 'rails_table_for/table_helper'
2
+ require 'rails_table_for/action_view_ext'
3
3
 
4
4
  module RailsTableFor
5
5
  # Your code goes here...
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler', '~> 2.1', '>= 2.1.4'
26
26
  spec.add_development_dependency 'byebug', '~> 11.1', '>= 11.1.1'
27
27
  spec.add_development_dependency 'minitest', '~> 5.14'
28
+ spec.add_development_dependency 'nokogiri', '~> 1.10', '>= 1.10.9'
28
29
  spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
29
30
  spec.add_development_dependency 'sqlite3', '~> 1.4', '>= 1.4.2'
30
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_table_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Roos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-25 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -92,6 +92,26 @@ dependencies:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: '5.14'
95
+ - !ruby/object:Gem::Dependency
96
+ name: nokogiri
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.10'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.10.9
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.10'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 1.10.9
95
115
  - !ruby/object:Gem::Dependency
96
116
  name: rake
97
117
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +162,7 @@ extra_rdoc_files: []
142
162
  files:
143
163
  - ".github/ISSUE_TEMPLATE/bug_report.md"
144
164
  - ".github/ISSUE_TEMPLATE/new-user-story.md"
145
- - ".github/workflows/ruby.yml"
165
+ - ".github/workflows/tests.yml"
146
166
  - ".gitignore"
147
167
  - Gemfile
148
168
  - Gemfile.lock
@@ -157,12 +177,13 @@ files:
157
177
  - docs/_config.yml
158
178
  - docs/index.md
159
179
  - lib/rails_table_for.rb
160
- - lib/rails_table_for/block_column.rb
161
- - lib/rails_table_for/column.rb
162
- - lib/rails_table_for/field_column.rb
180
+ - lib/rails_table_for/action_view_ext.rb
181
+ - lib/rails_table_for/elements/block_column.rb
182
+ - lib/rails_table_for/elements/column.rb
183
+ - lib/rails_table_for/elements/field_column.rb
184
+ - lib/rails_table_for/elements/table.rb
185
+ - lib/rails_table_for/helpers/auto_link.rb
163
186
  - lib/rails_table_for/railtie.rb
164
- - lib/rails_table_for/table.rb
165
- - lib/rails_table_for/table_helper.rb
166
187
  - lib/rails_table_for/version.rb
167
188
  - lib/tasks/rails_table_for_tasks.rake
168
189
  - rails_table_for.gemspec
@@ -1,14 +0,0 @@
1
- require 'rails_table_for/column'
2
-
3
- class BlockColumn < Column
4
- attr_reader :title
5
-
6
- def initialize(block, title)
7
- @block = block
8
- @title = title
9
- end
10
-
11
- def value_for(record)
12
- @block.call(record)
13
- end
14
- end
@@ -1,5 +0,0 @@
1
- class Column
2
- def value_for(record)
3
- raise "Not implemented"
4
- end
5
- end
@@ -1,17 +0,0 @@
1
- require 'rails_table_for/column'
2
-
3
- class FieldColumn < Column
4
- attr_reader :title
5
-
6
- def initialize(field, title = nil)
7
- if field.nil?
8
- raise ArgumentError.new("Field cannot be nil")
9
- end
10
- @field = field
11
- @title = title || field.to_s.humanize
12
- end
13
-
14
- def value_for(record)
15
- record.send(@field)
16
- end
17
- end
@@ -1,61 +0,0 @@
1
- require 'rails_table_for/block_column'
2
- require 'rails_table_for/field_column'
3
-
4
- class Table
5
- include ActionView::Helpers::TagHelper
6
-
7
- attr_accessor :output_buffer
8
-
9
- def initialize(**options)
10
- columns = options[:columns] || []
11
- @columns = columns.map do |field|
12
- FieldColumn.new(field)
13
- end
14
- @options = options
15
- end
16
-
17
- def column(field=nil, **options, &block)
18
- if field.nil? && !block_given?
19
- raise 'Must provide either field or block'
20
- end
21
- title = options[:title] || options['title']
22
-
23
- if block_given?
24
- @columns << BlockColumn.new(block, title)
25
- else
26
- @columns << FieldColumn.new(field, title)
27
- end
28
- end
29
-
30
- def build(records)
31
- return '' if records.nil? || records.empty?
32
- table(records)
33
- end
34
-
35
- private
36
- def table(records)
37
- content_tag :table, class: @options[:class] do
38
- [head, body(records)].join.html_safe
39
- end
40
- end
41
-
42
- def head
43
- content_tag :thead do
44
- content_tag :tr do
45
- @columns.map {|column| content_tag :th, column.title }.join.html_safe
46
- end
47
- end
48
- end
49
-
50
- def body(records)
51
- content_tag :tbody do
52
- records.map {|record| body_row(record) }.join.html_safe
53
- end
54
- end
55
-
56
- def body_row(record)
57
- content_tag :tr do
58
- @columns.map {|column| content_tag :td, column.value_for(record) }.join.html_safe
59
- end
60
- end
61
- end
@@ -1,17 +0,0 @@
1
- require 'rails_table_for/table'
2
-
3
- module TableHelper
4
- include ActionView::Helpers::TagHelper
5
-
6
- def table_for(records, **options)
7
- table = Table.new(options)
8
- if block_given?
9
- yield table
10
- end
11
- table.build(records)
12
- end
13
- end
14
-
15
- ActionView::Base.class_eval do
16
- include TableHelper
17
- end