rails_table_for 0.2.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsTableFor
4
+ module Helpers
5
+ module AutoLink
6
+ include ActionView::Helpers::UrlHelper
7
+
8
+ def auto_link(record, text)
9
+ path = Rails.application.routes.url_helpers.polymorphic_path(record)
10
+ link_to(text, path)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'byebug'
4
+
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
24
+ end
25
+
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
33
+ end
34
+
35
+ private
36
+
37
+ def paginated?
38
+ page_size != nil
39
+ end
40
+
41
+ def num_pages
42
+ full_pages = record_count / page_size
43
+ partial_final_page = (record_count % page_size).zero? ? 0 : 1
44
+
45
+ full_pages + partial_final_page
46
+ end
47
+
48
+ def current_page_number
49
+ page = request_params[:page]&.to_i || 1
50
+ raise 'Invalid page number' if page < 1 || page > num_pages
51
+
52
+ page
53
+ end
54
+
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
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsTableFor
2
4
  class Railtie < ::Rails::Railtie
3
5
  end
@@ -1,17 +1,29 @@
1
- require 'rails_table_for/table'
1
+ # frozen_string_literal: true
2
2
 
3
- module TableHelper
4
- include ActionView::Helpers::TagHelper
3
+ require 'rails_table_for/elements/table'
5
4
 
6
- def table_for(records, **options)
7
- table = Table.new(options)
8
- if block_given?
9
- yield table
5
+ module RailsTableFor
6
+ module TableHelper
7
+ include ActionView::Helpers::TagHelper
8
+
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
15
+
16
+ private
17
+
18
+ def request_options
19
+ {
20
+ request_path: request.path,
21
+ request_params: request.params.except(:action, :controller)
22
+ }
10
23
  end
11
- table.build(records)
12
24
  end
13
25
  end
14
26
 
15
27
  ActionView::Base.class_eval do
16
- include TableHelper
17
- end
28
+ include RailsTableFor::TableHelper
29
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsTableFor
2
- VERSION = '0.2.0'
4
+ VERSION = '0.3.2'
3
5
  end
@@ -25,7 +25,9 @@ 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'
30
+ spec.add_development_dependency 'rubocop', '~> 0.80', '>= 0.80.1'
29
31
  spec.add_development_dependency 'sqlite3', '~> 1.4', '>= 1.4.2'
30
32
  end
31
33
 
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.3.2
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-06-08 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
@@ -112,6 +132,26 @@ dependencies:
112
132
  - - ">="
113
133
  - !ruby/object:Gem::Version
114
134
  version: 13.0.1
135
+ - !ruby/object:Gem::Dependency
136
+ name: rubocop
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '0.80'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 0.80.1
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '0.80'
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 0.80.1
115
155
  - !ruby/object:Gem::Dependency
116
156
  name: sqlite3
117
157
  requirement: !ruby/object:Gem::Requirement
@@ -141,9 +181,11 @@ extensions: []
141
181
  extra_rdoc_files: []
142
182
  files:
143
183
  - ".github/ISSUE_TEMPLATE/bug_report.md"
144
- - ".github/ISSUE_TEMPLATE/new-user-story.md"
145
- - ".github/workflows/ruby.yml"
184
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
185
+ - ".github/workflows/test.yml"
146
186
  - ".gitignore"
187
+ - ".rubocop.yml"
188
+ - CHANGELOG.md
147
189
  - Gemfile
148
190
  - Gemfile.lock
149
191
  - LICENSE
@@ -155,16 +197,22 @@ files:
155
197
  - docs/Gemfile
156
198
  - docs/Gemfile.lock
157
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
158
205
  - docs/index.md
159
206
  - 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
207
+ - lib/rails_table_for/elements/block_column.rb
208
+ - lib/rails_table_for/elements/column.rb
209
+ - lib/rails_table_for/elements/field_column.rb
210
+ - lib/rails_table_for/elements/table.rb
211
+ - lib/rails_table_for/helpers/auto_link.rb
212
+ - lib/rails_table_for/helpers/paginate.rb
163
213
  - lib/rails_table_for/railtie.rb
164
- - lib/rails_table_for/table.rb
165
214
  - lib/rails_table_for/table_helper.rb
166
215
  - lib/rails_table_for/version.rb
167
- - lib/tasks/rails_table_for_tasks.rake
168
216
  - rails_table_for.gemspec
169
217
  homepage: https://github.com/acroos/rails_table_for
170
218
  licenses:
@@ -185,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
233
  - !ruby/object:Gem::Version
186
234
  version: '0'
187
235
  requirements: []
188
- rubygems_version: 3.0.3
236
+ rubygems_version: 3.1.2
189
237
  signing_key:
190
238
  specification_version: 4
191
239
  summary: HTML tables for ActiveRecord collections, made simple
@@ -1,20 +0,0 @@
1
- ---
2
- name: New User Story
3
- about: Request for new user functionality
4
- title: "[Feature Request]"
5
- labels: ''
6
- assignees: acroos
7
-
8
- ---
9
-
10
- ## Story
11
-
12
- What is the new functionality? How will it be used? Please provide a code snippet if possible
13
-
14
- ## Acceptance Criteria
15
-
16
- - List of criteria to consider this task finished
17
-
18
- ## Additional Details
19
-
20
- - Additional implementation details that may prove helpful
@@ -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,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :rails_table_for do
3
- # # Task goes here
4
- # end