rails_table_for 0.2.2 → 0.3.0

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: 155237f1a1d730b42f5163e83f94f851b333abb4629337028eb46921fdb803ea
4
- data.tar.gz: de618efd0f8849900c52e83db411a7960c12039cfc9e1e5437cc427e4cee7a44
3
+ metadata.gz: a2c69c3a95a5605f310ce4ef150157d44ab326a07d58b0b94ae984c61bc8059b
4
+ data.tar.gz: 7b6cbf7ddebf42a2cad86d6205e00018b36539be505ca41e01e2bc610dfc4f4f
5
5
  SHA512:
6
- metadata.gz: a936961884260f6ffed048e9838fa08b1b089903b5a8d6e81618076215452579e73400025f863ca97aebc3c95669eda880ede96e209b62d91cde3b93ceae4073
7
- data.tar.gz: 23f1e6a33f112248ca84233af07fcdc815857e348b86a1183cb5bc72b332ae295da34ce229fca428eae3000625cc380b7d541eeb6576c2c5c681a42e35043e47
6
+ metadata.gz: 51503314ccec53647609524aadee9b411a8725274e5c18a88b26d49c94b8fc4d42c2451f5ee40a5d546e2dbecc776983f6f0f8b2311f68a0e04378d7f82a5361
7
+ data.tar.gz: 144a74ce2af192f88e587535ec4278a4d46f4d2d92a750131a2834de1cf72f9de2ba6c56af266d0953af5fe86ab7c68232c3d7da949870acc874a65fc99a4028
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: request
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -1,4 +1,4 @@
1
- name: Tests
1
+ name: Build & Test
2
2
 
3
3
  on:
4
4
  push:
@@ -7,7 +7,7 @@ on:
7
7
  branches: [ master ]
8
8
 
9
9
  jobs:
10
- run_minitest:
10
+ build_and_test:
11
11
 
12
12
  runs-on: ubuntu-latest
13
13
 
@@ -18,10 +18,12 @@ jobs:
18
18
  with:
19
19
  ruby-version: 2.6.x
20
20
  - name: Install libsqlite3-dev
21
- run: |
22
- sudo apt-get install -y libsqlite3-dev
23
- - name: Build and test
21
+ run: sudo apt-get install -y libsqlite3-dev
22
+ - name: Build
24
23
  run: |
25
24
  gem install bundler
26
25
  bundle install --jobs 4 --retry 3
27
- bundle exec rake
26
+ - name: Rubocop
27
+ run: bundle exec rake rubocop
28
+ - name: Test
29
+ run: bundle exec rake
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Layout/LineLength:
5
+ Max: 100
6
+
7
+ Style/HashEachMethods:
8
+ Enabled: true
9
+
10
+ Style/HashTransformKeys:
11
+ Enabled: true
12
+
13
+ Style/HashTransformValues:
14
+ Enabled: true
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_table_for (0.2.2)
4
+ rails_table_for (0.3.0)
5
5
  rails (>= 5)
6
6
 
7
7
  GEM
@@ -62,6 +62,7 @@ GEM
62
62
  minitest (~> 5.1)
63
63
  tzinfo (~> 1.1)
64
64
  zeitwerk (~> 2.2)
65
+ ast (2.4.0)
65
66
  builder (3.2.4)
66
67
  bump (0.9.0)
67
68
  byebug (11.1.1)
@@ -72,7 +73,8 @@ GEM
72
73
  activesupport (>= 4.2.0)
73
74
  i18n (1.8.2)
74
75
  concurrent-ruby (~> 1.0)
75
- loofah (2.4.0)
76
+ jaro_winkler (1.5.4)
77
+ loofah (2.5.0)
76
78
  crass (~> 1.0.2)
77
79
  nokogiri (>= 1.5.9)
78
80
  mail (2.7.1)
@@ -87,6 +89,9 @@ GEM
87
89
  nio4r (2.5.2)
88
90
  nokogiri (1.10.9)
89
91
  mini_portile2 (~> 2.4.0)
92
+ parallel (1.19.1)
93
+ parser (2.7.0.5)
94
+ ast (~> 2.4.0)
90
95
  rack (2.2.2)
91
96
  rack-test (1.1.0)
92
97
  rack (>= 1.0, < 3)
@@ -116,7 +121,18 @@ GEM
116
121
  method_source
117
122
  rake (>= 0.8.7)
118
123
  thor (>= 0.20.3, < 2.0)
124
+ rainbow (3.0.0)
119
125
  rake (13.0.1)
126
+ rexml (3.2.4)
127
+ rubocop (0.80.1)
128
+ jaro_winkler (~> 1.5.1)
129
+ parallel (~> 1.10)
130
+ parser (>= 2.7.0.1)
131
+ rainbow (>= 2.2.2, < 4.0)
132
+ rexml
133
+ ruby-progressbar (~> 1.7)
134
+ unicode-display_width (>= 1.4.0, < 1.7)
135
+ ruby-progressbar (1.10.1)
120
136
  sprockets (4.0.0)
121
137
  concurrent-ruby (~> 1.0)
122
138
  rack (> 1, < 3)
@@ -127,8 +143,9 @@ GEM
127
143
  sqlite3 (1.4.2)
128
144
  thor (1.0.1)
129
145
  thread_safe (0.3.6)
130
- tzinfo (1.2.6)
146
+ tzinfo (1.2.7)
131
147
  thread_safe (~> 0.1)
148
+ unicode-display_width (1.6.1)
132
149
  websocket-driver (0.7.1)
133
150
  websocket-extensions (>= 0.1.0)
134
151
  websocket-extensions (0.1.4)
@@ -145,6 +162,7 @@ DEPENDENCIES
145
162
  nokogiri (~> 1.10, >= 1.10.9)
146
163
  rails_table_for!
147
164
  rake (~> 13.0, >= 13.0.1)
165
+ rubocop (~> 0.80, >= 0.80.1)
148
166
  sqlite3 (~> 1.4, >= 1.4.2)
149
167
 
150
168
  BUNDLED WITH
data/Rakefile CHANGED
@@ -24,4 +24,12 @@ Rake::TestTask.new(:test) do |t|
24
24
  t.verbose = false
25
25
  end
26
26
 
27
+ require 'rubocop/rake_task'
28
+
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
34
+
27
35
  task default: :test
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/railtie'
2
4
  require 'rails_table_for/table_helper'
3
5
 
@@ -1,20 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/helpers/auto_link'
2
4
  require 'rails_table_for/elements/column'
3
5
 
4
6
  module Elements
5
7
  class BlockColumn < Column
6
- include Helpers::AutoLink, ActionView::Helpers::TagHelper
8
+ include ActionView::Helpers::TagHelper
9
+ include Helpers::AutoLink
10
+
11
+ attr_reader :block, :title, :auto_link_enabled
12
+ private :block, :title, :auto_link_enabled
7
13
 
8
14
  def initialize(block, **options)
9
15
  @block = block
10
16
  @title = options[:title]
11
- @auto_link = options[:auto_link] || false
17
+ @auto_link_enabled = options[:auto_link] || false
12
18
  end
13
19
 
14
20
  def td(record)
15
- text = @block.call(record)
16
- content = @auto_link ? auto_link(record, text) : text
21
+ text = block.call(record)
22
+ content = auto_link_enabled ? auto_link(record, text) : text
17
23
  content_tag :td, content
18
24
  end
19
25
  end
20
- end
26
+ end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Elements
2
4
  class Column
3
5
  def th
4
- content_tag :th, @title
6
+ content_tag :th, title
5
7
  end
6
8
 
7
- def td(record)
9
+ def td(_)
8
10
  raise 'Not implemented'
9
11
  end
10
12
  end
11
- end
13
+ end
@@ -1,23 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/helpers/auto_link'
2
4
  require 'rails_table_for/elements/column'
3
5
 
4
6
  module Elements
5
7
  class FieldColumn < Column
6
- include Helpers::AutoLink, ActionView::Helpers::TagHelper
8
+ include ActionView::Helpers::TagHelper
9
+ include Helpers::AutoLink
10
+
11
+ attr_reader :field, :title, :auto_link_enabled
12
+ private :field, :title, :auto_link_enabled
7
13
 
8
14
  def initialize(field, **options)
9
- if field.nil?
10
- raise ArgumentError.new("Field cannot be nil")
11
- end
15
+ raise('Field cannot be nil', ArgumentError) if field.nil?
16
+
12
17
  @field = field
13
18
  @title = options[:title] || field.to_s.humanize
14
- @auto_link = options[:auto_link] || false
19
+ @auto_link_enabled = options[:auto_link] || false
15
20
  end
16
21
 
17
22
  def td(record)
18
- text = record.send(@field)
19
- content = @auto_link ? auto_link(record, text) : text
23
+ text = record.send(field)
24
+ content = auto_link_enabled ? auto_link(record, text) : text
20
25
  content_tag :td, content
21
26
  end
22
27
  end
23
- end
28
+ end
@@ -1,63 +1,77 @@
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
7
  module Elements
5
8
  class Table
9
+ include Helpers::Paginate
6
10
  include ActionView::Helpers::TagHelper
7
11
 
8
- attr_accessor :output_buffer
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
9
15
 
10
- def initialize(**options)
11
- columns = options[:columns] || []
12
- @columns = columns.map do |field|
13
- FieldColumn.new(field)
14
- end
15
- @options = options
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]
16
24
  end
17
25
 
18
- def column(field=nil, **options, &block)
19
- if field.nil? && !block_given?
20
- raise 'Must provide either field or block'
21
- end
22
-
26
+ def column(field = nil, **options, &block)
23
27
  if block_given?
24
- @columns << BlockColumn.new(block, options)
28
+ columns << BlockColumn.new(block, options)
29
+ elsif field
30
+ columns << FieldColumn.new(field, options)
25
31
  else
26
- @columns << FieldColumn.new(field, options)
32
+ raise 'Must provide either field or block'
27
33
  end
28
34
  end
29
35
 
30
- def build(records)
31
- return '' if records.nil? || records.empty?
32
- return '' if @columns.nil? || @columns.empty?
33
- table(records)
36
+ def to_s
37
+ return '' if record_count.zero?
38
+ return '' if columns.nil? || columns.empty?
39
+
40
+ draw
34
41
  end
35
42
 
36
43
  private
37
- def table(records)
38
- content_tag :table, class: @options[:class] do
39
- [head, body(records)].join.html_safe
44
+
45
+ def draw
46
+ content_tag :div do
47
+ table + pagination_links
48
+ end
49
+ end
50
+
51
+ def table
52
+ content_tag :table do
53
+ head + body
40
54
  end
41
55
  end
42
56
 
43
57
  def head
44
58
  content_tag :thead do
45
59
  content_tag :tr do
46
- @columns.map(&:th).join.html_safe
60
+ columns.map(&:th).join.html_safe
47
61
  end
48
62
  end
49
63
  end
50
64
 
51
- def body(records)
65
+ def body
52
66
  content_tag :tbody do
53
- records.map {|record| body_row(record) }.join.html_safe
67
+ current_page_records.map { |record| body_row(record) }.join.html_safe
54
68
  end
55
69
  end
56
70
 
57
71
  def body_row(record)
58
72
  content_tag :tr do
59
- @columns.map {|column| column.td(record) }.join.html_safe
73
+ columns.map { |column| column.td(record) }.join.html_safe
60
74
  end
61
75
  end
62
76
  end
63
- end
77
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Helpers
2
4
  module AutoLink
3
5
  include ActionView::Helpers::UrlHelper
@@ -7,4 +9,4 @@ module Helpers
7
9
  link_to(text, path)
8
10
  end
9
11
  end
10
- end
12
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'byebug'
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
22
+ end
23
+ end
24
+
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
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def paginated?
37
+ page_size != nil
38
+ end
39
+
40
+ def num_pages
41
+ full_pages = record_count / page_size
42
+ partial_final_page = (record_count % page_size).zero? ? 0 : 1
43
+
44
+ full_pages + partial_final_page
45
+ end
46
+
47
+ def current_page_number
48
+ page = request_params[:page]&.to_i || 1
49
+ raise 'Invalid page number' if page < 1 || page > num_pages
50
+
51
+ page
52
+ end
53
+
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}"
58
+ end
59
+ end
60
+ 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,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_table_for/elements/table'
2
4
 
3
5
  module TableHelper
4
6
  include ActionView::Helpers::TagHelper
5
7
 
6
8
  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
14
- end
15
- table.build(records)
9
+ options.merge!(request_options)
10
+ table = Elements::Table.new(records, options)
11
+ yield table if block_given?
12
+ table.to_s
13
+ end
14
+
15
+ private
16
+
17
+ def request_options
18
+ {
19
+ request_path: request.path,
20
+ request_params: request.params.except(:action, :controller)
21
+ }
16
22
  end
17
23
  end
18
24
 
19
25
  ActionView::Base.class_eval do
20
26
  include TableHelper
21
- end
27
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsTableFor
2
- VERSION = '0.2.2'
4
+ VERSION = '0.3.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.3.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: 2020-04-07 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
@@ -183,10 +204,10 @@ files:
183
204
  - lib/rails_table_for/elements/field_column.rb
184
205
  - lib/rails_table_for/elements/table.rb
185
206
  - lib/rails_table_for/helpers/auto_link.rb
207
+ - lib/rails_table_for/helpers/paginate.rb
186
208
  - lib/rails_table_for/railtie.rb
187
209
  - lib/rails_table_for/table_helper.rb
188
210
  - lib/rails_table_for/version.rb
189
- - lib/tasks/rails_table_for_tasks.rake
190
211
  - rails_table_for.gemspec
191
212
  homepage: https://github.com/acroos/rails_table_for
192
213
  licenses:
@@ -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