rails_table_for 0.3.0 → 0.3.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: a2c69c3a95a5605f310ce4ef150157d44ab326a07d58b0b94ae984c61bc8059b
4
- data.tar.gz: 7b6cbf7ddebf42a2cad86d6205e00018b36539be505ca41e01e2bc610dfc4f4f
3
+ metadata.gz: 62eb739afea2cf82a0adf56d8f2b5a274c841a242b60ef52a5442cd2ae710404
4
+ data.tar.gz: 595f796250f115a9618511fec2f32aafd49e1366530aa72f1b476909c52cfa3e
5
5
  SHA512:
6
- metadata.gz: 51503314ccec53647609524aadee9b411a8725274e5c18a88b26d49c94b8fc4d42c2451f5ee40a5d546e2dbecc776983f6f0f8b2311f68a0e04378d7f82a5361
7
- data.tar.gz: 144a74ce2af192f88e587535ec4278a4d46f4d2d92a750131a2834de1cf72f9de2ba6c56af266d0953af5fe86ab7c68232c3d7da949870acc874a65fc99a4028
6
+ metadata.gz: 9b74e9fb8d73aadb0a55d5157af96a4df488442aad4af7728b2dfd70d1c683251e1a0f1b57cdd85b84777bcf436d913ce1eb955896917bdd4dc85bedc1144a60
7
+ data.tar.gz: d8e86a815cddf774c524f694101706feb046db52962e904948cc1569899e0abf512f292233111371ca69f43a97a09967e43b84cccc2b07305ac4efa9c6580bf0
@@ -1,8 +1,19 @@
1
+ AllCops:
2
+ Include:
3
+ - '**/Gemfile'
4
+ - 'lib/**/*.rb'
5
+ - 'test/**/*.rb'
6
+ Exclude:
7
+ - 'test/dummy/bin/*'
8
+ - 'test/dummy/db/schema.rb'
9
+
1
10
  Style/Documentation:
2
11
  Enabled: false
3
12
 
4
13
  Layout/LineLength:
5
14
  Max: 100
15
+ Exclude:
16
+ - 'test/elements/table_test.rb'
6
17
 
7
18
  Style/HashEachMethods:
8
19
  Enabled: true
@@ -11,4 +22,4 @@ Style/HashTransformKeys:
11
22
  Enabled: true
12
23
 
13
24
  Style/HashTransformValues:
14
- Enabled: true
25
+ Enabled: true
@@ -1,8 +1,14 @@
1
1
  # CHANGELOG
2
2
  ## Master
3
3
 
4
+ ---
5
+ ## 0.3.0
6
+
7
+ ### Features:
8
+ - Pagination: [#3](https://github.com/acroos/rails_table_for/issues/3)
9
+
4
10
  ---
5
11
  ## 0.2.2
6
12
 
7
13
  ### Bugs Fixed:
8
- - `table_for` method was nested, resulting in initial load rendering "table_for" as text
14
+ - `table_for` method was nested, resulting in initial load rendering "table_for" as text
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in rails_table_for.gemspec
6
8
  gemspec
@@ -1,71 +1,71 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_table_for (0.3.0)
4
+ rails_table_for (0.3.1)
5
5
  rails (>= 5)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (6.0.2.2)
11
- actionpack (= 6.0.2.2)
10
+ actioncable (6.0.3.1)
11
+ actionpack (= 6.0.3.1)
12
12
  nio4r (~> 2.0)
13
13
  websocket-driver (>= 0.6.1)
14
- actionmailbox (6.0.2.2)
15
- actionpack (= 6.0.2.2)
16
- activejob (= 6.0.2.2)
17
- activerecord (= 6.0.2.2)
18
- activestorage (= 6.0.2.2)
19
- activesupport (= 6.0.2.2)
14
+ actionmailbox (6.0.3.1)
15
+ actionpack (= 6.0.3.1)
16
+ activejob (= 6.0.3.1)
17
+ activerecord (= 6.0.3.1)
18
+ activestorage (= 6.0.3.1)
19
+ activesupport (= 6.0.3.1)
20
20
  mail (>= 2.7.1)
21
- actionmailer (6.0.2.2)
22
- actionpack (= 6.0.2.2)
23
- actionview (= 6.0.2.2)
24
- activejob (= 6.0.2.2)
21
+ actionmailer (6.0.3.1)
22
+ actionpack (= 6.0.3.1)
23
+ actionview (= 6.0.3.1)
24
+ activejob (= 6.0.3.1)
25
25
  mail (~> 2.5, >= 2.5.4)
26
26
  rails-dom-testing (~> 2.0)
27
- actionpack (6.0.2.2)
28
- actionview (= 6.0.2.2)
29
- activesupport (= 6.0.2.2)
27
+ actionpack (6.0.3.1)
28
+ actionview (= 6.0.3.1)
29
+ activesupport (= 6.0.3.1)
30
30
  rack (~> 2.0, >= 2.0.8)
31
31
  rack-test (>= 0.6.3)
32
32
  rails-dom-testing (~> 2.0)
33
33
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
- actiontext (6.0.2.2)
35
- actionpack (= 6.0.2.2)
36
- activerecord (= 6.0.2.2)
37
- activestorage (= 6.0.2.2)
38
- activesupport (= 6.0.2.2)
34
+ actiontext (6.0.3.1)
35
+ actionpack (= 6.0.3.1)
36
+ activerecord (= 6.0.3.1)
37
+ activestorage (= 6.0.3.1)
38
+ activesupport (= 6.0.3.1)
39
39
  nokogiri (>= 1.8.5)
40
- actionview (6.0.2.2)
41
- activesupport (= 6.0.2.2)
40
+ actionview (6.0.3.1)
41
+ activesupport (= 6.0.3.1)
42
42
  builder (~> 3.1)
43
43
  erubi (~> 1.4)
44
44
  rails-dom-testing (~> 2.0)
45
45
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
- activejob (6.0.2.2)
47
- activesupport (= 6.0.2.2)
46
+ activejob (6.0.3.1)
47
+ activesupport (= 6.0.3.1)
48
48
  globalid (>= 0.3.6)
49
- activemodel (6.0.2.2)
50
- activesupport (= 6.0.2.2)
51
- activerecord (6.0.2.2)
52
- activemodel (= 6.0.2.2)
53
- activesupport (= 6.0.2.2)
54
- activestorage (6.0.2.2)
55
- actionpack (= 6.0.2.2)
56
- activejob (= 6.0.2.2)
57
- activerecord (= 6.0.2.2)
49
+ activemodel (6.0.3.1)
50
+ activesupport (= 6.0.3.1)
51
+ activerecord (6.0.3.1)
52
+ activemodel (= 6.0.3.1)
53
+ activesupport (= 6.0.3.1)
54
+ activestorage (6.0.3.1)
55
+ actionpack (= 6.0.3.1)
56
+ activejob (= 6.0.3.1)
57
+ activerecord (= 6.0.3.1)
58
58
  marcel (~> 0.3.1)
59
- activesupport (6.0.2.2)
59
+ activesupport (6.0.3.1)
60
60
  concurrent-ruby (~> 1.0, >= 1.0.2)
61
61
  i18n (>= 0.7, < 2)
62
62
  minitest (~> 5.1)
63
63
  tzinfo (~> 1.1)
64
- zeitwerk (~> 2.2)
64
+ zeitwerk (~> 2.2, >= 2.2.2)
65
65
  ast (2.4.0)
66
66
  builder (3.2.4)
67
67
  bump (0.9.0)
68
- byebug (11.1.1)
68
+ byebug (11.1.3)
69
69
  concurrent-ruby (1.1.6)
70
70
  crass (1.0.6)
71
71
  erubi (1.9.0)
@@ -73,7 +73,6 @@ GEM
73
73
  activesupport (>= 4.2.0)
74
74
  i18n (1.8.2)
75
75
  concurrent-ruby (~> 1.0)
76
- jaro_winkler (1.5.4)
77
76
  loofah (2.5.0)
78
77
  crass (~> 1.0.2)
79
78
  nokogiri (>= 1.5.9)
@@ -82,56 +81,58 @@ GEM
82
81
  marcel (0.3.3)
83
82
  mimemagic (~> 0.3.2)
84
83
  method_source (1.0.0)
85
- mimemagic (0.3.4)
84
+ mimemagic (0.3.5)
86
85
  mini_mime (1.0.2)
87
86
  mini_portile2 (2.4.0)
88
- minitest (5.14.0)
87
+ minitest (5.14.1)
89
88
  nio4r (2.5.2)
90
89
  nokogiri (1.10.9)
91
90
  mini_portile2 (~> 2.4.0)
92
91
  parallel (1.19.1)
93
- parser (2.7.0.5)
92
+ parser (2.7.1.3)
94
93
  ast (~> 2.4.0)
95
94
  rack (2.2.2)
96
95
  rack-test (1.1.0)
97
96
  rack (>= 1.0, < 3)
98
- rails (6.0.2.2)
99
- actioncable (= 6.0.2.2)
100
- actionmailbox (= 6.0.2.2)
101
- actionmailer (= 6.0.2.2)
102
- actionpack (= 6.0.2.2)
103
- actiontext (= 6.0.2.2)
104
- actionview (= 6.0.2.2)
105
- activejob (= 6.0.2.2)
106
- activemodel (= 6.0.2.2)
107
- activerecord (= 6.0.2.2)
108
- activestorage (= 6.0.2.2)
109
- activesupport (= 6.0.2.2)
97
+ rails (6.0.3.1)
98
+ actioncable (= 6.0.3.1)
99
+ actionmailbox (= 6.0.3.1)
100
+ actionmailer (= 6.0.3.1)
101
+ actionpack (= 6.0.3.1)
102
+ actiontext (= 6.0.3.1)
103
+ actionview (= 6.0.3.1)
104
+ activejob (= 6.0.3.1)
105
+ activemodel (= 6.0.3.1)
106
+ activerecord (= 6.0.3.1)
107
+ activestorage (= 6.0.3.1)
108
+ activesupport (= 6.0.3.1)
110
109
  bundler (>= 1.3.0)
111
- railties (= 6.0.2.2)
110
+ railties (= 6.0.3.1)
112
111
  sprockets-rails (>= 2.0.0)
113
112
  rails-dom-testing (2.0.3)
114
113
  activesupport (>= 4.2.0)
115
114
  nokogiri (>= 1.6)
116
115
  rails-html-sanitizer (1.3.0)
117
116
  loofah (~> 2.3)
118
- railties (6.0.2.2)
119
- actionpack (= 6.0.2.2)
120
- activesupport (= 6.0.2.2)
117
+ railties (6.0.3.1)
118
+ actionpack (= 6.0.3.1)
119
+ activesupport (= 6.0.3.1)
121
120
  method_source
122
121
  rake (>= 0.8.7)
123
122
  thor (>= 0.20.3, < 2.0)
124
123
  rainbow (3.0.0)
125
124
  rake (13.0.1)
126
125
  rexml (3.2.4)
127
- rubocop (0.80.1)
128
- jaro_winkler (~> 1.5.1)
126
+ rubocop (0.84.0)
129
127
  parallel (~> 1.10)
130
128
  parser (>= 2.7.0.1)
131
129
  rainbow (>= 2.2.2, < 4.0)
132
130
  rexml
131
+ rubocop-ast (>= 0.0.3)
133
132
  ruby-progressbar (~> 1.7)
134
- unicode-display_width (>= 1.4.0, < 1.7)
133
+ unicode-display_width (>= 1.4.0, < 2.0)
134
+ rubocop-ast (0.0.3)
135
+ parser (>= 2.7.0.1)
135
136
  ruby-progressbar (1.10.1)
136
137
  sprockets (4.0.0)
137
138
  concurrent-ruby (~> 1.0)
@@ -145,8 +146,8 @@ GEM
145
146
  thread_safe (0.3.6)
146
147
  tzinfo (1.2.7)
147
148
  thread_safe (~> 0.1)
148
- unicode-display_width (1.6.1)
149
- websocket-driver (0.7.1)
149
+ unicode-display_width (1.7.0)
150
+ websocket-driver (0.7.2)
150
151
  websocket-extensions (>= 0.1.0)
151
152
  websocket-extensions (0.1.4)
152
153
  zeitwerk (2.3.0)
data/Rakefile CHANGED
@@ -27,9 +27,6 @@ end
27
27
  require 'rubocop/rake_task'
28
28
 
29
29
  desc 'Run RuboCop on the lib directory'
30
- RuboCop::RakeTask.new(:rubocop) do |task|
31
- task.patterns = ['lib/**/*.rb']
32
- task.fail_on_error = true
33
- end
30
+ RuboCop::RakeTask.new
34
31
 
35
32
  task default: :test
data/bin/test CHANGED
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- $: << File.expand_path("../test", __dir__)
2
+ # frozen_string_literal: true
3
3
 
4
- require "bundler/setup"
5
- require "rails/plugin/test"
4
+ $LOAD_PATH << File.expand_path('../test', __dir__)
5
+
6
+ require 'bundler/setup'
7
+ require 'rails/plugin/test'
@@ -1,16 +1,17 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  group :jekyll_plugins do
4
- gem "github-pages", "~> 204"
6
+ gem 'github-pages', '~> 204'
5
7
  end
6
8
 
7
9
  # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
8
10
  # and associated library.
9
- install_if -> { RUBY_PLATFORM =~ %r!mingw|mswin|java! } do
10
- gem "tzinfo", "~> 1.2"
11
- gem "tzinfo-data"
11
+ install_if -> { RUBY_PLATFORM =~ /mingw|mswin|java/ } do
12
+ gem 'tzinfo', '~> 1.2'
13
+ gem 'tzinfo-data'
12
14
  end
13
15
 
14
16
  # Performance-booster for watching directories on Windows
15
- gem "wdm", "~> 0.1.0", :install_if => Gem.win_platform?
16
-
17
+ gem 'wdm', '~> 0.1.0', install_if: Gem.win_platform?
@@ -246,8 +246,6 @@ PLATFORMS
246
246
 
247
247
  DEPENDENCIES
248
248
  github-pages (~> 204)
249
- jekyll-feed (~> 0.6)
250
- minima (~> 2.0)
251
249
  tzinfo (~> 1.2)
252
250
  tzinfo-data
253
251
  wdm (~> 0.1.0)
@@ -0,0 +1,44 @@
1
+ ---
2
+ layout: default
3
+ title: rails_table_for
4
+ ---
5
+
6
+ # Auto-linking Rows
7
+
8
+ With the last example, we displayed a link for each row by passing a block to the `column` method
9
+ and using `link_to`. This is a whole lot of extra code for something so simple. Let's see how we
10
+ can do something similar in a single line:
11
+
12
+ ```
13
+ <%=
14
+ table_for @records do |table|
15
+ table.column :name, auto_link: true
16
+ end
17
+ %>
18
+ ```
19
+
20
+ This will generate the following HTML:
21
+
22
+ ```html
23
+ <table>
24
+ <thead>
25
+ <tr>
26
+ <th>Name</th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ <tr>
31
+ <td><a href="/records/1">Austin Roos</a></td>
32
+ </tr>
33
+ </tbody>
34
+ </table>
35
+ ```
36
+
37
+ You can also pass a block to `column` still, whatever value you return will be placed inside
38
+ the anchor tag
39
+
40
+ If no route can be found for the record, a `NoMethodError` will be thrown.
41
+
42
+ Now that we've simplified the linking, we should move on to some more features to make the table
43
+ more user-friendly. It's likely that some collections are going to be much too large to display
44
+ on a single page, so let's try out [pagination](./pagination)
@@ -0,0 +1,36 @@
1
+ ---
2
+ layout: default
3
+ title: rails_table_for
4
+ ---
5
+
6
+ # Customizing Column Headers
7
+
8
+ A one-line table is a delight, but we're a bit limited by only getting values and headers directly from our models.
9
+ Let's start by customizing the column header. The `table_for` method accepts a block with a single `Table` argument.
10
+ This object has a single method (`.column`) that allows you some customization. You can use it like this:
11
+
12
+ ```
13
+ <%= table_for @records do |table| %>
14
+ <% table.column :name, title: 'Naam' %>
15
+ <% end %>
16
+ ```
17
+
18
+ The title of the column will now be `Naam` and the rest of the table will be the same as the first example:
19
+ ```html
20
+ <table>
21
+ <thead>
22
+ <tr>
23
+ <th>Naam</th>
24
+ </tr>
25
+ </thead>
26
+ <tbody>
27
+ <tr>
28
+ <td>Austin Roos</td>
29
+ </tr>
30
+ ...
31
+ </tbody>
32
+ </table>
33
+ ```
34
+
35
+ So... we've got a pretty simple table here with our header in Dutch... there must be more right? Indeed, next let's see
36
+ how to do more than print out values directly off the model. Move on to [customizing row values](./customizing-row-values)
@@ -0,0 +1,41 @@
1
+ ---
2
+ layout: default
3
+ title: rails_table_for
4
+ ---
5
+
6
+ # Customizing Row Values
7
+
8
+ It's great that we can change the header on a column displaying a user's name, but we must be able to do more than just show
9
+ simple strings from our models in these rows, right? Right. You can pass a block to the `Table#column` method and return
10
+ anything your heart desires. Let's add a column that gives us a link to 'Show' the user.
11
+
12
+ ```
13
+ <%=
14
+ table_for @records do |table|
15
+ table.column :name, title: 'Naam'
16
+ table.column title: 'Link' { |record| link_to 'Show', record }
17
+ end
18
+ %>
19
+ ```
20
+
21
+ Assuming the route exists to show that record, this will generate HTML like this:
22
+
23
+ ```html
24
+ <table>
25
+ <thead>
26
+ <tr>
27
+ <th>Naam</th>
28
+ <th>Link</th>
29
+ </tr>
30
+ </thead>
31
+ <tbody>
32
+ <tr>
33
+ <td>Austin Roos</td>
34
+ <td><a href="/records/1">Show</a></td>
35
+ </tr>
36
+ </tbody>
37
+ </table>
38
+ ```
39
+
40
+ So... this gets us through most of the basics, but let's see if we can do things a little more simply. Check out
41
+ [auto-linking rows](./auto-linking-rows)
@@ -0,0 +1,35 @@
1
+ ---
2
+ layout: default
3
+ title: rails_table_for
4
+ ---
5
+
6
+ # Getting Started
7
+
8
+ For a simple table displaying some attributes of your ActiveRecord model, simply
9
+ call `table_for` with an ActiveRecord collection and an array of columns:
10
+
11
+ ```
12
+ <%= table_for @records, columns: [:full_name] %>
13
+ ```
14
+
15
+ The columns must correspond to methods/properties on the model class. The column header
16
+ will be the method/property name "humanized" (capitalized, split into words).
17
+
18
+ Example output:
19
+ ```html
20
+ <table>
21
+ <thead>
22
+ <tr>
23
+ <th>Full Name</th>
24
+ </tr>
25
+ </thead>
26
+ <tbody>
27
+ <tr>
28
+ <td>Austin Roos</td>
29
+ </tr>
30
+ ...
31
+ </tbody>
32
+ </table>
33
+ ```
34
+
35
+ This is a good start, but surely you'd like to customize things a little bit. Move on to [customizing columns](./customizing-column-headers)
@@ -0,0 +1,55 @@
1
+ ---
2
+ layout: default
3
+ title: rails_table_for
4
+ ---
5
+
6
+ # Pagination (beta)
7
+
8
+ If you've made it this far, you know how to create some basic tables to present your data
9
+ in whatever way you see fit. Now you're so happy with your table that you want to present
10
+ 50 million rows. This... is a little much for a single page. Enter pagination:
11
+
12
+ ```
13
+ <%= table_for @records, columns: [:id, :name], page_size: 10 %>
14
+ ```
15
+
16
+ and voila! you have a table showing only 10 rows per page as well as a set of links beneath
17
+ the table to take you to each page
18
+
19
+ ```html
20
+ <div>
21
+ <table>
22
+ <thead>
23
+ <tr>
24
+ <th>Id</th>
25
+ <th>Name</th>
26
+ </tr>
27
+ </thead>
28
+ <tbody>
29
+ <tr>
30
+ <td>Austin Roos</td>
31
+ <td>1</td>
32
+ </tr>
33
+ ...
34
+ </tbody>
35
+ </table>
36
+ <div class="pagination-links">
37
+ 1
38
+ <a href="/users?page=2">2</a>
39
+ <a href="/users?page=3">3</a>
40
+ <a href="/users?page=4">4</a>
41
+ <a href="/users?page=5">5</a>
42
+ </div>
43
+ </div>
44
+ ```
45
+
46
+ This feature is still in beta and there are some known limitations.
47
+
48
+ ### Known Issues
49
+
50
+ If there are multiple tables on a single web page, the pages of the table will always be
51
+ synced because of the query parameter.
52
+
53
+ If the number of pages is very large, every pagination link is still shown instead of
54
+ something a bit more elegant like a few pages before and after the current one as well as
55
+ a link to first and last page
@@ -3,6 +3,12 @@ layout: default
3
3
  title: rails_table_for
4
4
  ---
5
5
 
6
+ # Introduciton
7
+
8
+ No more writing out dozens of <tr>, <th>, <td> elements. Write a few lines of ruby code (or even a single line) and
9
+ get a lovely little table generated for you. You can present data from your records directly with almost no work, or
10
+ do a little customization if you'd like something extra special. More features are coming soon, but pagination
11
+ already comes included (only if you want it).
6
12
 
7
13
  # Installation
8
14
 
@@ -18,100 +24,14 @@ And then execute:
18
24
  $ bundle
19
25
  ```
20
26
 
21
- # Simple Table
22
-
23
- For the simplest table, just pass an ActiveRecord collection and an array of columns
24
- ```
25
- <%= table_for @records, columns: [:name] %>
26
- ```
27
-
28
- The columns must correspond to methods/properties on the record class. The column header
29
- will be the method/property name "humanized".
30
-
31
- Example output:
32
- ```html
33
- <table>
34
- <thead>
35
- <tr>
36
- <th>Name</th>
37
- </tr>
38
- </thead>
39
- <tbody>
40
- <tr>
41
- <td>Austin</td>
42
- </tr>
43
- </tbody>
44
- </table>
45
- ```
27
+ # Code Examples
46
28
 
47
- # Customize Column Headers
48
-
49
- You can can change the default column headers by passing a block to `table_for` and using the `Table#column` method:
50
-
51
- ```
52
- <%= table_for @records do |table| %>
53
- <% table.column :name, title: 'Naam' %>
54
- <% end %>
55
- ```
56
-
57
- The title of the column will now be `Naam` and the rest of the table will be the same as the first example:
58
- ```html
59
- <table>
60
- <thead>
61
- <tr>
62
- <th>Naam</th>
63
- </tr>
64
- </thead>
65
- <tbody>
66
- <tr>
67
- <td>Austin</td>
68
- </tr>
69
- </tbody>
70
- </table>
71
- ```
29
+ - [Getting Started](./guides/getting-started) (a one-liner?!)
30
+ - [Customizing Column Headers](./guides/customizing-column-headers)
31
+ - [Customizing Row Values](./guides/customizing-row-values)
32
+ - [Auto-linking Rows](./guides/auto-linking-rows)
33
+ - [Pagination](./guides/pagination)
72
34
 
73
- # Pass Blocks to `Table#column`
74
-
75
- You can pass a block to the `Table#column` method to display values not directly accessible
76
- from the model's methods/properties:
77
-
78
- ```
79
- <%=
80
- table_for @records do |table|
81
- table.column title: 'Link' do |record|
82
- link_to record.name, record
83
- end
84
- end
85
- %>
86
- ```
87
-
88
- Assuming the route exists to show that record, this will generate HTML like this:
89
-
90
- ```html
91
- <table>
92
- <thead>
93
- <tr>
94
- <th>Link</th>
95
- </tr>
96
- </thead>
97
- <tbody>
98
- <tr>
99
- <td><a href="/records/1">Record 1</a></td>
100
- </tr>
101
- </tbody>
102
- </table>
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
- ```
35
+ # Wanna Help?
116
36
 
117
- If no route can be found for the record, a `NoMethodError` will be thrown.
37
+ Happily accepting bug reports, feature requests, and pull requests. Visit https://github.com/acroos/rails_table_for
@@ -3,24 +3,27 @@
3
3
  require 'rails_table_for/helpers/auto_link'
4
4
  require 'rails_table_for/elements/column'
5
5
 
6
- module Elements
7
- class BlockColumn < Column
8
- include ActionView::Helpers::TagHelper
9
- include Helpers::AutoLink
6
+ module RailsTableFor
7
+ module Elements
8
+ class BlockColumn
9
+ include ActionView::Helpers::TagHelper
10
+ include Helpers::AutoLink
11
+ include Column
10
12
 
11
- attr_reader :block, :title, :auto_link_enabled
12
- private :block, :title, :auto_link_enabled
13
+ attr_reader :block, :title, :auto_link_enabled
14
+ private :block, :title, :auto_link_enabled
13
15
 
14
- def initialize(block, **options)
15
- @block = block
16
- @title = options[:title]
17
- @auto_link_enabled = options[:auto_link] || false
18
- end
16
+ def initialize(block, **options)
17
+ @block = block
18
+ @title = options[:title]
19
+ @auto_link_enabled = options[:auto_link] || false
20
+ end
19
21
 
20
- def td(record)
21
- text = block.call(record)
22
- content = auto_link_enabled ? auto_link(record, text) : text
23
- content_tag :td, content
22
+ def td(record)
23
+ text = block.call(record)
24
+ content = auto_link_enabled ? auto_link(record, text) : text
25
+ content_tag :td, content
26
+ end
24
27
  end
25
28
  end
26
29
  end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Elements
4
- class Column
5
- def th
6
- content_tag :th, title
7
- end
3
+ module RailsTableFor
4
+ module Elements
5
+ module Column
6
+ def th
7
+ content_tag :th, title
8
+ end
8
9
 
9
- def td(_)
10
- raise 'Not implemented'
10
+ def td(_)
11
+ raise 'Not implemented'
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -3,26 +3,29 @@
3
3
  require 'rails_table_for/helpers/auto_link'
4
4
  require 'rails_table_for/elements/column'
5
5
 
6
- module Elements
7
- class FieldColumn < Column
8
- include ActionView::Helpers::TagHelper
9
- include Helpers::AutoLink
6
+ module RailsTableFor
7
+ module Elements
8
+ class FieldColumn
9
+ include ActionView::Helpers::TagHelper
10
+ include Helpers::AutoLink
11
+ include Column
10
12
 
11
- attr_reader :field, :title, :auto_link_enabled
12
- private :field, :title, :auto_link_enabled
13
+ attr_reader :field, :title, :auto_link_enabled
14
+ private :field, :title, :auto_link_enabled
13
15
 
14
- def initialize(field, **options)
15
- raise('Field cannot be nil', ArgumentError) if field.nil?
16
+ def initialize(field, **options)
17
+ raise('Field cannot be nil', ArgumentError) if field.nil?
16
18
 
17
- @field = field
18
- @title = options[:title] || field.to_s.humanize
19
- @auto_link_enabled = options[:auto_link] || false
20
- end
19
+ @field = field
20
+ @title = options[:title] || field.to_s.humanize
21
+ @auto_link_enabled = options[:auto_link] || false
22
+ end
21
23
 
22
- def td(record)
23
- text = record.send(field)
24
- content = auto_link_enabled ? auto_link(record, text) : text
25
- content_tag :td, content
24
+ def td(record)
25
+ text = record.send(field)
26
+ content = auto_link_enabled ? auto_link(record, text) : text
27
+ content_tag :td, content
28
+ end
26
29
  end
27
30
  end
28
31
  end
@@ -4,73 +4,75 @@ require 'rails_table_for/elements/block_column'
4
4
  require 'rails_table_for/elements/field_column'
5
5
  require 'rails_table_for/helpers/paginate'
6
6
 
7
- module Elements
8
- class Table
9
- include Helpers::Paginate
10
- include ActionView::Helpers::TagHelper
7
+ module RailsTableFor
8
+ module Elements
9
+ class Table
10
+ include Helpers::Paginate
11
+ include ActionView::Helpers::TagHelper
11
12
 
12
- attr_accessor :columns, :output_buffer, :page_size, :record_count, :records, :request_params,
13
- :request_path
14
- private :columns, :page_size, :record_count, :records, :request_params, :request_path
13
+ attr_accessor :columns, :output_buffer, :page_size, :record_count, :records, :request_params,
14
+ :request_path
15
+ private :columns, :page_size, :record_count, :records, :request_params, :request_path
15
16
 
16
- def initialize(records, **options)
17
- @records = records
18
- @record_count = records.count
19
- @columns = []
20
- options[:columns]&.each { |field| column(field) }
21
- @page_size = options[:page_size]
22
- @request_path = options[:request_path]
23
- @request_params = options[:request_params]
24
- end
17
+ def initialize(records, **options)
18
+ @records = records
19
+ @record_count = records.count
20
+ @columns = []
21
+ options[:columns]&.each { |field| column(field) }
22
+ @page_size = options[:page_size]
23
+ @request_path = options[:request_path]
24
+ @request_params = options[:request_params]
25
+ end
25
26
 
26
- def column(field = nil, **options, &block)
27
- if block_given?
28
- columns << BlockColumn.new(block, options)
29
- elsif field
30
- columns << FieldColumn.new(field, options)
31
- else
32
- raise 'Must provide either field or block'
27
+ def column(field = nil, **options, &block)
28
+ if block_given?
29
+ columns << BlockColumn.new(block, options)
30
+ elsif field
31
+ columns << FieldColumn.new(field, options)
32
+ else
33
+ raise 'Must provide either field or block'
34
+ end
33
35
  end
34
- end
35
36
 
36
- def to_s
37
- return '' if record_count.zero?
38
- return '' if columns.nil? || columns.empty?
37
+ def to_s
38
+ return '' if record_count.zero?
39
+ return '' if columns.empty?
39
40
 
40
- draw
41
- end
41
+ draw
42
+ end
42
43
 
43
- private
44
+ private
44
45
 
45
- def draw
46
- content_tag :div do
47
- table + pagination_links
46
+ def draw
47
+ content_tag :div do
48
+ table + pagination_links
49
+ end
48
50
  end
49
- end
50
51
 
51
- def table
52
- content_tag :table do
53
- head + body
52
+ def table
53
+ content_tag :table do
54
+ head + body
55
+ end
54
56
  end
55
- end
56
57
 
57
- def head
58
- content_tag :thead do
59
- content_tag :tr do
60
- columns.map(&:th).join.html_safe
58
+ def head
59
+ content_tag :thead do
60
+ content_tag :tr do
61
+ columns.map(&:th).join.html_safe
62
+ end
61
63
  end
62
64
  end
63
- end
64
65
 
65
- def body
66
- content_tag :tbody do
67
- current_page_records.map { |record| body_row(record) }.join.html_safe
66
+ def body
67
+ content_tag :tbody do
68
+ current_page_records.map { |record| body_row(record) }.join.html_safe
69
+ end
68
70
  end
69
- end
70
71
 
71
- def body_row(record)
72
- content_tag :tr do
73
- columns.map { |column| column.td(record) }.join.html_safe
72
+ def body_row(record)
73
+ content_tag :tr do
74
+ columns.map { |column| column.td(record) }.join.html_safe
75
+ end
74
76
  end
75
77
  end
76
78
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Helpers
4
- module AutoLink
5
- include ActionView::Helpers::UrlHelper
3
+ module RailsTableFor
4
+ module Helpers
5
+ module AutoLink
6
+ include ActionView::Helpers::UrlHelper
6
7
 
7
- def auto_link(record, text)
8
- path = Rails.application.routes.url_helpers.polymorphic_path(record)
9
- link_to(text, path)
8
+ def auto_link(record, text)
9
+ path = Rails.application.routes.url_helpers.polymorphic_path(record)
10
+ link_to(text, path)
11
+ end
10
12
  end
11
13
  end
12
14
  end
@@ -2,59 +2,61 @@
2
2
 
3
3
  require 'byebug'
4
4
 
5
- module Helpers
6
- module Paginate
7
- include ActionView::Helpers::UrlHelper
8
-
9
- protected
10
-
11
- def pagination_links
12
- return '' unless paginated?
13
-
14
- content_tag :div, class: 'pagination-links' do
15
- (1..num_pages).map do |page_number|
16
- if page_number == current_page_number
17
- page_number.to_s
18
- else
19
- page_link(page_number)
20
- end
21
- end.join.html_safe
5
+ module RailsTableFor
6
+ module Helpers
7
+ module Paginate
8
+ include ActionView::Helpers::UrlHelper
9
+
10
+ protected
11
+
12
+ def pagination_links
13
+ return '' unless paginated?
14
+
15
+ content_tag :div, class: 'pagination-links' do
16
+ (1..num_pages).map do |page_number|
17
+ if page_number == current_page_number
18
+ page_number.to_s
19
+ else
20
+ page_link(page_number)
21
+ end
22
+ end.join.html_safe
23
+ end
22
24
  end
23
- end
24
25
 
25
- def current_page_records
26
- if paginated?
27
- start_index = (current_page_number - 1) * page_size
28
- records.slice(start_index, page_size)
29
- else
30
- records
26
+ def current_page_records
27
+ if paginated?
28
+ start_index = (current_page_number - 1) * page_size
29
+ records.slice(start_index, page_size)
30
+ else
31
+ records
32
+ end
31
33
  end
32
- end
33
34
 
34
- private
35
+ private
35
36
 
36
- def paginated?
37
- page_size != nil
38
- end
37
+ def paginated?
38
+ page_size != nil
39
+ end
39
40
 
40
- def num_pages
41
- full_pages = record_count / page_size
42
- partial_final_page = (record_count % page_size).zero? ? 0 : 1
41
+ def num_pages
42
+ full_pages = record_count / page_size
43
+ partial_final_page = (record_count % page_size).zero? ? 0 : 1
43
44
 
44
- full_pages + partial_final_page
45
- end
45
+ full_pages + partial_final_page
46
+ end
46
47
 
47
- def current_page_number
48
- page = request_params[:page]&.to_i || 1
49
- raise 'Invalid page number' if page < 1 || page > num_pages
48
+ def current_page_number
49
+ page = request_params[:page]&.to_i || 1
50
+ raise 'Invalid page number' if page < 1 || page > num_pages
50
51
 
51
- page
52
- end
52
+ page
53
+ end
53
54
 
54
- def page_link(page_number)
55
- path = request_path
56
- query = request_params.merge({ page: page_number }).to_query
57
- link_to page_number, "#{path}?#{query}"
55
+ def page_link(page_number)
56
+ path = request_path
57
+ query = request_params.merge({ page: page_number }).to_query
58
+ link_to page_number, "#{path}?#{query}"
59
+ end
58
60
  end
59
61
  end
60
62
  end
@@ -2,26 +2,28 @@
2
2
 
3
3
  require 'rails_table_for/elements/table'
4
4
 
5
- module TableHelper
6
- include ActionView::Helpers::TagHelper
5
+ module RailsTableFor
6
+ module TableHelper
7
+ include ActionView::Helpers::TagHelper
7
8
 
8
- def table_for(records, **options)
9
- options.merge!(request_options)
10
- table = Elements::Table.new(records, options)
11
- yield table if block_given?
12
- table.to_s
13
- end
9
+ def table_for(records, **options)
10
+ options.merge!(request_options)
11
+ table = Elements::Table.new(records, options)
12
+ yield table if block_given?
13
+ table.to_s
14
+ end
14
15
 
15
- private
16
+ private
16
17
 
17
- def request_options
18
- {
19
- request_path: request.path,
20
- request_params: request.params.except(:action, :controller)
21
- }
18
+ def request_options
19
+ {
20
+ request_path: request.path,
21
+ request_params: request.params.except(:action, :controller)
22
+ }
23
+ end
22
24
  end
23
25
  end
24
26
 
25
27
  ActionView::Base.class_eval do
26
- include TableHelper
28
+ include RailsTableFor::TableHelper
27
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsTableFor
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  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.3.0
4
+ version: 0.3.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-04-07 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -197,6 +197,11 @@ files:
197
197
  - docs/Gemfile
198
198
  - docs/Gemfile.lock
199
199
  - docs/_config.yml
200
+ - docs/guides/auto-linking-rows.md
201
+ - docs/guides/customizing-column-headers.md
202
+ - docs/guides/customizing-row-values.md
203
+ - docs/guides/getting-started.md
204
+ - docs/guides/pagination.md
200
205
  - docs/index.md
201
206
  - lib/rails_table_for.rb
202
207
  - lib/rails_table_for/elements/block_column.rb
@@ -228,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
233
  - !ruby/object:Gem::Version
229
234
  version: '0'
230
235
  requirements: []
231
- rubygems_version: 3.0.3
236
+ rubygems_version: 3.1.2
232
237
  signing_key:
233
238
  specification_version: 4
234
239
  summary: HTML tables for ActiveRecord collections, made simple