rails_table_for 0.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,80 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/elements/block_column'
2
4
  require 'rails_table_for/elements/field_column'
5
+ require 'rails_table_for/helpers/paginate'
3
6
 
4
- module Elements
5
- class Table
6
- include ActionView::Helpers::TagHelper
7
+ module RailsTableFor
8
+ module Elements
9
+ class Table
10
+ include Helpers::Paginate
11
+ include ActionView::Helpers::TagHelper
7
12
 
8
- attr_accessor :output_buffer
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
9
16
 
10
- def initialize(**options)
11
- columns = options[:columns] || []
12
- @columns = columns.map do |field|
13
- FieldColumn.new(field)
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
+ @table_class = options[:class]
14
26
  end
15
- @options = options
16
- end
17
27
 
18
- def column(field=nil, **options, &block)
19
- if field.nil? && !block_given?
20
- raise 'Must provide either field or block'
28
+ def column(field = nil, **options, &block)
29
+ if block_given?
30
+ columns << BlockColumn.new(block, options)
31
+ elsif field
32
+ columns << FieldColumn.new(field, options)
33
+ else
34
+ raise 'Must provide either field or block'
35
+ end
21
36
  end
22
37
 
23
- if block_given?
24
- @columns << BlockColumn.new(block, options)
25
- else
26
- @columns << FieldColumn.new(field, options)
38
+ def to_s
39
+ return '' if record_count.zero?
40
+ return '' if columns.empty?
41
+
42
+ draw
27
43
  end
28
- end
29
44
 
30
- def build(records)
31
- return '' if records.nil? || records.empty?
32
- return '' if @columns.nil? || @columns.empty?
33
- table(records)
34
- end
45
+ private
35
46
 
36
- private
37
- def table(records)
38
- content_tag :table, class: @options[:class] do
39
- [head, body(records)].join.html_safe
47
+ def draw
48
+ content_tag :div do
49
+ table + pagination_links
50
+ end
40
51
  end
41
- end
42
52
 
43
- def head
44
- content_tag :thead do
45
- content_tag :tr do
46
- @columns.map(&:th).join.html_safe
53
+ def table
54
+ content_tag :table, class: @table_class do
55
+ head + body
47
56
  end
48
57
  end
49
- end
50
58
 
51
- def body(records)
52
- content_tag :tbody do
53
- records.map {|record| body_row(record) }.join.html_safe
59
+ def head
60
+ content_tag :thead do
61
+ content_tag :tr do
62
+ columns.map(&:th).join.html_safe
63
+ end
64
+ end
65
+ end
66
+
67
+ def body
68
+ content_tag :tbody do
69
+ current_page_records.map { |record| body_row(record) }.join.html_safe
70
+ end
54
71
  end
55
- end
56
72
 
57
- def body_row(record)
58
- content_tag :tr do
59
- @columns.map {|column| column.td(record) }.join.html_safe
73
+ def body_row(record)
74
+ content_tag :tr do
75
+ columns.map { |column| column.td(record) }.join.html_safe
76
+ end
60
77
  end
61
78
  end
62
79
  end
63
- end
80
+ end
@@ -1,10 +1,14 @@
1
- module Helpers
2
- module AutoLink
3
- include ActionView::Helpers::UrlHelper
1
+ # frozen_string_literal: true
4
2
 
5
- def auto_link(record, text)
6
- path = Rails.application.routes.url_helpers.polymorphic_path(record)
7
- link_to(text, path)
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
8
12
  end
9
13
  end
10
- 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,21 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/elements/table'
2
4
 
3
- module TableHelper
4
- include ActionView::Helpers::TagHelper
5
+ module RailsTableFor
6
+ module TableHelper
7
+ include ActionView::Helpers::TagHelper
5
8
 
6
- def table_for(records, **options)
7
- options.merge({
8
- request_url: request.original_url,
9
- request_params: request.params
10
- })
11
- table = Elements::Table.new(options)
12
- if block_given?
13
- yield table
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
+ }
14
23
  end
15
- table.build(records)
16
24
  end
17
25
  end
18
26
 
19
27
  ActionView::Base.class_eval do
20
- include TableHelper
21
- 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.2'
4
+ VERSION = '0.4.0'
3
5
  end
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'minitest', '~> 5.14'
28
28
  spec.add_development_dependency 'nokogiri', '~> 1.10', '>= 1.10.9'
29
29
  spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
30
+ spec.add_development_dependency 'rubocop', '~> 0.80', '>= 0.80.1'
30
31
  spec.add_development_dependency 'sqlite3', '~> 1.4', '>= 1.4.2'
31
32
  end
32
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.2
4
+ version: 0.4.0
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-29 00:00:00.000000000 Z
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -132,6 +132,26 @@ dependencies:
132
132
  - - ">="
133
133
  - !ruby/object:Gem::Version
134
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
135
155
  - !ruby/object:Gem::Dependency
136
156
  name: sqlite3
137
157
  requirement: !ruby/object:Gem::Requirement
@@ -161,9 +181,10 @@ extensions: []
161
181
  extra_rdoc_files: []
162
182
  files:
163
183
  - ".github/ISSUE_TEMPLATE/bug_report.md"
164
- - ".github/ISSUE_TEMPLATE/new-user-story.md"
165
- - ".github/workflows/tests.yml"
184
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
185
+ - ".github/workflows/test.yml"
166
186
  - ".gitignore"
187
+ - ".rubocop.yml"
167
188
  - CHANGELOG.md
168
189
  - Gemfile
169
190
  - Gemfile.lock
@@ -176,6 +197,11 @@ files:
176
197
  - docs/Gemfile
177
198
  - docs/Gemfile.lock
178
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
179
205
  - docs/index.md
180
206
  - lib/rails_table_for.rb
181
207
  - lib/rails_table_for/elements/block_column.rb
@@ -183,10 +209,10 @@ files:
183
209
  - lib/rails_table_for/elements/field_column.rb
184
210
  - lib/rails_table_for/elements/table.rb
185
211
  - lib/rails_table_for/helpers/auto_link.rb
212
+ - lib/rails_table_for/helpers/paginate.rb
186
213
  - lib/rails_table_for/railtie.rb
187
214
  - lib/rails_table_for/table_helper.rb
188
215
  - lib/rails_table_for/version.rb
189
- - lib/tasks/rails_table_for_tasks.rake
190
216
  - rails_table_for.gemspec
191
217
  homepage: https://github.com/acroos/rails_table_for
192
218
  licenses:
@@ -207,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
207
233
  - !ruby/object:Gem::Version
208
234
  version: '0'
209
235
  requirements: []
210
- rubygems_version: 3.0.3
236
+ rubygems_version: 3.1.2
211
237
  signing_key:
212
238
  specification_version: 4
213
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,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :rails_table_for do
3
- # # Task goes here
4
- # end