wor-paginate 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2465192c522659e0f6e779d73cda2dfaa3fa47aca0966ebc9fd4292587143cea
4
- data.tar.gz: d71ab589396a9f76e25849e63bec31a1fe5aafcd1dd54c3b9ca79104957cbee7
3
+ metadata.gz: 4481aa957c0963f107c5f1bdd9b2c6859cf91930aa7737e1a184a5634ad2bddf
4
+ data.tar.gz: 3a66f12b6c3348f7e43aa98586218d812ba6df36f6917a926d8bd1abc19caf61
5
5
  SHA512:
6
- metadata.gz: 8ed75b64b43ffe168a35a09a054532c384d51470e3942964b8a60ac191644b264e8d42a59e73ceea3485a0cc7680162a74dcd9d81b5581a110bb8510f965dfcc
7
- data.tar.gz: 93143a274cdbd3bbaffe3898fa4038de9173b6d916eab033e5ae13c8fcaceb63baefc8c7a4d0a6c6ffbb4fc2f6513c2d68769a533fdc05416d3d5336c256dfd8
6
+ metadata.gz: 8924bd31085db92676e53a290e5d9ae109295f15af0a908b798dec323a99a5c9b97a1b367cdc17f219126167bf4d6f703cc44a419c5d06acee3d8fe81d96c2d8
7
+ data.tar.gz: 989d4fb9cef8a3e149178c1f1a777195f1b63b54ddf6e2be10e7612b11bb95a04ba6b3280680a90918e56473efba951439bfeb362eb681a98e4f7cdfe5a33254
@@ -0,0 +1,52 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ name: "Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}"
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby: ['3.2', '3.3', '3.4']
16
+ rails: ['6.1', '7.0', '7.1', '7.2', '8.0', '8.1']
17
+ steps:
18
+ - uses: actions/checkout@v7
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+ bundler-cache: false
23
+ - name: Generate appraisal gemfiles
24
+ run: |
25
+ bundle install
26
+ bundle exec appraisal generate
27
+ - name: Install dependencies for this Rails version
28
+ env:
29
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile
30
+ run: bundle install
31
+ - name: Prepare test database
32
+ env:
33
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile
34
+ RAILS_ENV: test
35
+ working-directory: spec/dummy
36
+ run: bundle exec rails db:schema:load
37
+ - name: Run tests
38
+ env:
39
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails }}.gemfile
40
+ run: bundle exec rspec
41
+
42
+ lint:
43
+ name: RuboCop
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: actions/checkout@v7
47
+ - uses: ruby/setup-ruby@v1
48
+ with:
49
+ ruby-version: '3.4'
50
+ bundler-cache: true
51
+ - name: Run RuboCop
52
+ run: bundle exec rubocop lib spec --format simple
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  spec/dummy/db/*.sqlite3
2
2
  spec/dummy/db/*.sqlite3-journal
3
+ /test.sqlite3
3
4
  spec/dummy/log/*.log
4
5
  spec/dummy/tmp/
5
6
 
@@ -330,3 +331,13 @@ $RECYCLE.BIN/
330
331
  *.lnk
331
332
 
332
333
  # End of https://www.gitignore.io/api/ruby,rubymine,rails,emacs,vim,sublimetext,osx,macos,linux,windows
334
+
335
+ ### Claude Code / superpowers ###
336
+ CLAUDE.md
337
+ .claude/
338
+ docs/superpowers/
339
+ .superpowers/
340
+
341
+ ### Appraisal ###
342
+ /gemfiles/*.gemfile
343
+ /gemfiles/*.gemfile.lock
data/.overcommit.yml ADDED
@@ -0,0 +1,16 @@
1
+ PrePush:
2
+ ALL:
3
+ problem_on_unmodified_line: report
4
+ requires_files: false
5
+ quiet: false
6
+
7
+ RuboCop:
8
+ enabled: true
9
+ command: ['bundle', 'exec', 'rubocop']
10
+
11
+ CustomScript:
12
+ enabled: true
13
+ description: 'Run RSpec with 100% coverage gate'
14
+ command: ['bundle', 'exec', 'rspec']
15
+ env:
16
+ BUNDLE_GEMFILE: gemfiles/rails_8.1.gemfile
data/.rubocop.yml CHANGED
@@ -1,19 +1,23 @@
1
+ plugins:
2
+ - rubocop-rspec
3
+
1
4
  AllCops:
2
5
  Exclude:
3
6
  - wor-paginate.gemspec
4
- - spec/spy.rb
5
7
  - spec/dummy/db/**/*
6
8
  - spec/dummy/config/**/*
7
- - spec/spec_helper.rb
8
- TargetRubyVersion: 2.3.8
9
+ - spec/dummy/bin/**/*
10
+ TargetRubyVersion: 3.2
11
+ NewCops: disable
12
+ SuggestExtensions: false
9
13
 
10
- Documentation:
14
+ Style/Documentation:
11
15
  Enabled: false
12
16
 
13
- LineLength:
17
+ Layout/LineLength:
14
18
  Max: 99
15
19
 
16
- FrozenStringLiteralComment:
20
+ Style/FrozenStringLiteralComment:
17
21
  Enabled: false
18
22
 
19
23
  Metrics/BlockLength:
@@ -23,9 +27,7 @@ Metrics/BlockLength:
23
27
 
24
28
  # rubocop-rspec custom configurations
25
29
 
26
- require: rubocop-rspec
27
-
28
- Style/VariableNumber:
30
+ Naming/VariableNumber:
29
31
  EnforcedStyle: snake_case
30
32
 
31
33
  RSpec/ExampleLength:
@@ -46,8 +48,13 @@ Lint/AmbiguousBlockAssociation:
46
48
  RSpec/LetSetup:
47
49
  Enabled: false
48
50
 
49
- RSpec/FilePath:
51
+ # matchers_spec.rb describes DummyModelsController to test the be_paginated
52
+ # matcher against real responses, not to test the controller itself, so its
53
+ # name doesn't follow the described-class convention on purpose.
54
+ RSpec/SpecFilePathFormat:
55
+ Exclude:
56
+ - spec/matchers_spec.rb
57
+
58
+ RSpec/SpecFilePathSuffix:
50
59
  Exclude:
51
- - spec/dummy_controller_without_gems_spec.rb
52
60
  - spec/matchers_spec.rb
53
- - spec/dummy_controller_spec.rb
data/Appraisals ADDED
@@ -0,0 +1,41 @@
1
+ # sqlite3 is pinned per Rails version here, not in the Gemfile: activerecord
2
+ # 6.1/7.0 hard-pin "~> 1.4" internally, 8.0/8.1 need ">= 2.1", and Bundler
3
+ # won't allow the same gem pinned twice in one Gemfile.
4
+
5
+ appraise 'rails-6.1' do
6
+ gem 'rails', '~> 6.1.0'
7
+
8
+ # activesupport 6.1.x requires mutex_m without declaring it; Ruby 3.4+ no
9
+ # longer bundles it by default.
10
+ gem 'mutex_m'
11
+
12
+ # Same issue for benchmark, removed by default in Ruby 4.0.
13
+ gem 'benchmark'
14
+
15
+ gem 'sqlite3', '~> 1.4'
16
+ end
17
+
18
+ appraise 'rails-7.0' do
19
+ gem 'rails', '~> 7.0.0'
20
+ gem 'sqlite3', '~> 1.4'
21
+ end
22
+
23
+ appraise 'rails-7.1' do
24
+ gem 'rails', '~> 7.1.0'
25
+ gem 'sqlite3', '~> 2.9'
26
+ end
27
+
28
+ appraise 'rails-7.2' do
29
+ gem 'rails', '~> 7.2.0'
30
+ gem 'sqlite3', '~> 2.9'
31
+ end
32
+
33
+ appraise 'rails-8.0' do
34
+ gem 'rails', '~> 8.0.0'
35
+ gem 'sqlite3', '~> 2.9'
36
+ end
37
+
38
+ appraise 'rails-8.1' do
39
+ gem 'rails', '~> 8.1.0'
40
+ gem 'sqlite3', '~> 2.9'
41
+ end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## Change log
2
2
 
3
+ ### V0.4.0
4
+ * [#1](https://github.com/icoluccio/wor-paginate/pull/1) Add Rails 8.0/8.1 support, HTTP-level request specs, and rubocop cleanup - [@icoluccio](https://github.com/icoluccio).
5
+
6
+ ### V0.3.0
7
+ * [#94](https://github.com/Wolox/wor-paginate/pull/94) Add Panko formatter - [@blacksam07](https://github.com/blacksam07).
8
+ * [#96](https://github.com/Wolox/wor-paginate/pull/96) Dynamic Adapters - [@juanpablo-rojas](https://github.com/juanpablo-rojas).
9
+
3
10
  ### V0.2.0
4
11
  * [#95](https://github.com/Wolox/wor-paginate/pull/95) Added total_count option to overwrite the count in render_paginated - [@juanpablo-rojas](https://github.com/juanpablo-rojas).
5
12
  * [#93](https://github.com/Wolox/wor-paginate/pull/93) Infinite scroll - [@mnmallea](https://github.com/mnmallea).
data/Gemfile CHANGED
@@ -14,22 +14,25 @@ gemspec
14
14
  # gem 'byebug', group: [:development, :test]
15
15
 
16
16
  group :development, :test do
17
- gem 'active_model_serializers', '~> 0.10.0'
18
- gem 'bundler', '>= 2.0.1'
19
- gem 'byebug', '~> 9.0'
20
- gem 'codeclimate-test-reporter', '~> 1.0.0'
21
- gem 'database_cleaner', '~> 1.6.0'
22
- gem 'factory_girl_rails', '~> 4.7.0'
23
- gem 'faker', '~> 1.7.0'
24
- gem 'generator_spec', '~> 0.9.0'
25
- gem 'kaminari', '~> 0.17.0'
26
- gem 'rake', '~> 10.0'
27
- gem 'rspec', '~> 3.0'
28
- gem 'rspec-rails', '~> 3.6.0'
29
- gem 'rubocop', '~> 0.49.1'
30
- gem 'rubocop-rspec', '~> 1.15.0'
31
- gem 'simplecov', '~> 0.13.0'
32
- gem 'sqlite3', '~> 1.4'
33
- gem 'webmock', '~> 3.0.0'
34
- gem 'will_paginate', '~> 3.1.0'
17
+ gem 'active_model_serializers', '~> 0.10.16'
18
+ gem 'appraisal', '~> 2.5'
19
+ gem 'byebug', '~> 13.0'
20
+ gem 'database_cleaner-active_record', '~> 2.2', require: 'database_cleaner/active_record'
21
+ gem 'factory_bot_rails', '~> 6.5'
22
+ gem 'faker', '~> 3.8'
23
+ gem 'generator_spec', '~> 0.10'
24
+ gem 'kaminari', '~> 1.2'
25
+ gem 'overcommit', '~> 0.71'
26
+ gem 'panko_serializer', '~> 0.8.5'
27
+ gem 'puma', '~> 6.0'
28
+ gem 'rake', '~> 13.0'
29
+ gem 'rspec', '~> 3.13'
30
+ gem 'rspec-rails', '>= 6.0', '< 9'
31
+ gem 'rubocop', '~> 1.88'
32
+ gem 'rubocop-rspec', '~> 3.10'
33
+ gem 'simplecov', '~> 0.22'
34
+ # sqlite3 is pinned per Rails version in Appraisals instead, since the
35
+ # required range differs across supported versions.
36
+ gem 'webmock', '~> 3.26'
37
+ gem 'will_paginate', '~> 4.0'
35
38
  end
data/LICENSE.md CHANGED
@@ -1,6 +1,7 @@
1
1
  The MIT License (MIT)
2
2
 
3
3
  Copyright (c) 2017 Wolox
4
+ Copyright (c) 2026 Ignacio Coluccio
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
7
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  Wor::Paginate
2
2
  =============
3
3
 
4
- [![Build Status](https://travis-ci.org/Wolox/wor-paginate.svg?branch=master)](https://travis-ci.org/Wolox/wor-paginate)
4
+ [![CI](https://github.com/icoluccio/wor-paginate/actions/workflows/ci.yml/badge.svg)](https://github.com/icoluccio/wor-paginate/actions/workflows/ci.yml)
5
5
  [![Gem Version](https://badge.fury.io/rb/wor-paginate.svg)](https://badge.fury.io/rb/wor-paginate)
6
- [![Code Climate](https://codeclimate.com/github/Wolox/wor-paginate/badges/gpa.svg)](https://codeclimate.com/github/Wolox/wor-paginate)
7
6
 
8
7
  # Table of contents
9
8
  - [Description](#description)
@@ -14,6 +13,7 @@ Wor::Paginate
14
13
  - [Custom serializers](#custom-serializers)
15
14
  - [Custom options](#custom-options)
16
15
  - [Custom formatters](#custom-formatters)
16
+ - [Custom adapters](#custom-adapters)
17
17
  - [Working with Kaminari or will_paginate](#working-with-kaminari-or-will_paginate)
18
18
  - [Test helpers](#test-helpers)
19
19
  - [Contributing](#contributing)
@@ -48,7 +48,7 @@ Then you can run `rails generate wor:paginate:install` to create the initializer
48
48
 
49
49
  ## Usage
50
50
  ### Basic usage
51
- The basic use case is to paginate using default values. This is achieved by including the module in a controller and calling render_paginate in the method that needs pagination.
51
+ The basic use case is to paginate using default values. This is achieved by including the module in a controller and calling render_paginated in the method that needs pagination.
52
52
  ```ruby
53
53
  class DummyModelsController < ApplicationController
54
54
  include Wor::Paginate
@@ -91,11 +91,11 @@ The response to the index will then be:
91
91
  "previous_page": null,
92
92
  "next_page": 2,
93
93
  "previous_page_url": null,
94
- "next_page_url": "http://api.example.com/users?page=2
94
+ "next_page_url": "http://api.example.com/users?page=2"
95
95
  }
96
96
  ```
97
97
 
98
- Page number is passed through the `page` option of the `render_paginated` method. If none is supplied, `params[:page]` will be used, (or the default parameter configured in the initializer).
98
+ Page number is passed through the `page` option of the `render_paginated` method. If none is supplied, `params[:page]` will be used (or the default parameter configured in the initializer).
99
99
  By default, if the page parameter is not present we will use 1 as the page (or the default `page` parameter configured in the initializer).
100
100
  The amount of items is passed through the `limit` option of the `render_paginated` method. If none is supplied, `params[:limit]` will be used (or the default parameter configured in the initializer). Default is 25.
101
101
  The default serializer and formatter will be used.
@@ -110,7 +110,7 @@ where the serializer is just an [`ActiveModel::Serializer`](https://github.com/r
110
110
 
111
111
  #### Custom options
112
112
  ##### max_limit
113
- The max amount of items is passed through the `max_limit` option, You can set the value in the initializer or in the `render_paginated` method, (If none is supplied, take the default value configured in the initializer). Default is 50.
113
+ The max amount of items is passed through the `max_limit` option. You can set the value in the initializer or in the `render_paginated` method (If none is supplied, take the default value configured in the initializer). Default is 50.
114
114
 
115
115
  ```ruby
116
116
  render_paginated DummyModel, max_limit: 100
@@ -134,7 +134,7 @@ end
134
134
  ```
135
135
 
136
136
  ##### total_count
137
- You can overwrite the `total_count` pagination param by passing it as a single option to the method. This could be used if the whole collection to be paginated is complex and has the risk to broke when counting all the records.
137
+ You can overwrite the `total_count` pagination param by passing it as a single option to the method. This could be used if the whole collection to be paginated is complex and has the risk to break when counting all the records.
138
138
 
139
139
  ```ruby
140
140
  render_paginated DummyModel, total_count: 50
@@ -143,7 +143,7 @@ You can overwrite the `total_count` pagination param by passing it as a single o
143
143
  ##### preserve_records
144
144
  > WARNING: This option only works with an ActiveRecord collection.
145
145
 
146
- Preserve records option can be added to `render_paginated` to mantain current records. This allow to navigate pages like an infinite scroll without adding new records when switching pages.
146
+ Preserve records option can be added to `render_paginated` to maintain current records. This allows to navigate pages like an infinite scroll without adding new records when switching pages.
147
147
 
148
148
  - Timestamp mode (default)
149
149
  ```ruby
@@ -170,17 +170,17 @@ end
170
170
  ```
171
171
 
172
172
 
173
- #### Custom formatters
173
+ ### Custom formatters
174
174
  A formatter is an object that defines the output of the render_paginated method. In case the application needs a different format for a request, it can be passed to the `render_paginated` method using the `formatter` option:
175
175
  ```ruby
176
176
  render_paginated DummyModel, formatter: CustomFormatter
177
177
  ```
178
178
  or it can also be set as a default in the initializer.
179
179
 
180
- A new formatter can be created inheriting from the default one. The `format` method should be redefined returning something that can be converted to json.
180
+ A new formatter can be created inheriting from the default one. The `format` method should be redefined returning something that can be converted to JSON.
181
181
 
182
182
  ```ruby
183
- class CustomFormatter < Wor::Paginate::Formatter
183
+ class CustomFormatter < Wor::Paginate::Formatters::Base
184
184
  def format
185
185
  { page: serialized_content, current: current_page }
186
186
  end
@@ -195,8 +195,76 @@ Available helper methods are:
195
195
  * `paginated_content`: its class depends on the original content passed to render_paginated, it's the paginated but not yet serialized content.
196
196
  * `serialized_content`: array with all the items after going through the serializer (either the default or a supplied one)
197
197
 
198
+
199
+ ### Custom adapters
200
+ An adapter is an object that defines how to show the rendered content, and how to calculate several methods of the pagination, such as 'total_count', 'total_pages', 'paginated_content' among others. In case the application needs a different adapter or a custom one, it can be passed to the `render_paginated` method using the `adapter` option:
201
+ ```ruby
202
+ render_paginated DummyModel, adapter: CustomAdapter
203
+ ```
204
+ or it can also be set as a default in the initializer.
205
+
206
+ A new adapter can be created inheriting from the default Base Adapter. Some methods must be redefined in order to make the adapter "adaptable" to the content that will be rendered.
207
+ Below is an example of a simple possible CustomAdapter that extends from the base Adapter.
208
+
209
+ ```ruby
210
+ class CustomAdapter < Wor::Paginate::Adapters::Base
211
+ def required_methods
212
+ %i[count]
213
+ end
214
+
215
+ def paginated_content
216
+ @paginated_content ||= @content.first(5)
217
+ end
218
+
219
+ def total_pages
220
+ (total_count / @limit.to_f).ceil
221
+ end
222
+
223
+ def total_count
224
+ @content.count
225
+ end
226
+
227
+ delegate :count, to: :paginated_content
228
+ end
229
+ ```
230
+ Here's a brief explanation on every overwritten method in this CustomAdapter example:
231
+
232
+ ##### `#required_methods`
233
+ These will be the methods (as symbols) that the content to be rendered has to support. The next expression will be evaluated for every method added here: `@content.respond_to? method`. All required_methods must answer `true` to the previous expression, in order to make the adapter "adaptable" for the content. For example, if we rendered an ActiveRecord::Relation, this CustomAdapter would be adaptable because an ActiveRecord::Relation responds to the `#count` method. At least one symbol has to be returned in this method, otherwise the adapter won't be able to render content.
234
+
235
+ ##### `#paginated_content`
236
+ This is how the content will be shown. As the content comes in the inherited instance variable `@content`, we can transform the content however we want. In the CustomAdapter example, the first 5 records will always be shown.
237
+
238
+ ##### `#total_pages`
239
+ This could be defined as the number of pages, given the limit requested. As the other values, this can be a custom number of pages, depending on your needs. For this example, this number is just an integer calculation of the total pages, depending on the limit. Also, like `@content`, we are inheriting the `@limit` variable, which allows us to operate with it however we want.
240
+
241
+ ##### `#total_count`
242
+ This will be the number that will tell us 'how many records is returning the request'. Again, we can customize it however we want. For this particular example this will be just the count of `@content`.
243
+
244
+ ##### `#count` method as delegate
245
+ At the end of the CustomAdapter we are delegating the `#count` method to the `paginated_content`. This is because the Base Adapter delegates that method to the inherited adapter, so our custom adapter has to know "how to calculate" that method, that's why we are defining a `#count` method in the delegation (It is always mandatory to define the `#count` method in a custom adapter, whether it is a method definition or a delegate).
246
+
247
+ If the content is an ActiveRecord::Relation, for example, this adapter would work, because `paginated_content` would become an ActiveRecord::Relation, which actually knows "how to calculate" the count method. This works as a delegate, because ActiveRecord::Relation has an internal `#count` definition, but we would have to provide the needed method definition if it is a custom method, or if we want a custom behaviour of a known method.
248
+
249
+ ##### Other available methods to overwrite
250
+ `Wor::Paginate::Adapters::Base` also has implementations for `#next_page` and `#previous_page` methods (which calculate the number of the next and previous pages, respectively). If you want, you can also overwrite those methods, to calculate custom 'next' and 'previous' page numbers.
251
+
252
+ To understand better the implementation of the Base Adapter and how you could overwrite methods in order to make a functional Custom Adapter, take a look at its definition in: [Base adapter Class](https://github.com/icoluccio/wor-paginate/blob/main/lib/wor/paginate/adapters/base.rb).
253
+ Keep in mind that an instance of your Custom Adapter must answer `true` to the `#adapt?` method inherited from the Base Adapter, in order to make it "adaptable" to the content.
254
+
255
+ #### Adapters Operations
256
+ There are also helper methods available to dynamically operate the gem's adapters, so you can configure them, whether in the initializer or in an internal part of your application. Once you include the gem, you'll be provided with the following methods, inside the Config module:
257
+ * `Wor::Paginate::Config.add_adapter(adapter)`: Add a specific adapter to the array of the gem's adapters. The 'adapter' variable must be a Class reference to an Adapter Class (that class has to have a similar structure as the CustomAdapter example above).
258
+ * `Wor::Paginate::Config.remove_adapter(adapter)`: Remove a specific adapter from the array of the gem's adapters.
259
+ * `Wor::Paginate::Config.clear_adapters`: This method empties the array of the gem's adapters.
260
+ * `Wor::Paginate::Config.adapters`: Returns all the current internal adapters inside the gem.
261
+ * `Wor::Paginate::Config.reset_adapters!`: This helper resets the gem's adapters to its default array of adapters. You can see how the array of default adapters looks like at the beginning of the `Wor::Paginate::Config` module: [Config module](https://github.com/icoluccio/wor-paginate/blob/main/lib/wor/paginate/config.rb).
262
+
263
+ When the gem paginates, it tries to adapt the content to the first adapter that is "adaptable" for the content (unless a custom adapter has been passed to render_paginated or a default_adapter has been defined in the initializer). So beware of which adapters (and in which order) you are leaving in the `Wor::Paginate::Config.adapters` array, because depending on those, the gem will try to adapt the content.
264
+
265
+
198
266
  ### Working with Kaminari or will_paginate
199
- If either Kaminari or will_paginate are required in the project, Wor::Paginate will use them for pagination with no code or configuration change.
267
+ If either Kaminari or will_paginate is required in the project, Wor::Paginate will use them for pagination with no code or configuration change.
200
268
 
201
269
  ### Test helpers
202
270
  You can use the `be_paginated` matcher to test your endpoints. It also accepts the `with` chain method to receive a formatter.
@@ -232,38 +300,61 @@ describe YourController do
232
300
  end
233
301
  ```
234
302
 
303
+ ### Working with panko-serializer
304
+
305
+ The default formatter is [Active Model Serializer](https://github.com/rails-api/active_model_serializers).
306
+ If you want to change it, you should replace the formatter with another one. In this section, we are going to work with `PankoFormatter`
307
+
308
+ #### example
309
+ ```ruby
310
+ Wor::Paginate.configure do |config|
311
+ config.formatter = Wor::Paginate::Formatters::PankoFormatter
312
+ end
313
+ ```
314
+ and next, pass the specific serializer that you can use in the specific endpoint
315
+
316
+ ```ruby
317
+ def index
318
+ render_paginated DummyModel, each_serializer: DummyModelPankoSerializer
319
+ end
320
+ ```
235
321
  ## Contributing
236
322
 
237
323
  1. Fork it
238
- 2. Create your feature branch (`git checkout -b my-new-feature`)
239
- 3. Commit your changes (`git commit -am 'Add some feature'`)
240
- 4. Run rubocop lint (`bundle exec rubocop -R --format simple`)
241
- 5. Run rspec tests (`bundle exec rspec`)
242
- 6. Push your branch (`git push origin my-new-feature`)
243
- 7. Create a new Pull Request to `master` branch
324
+ 2. Run `bundle install && bundle exec appraisal generate` once, to install dependencies and generate the per-Rails-version gemfiles (`gemfiles/rails_*.gemfile`) used for testing
325
+ 3. Run `bundle exec overcommit --install` once, to enable the pre-push hook (runs RuboCop and the full spec suite with its 100%-coverage gate automatically on every `git push`)
326
+ 4. Create your feature branch (`git checkout -b my-new-feature`)
327
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
328
+ 6. Run RuboCop lint (`bundle exec rubocop lib spec --format simple`)
329
+ 7. Run rspec tests (`BUNDLE_GEMFILE=gemfiles/rails_8.1.gemfile bundle exec rspec`)
330
+ 8. Push your branch (`git push origin my-new-feature`) — the pre-push hook re-verifies both automatically
331
+ 9. Create a new Pull Request to `main` branch
244
332
 
245
333
  ## Releases
246
- 📢 [See what's changed in a recent version](https://github.com/Wolox/wor-paginate/releases)
334
+ 📢 [See what's changed in a recent version](https://github.com/icoluccio/wor-paginate/releases)
247
335
 
248
336
  ## About ##
249
337
 
250
- The current maintainers of this gem are :
251
- * [Martín Mallea](https://github.com/mnmallea)
338
+ The current maintainer of this gem is:
339
+ * [Ignacio Coluccio](https://github.com/icoluccio)
252
340
 
253
341
  This project was developed by:
254
- * [Hugo Farji](https://github.com/hdf1986)
255
342
  * [Ignacio Coluccio](https://github.com/icoluccio)
343
+ * [Martín Mallea](https://github.com/mnmallea)
344
+ * [Samir Tapiero](https://github.com/blacksam07)
345
+ * [Hugo Farji](https://github.com/hdf1986)
256
346
  * [Alan Halatian](https://github.com/alanhala)
257
347
 
258
- At [Wolox](http://www.wolox.com.ar)
348
+ Originally at [Wolox](http://www.wolox.com.ar)
259
349
 
260
350
  [![Wolox](https://raw.githubusercontent.com/Wolox/press-kit/master/logos/logo_banner.png)](http://www.wolox.com.ar)
261
351
 
262
352
  ## License
263
353
 
264
- **wor-paginate** is available under the MIT [license](https://raw.githubusercontent.com/Wolox/wor-paginate/master/LICENSE.md).
354
+ **wor-paginate** is available under the MIT [license](https://raw.githubusercontent.com/icoluccio/wor-paginate/main/LICENSE.md).
265
355
 
266
356
  Copyright (c) 2017 Wolox
357
+ Copyright (c) 2026 Ignacio Coluccio
267
358
 
268
359
  Permission is hereby granted, free of charge, to any person obtaining a copy
269
360
  of this software and associated documentation files (the "Software"), to deal
@@ -6,10 +6,10 @@ Wor::Paginate.configure do |config|
6
6
  config.per_page_param = :limit
7
7
 
8
8
  # In case you want to use other format for your response, you can override our formatter here
9
- # You can extend from Wor::Paginate::Formatter and override the 'format' method
9
+ # You can extend from Wor::Paginate::Formatters::Base and override the 'format' method
10
10
  # For more info about available methods for formatters see:
11
- # https://github.com/Wolox/wor-paginate/blob/master/lib/wor/paginate/formatter.rb
12
- # config.formatter = Wor::Paginate::Formatter
11
+ # https://github.com/Wolox/wor-paginate/blob/master/lib/wor/paginate/formatters/base.rb
12
+ # config.formatter = Wor::Paginate::Formatters::AmsFormatter
13
13
 
14
14
  # Configure a default adapter to use on pagination
15
15
  # config.default_adapter = nil
@@ -22,4 +22,19 @@ Wor::Paginate.configure do |config|
22
22
  # Wor::Paginate::Adapters::Kaminari
23
23
  # Wor::Paginate::Adapters::ActiveRecord
24
24
  # Wor::Paginate::Adapters::Enumerable
25
+
26
+ # Custom adapters
27
+
28
+ # config.adapters = [
29
+ # Adapters::KaminariAlreadyPaginated,
30
+ # Adapters::WillPaginateAlreadyPaginated,
31
+ # Adapters::WillPaginate,
32
+ # Adapters::Kaminari,
33
+ # Adapters::ActiveRecord,
34
+ # Adapters::Enumerable
35
+ # ]
36
+ # config.add_adapter(adapter)
37
+ # config.remove_adapter(adapter)
38
+ # config.clear_adapters
39
+ # config.reset_adapters!
25
40
  end
@@ -2,7 +2,7 @@ module Wor
2
2
  module Paginate
3
3
  module Generators
4
4
  class InstallGenerator < Rails::Generators::Base
5
- source_root File.expand_path('../../../templates', __FILE__)
5
+ source_root File.expand_path('../../templates', __dir__)
6
6
  desc 'Creates Wor-Paginate initializer for your application'
7
7
 
8
8
  def copy_initializer
@@ -24,13 +24,14 @@ module Wor
24
24
  end
25
25
 
26
26
  def total_pages
27
- (total_count.to_f / @limit.to_f).ceil
27
+ (total_count.to_f / @limit).ceil
28
28
  end
29
29
 
30
30
  private
31
31
 
32
32
  def offset
33
33
  return 0 if @page.zero?
34
+
34
35
  (@page - 1) * @limit
35
36
  end
36
37
  end
@@ -12,6 +12,7 @@ module Wor
12
12
 
13
13
  def paginated_content
14
14
  return @paginated_content if @paginated_content
15
+
15
16
  content_array = @content.to_a
16
17
  @paginated_content = content_array.slice((page - 1) * @limit, @limit) || []
17
18
  end
@@ -6,9 +6,11 @@ module Wor
6
6
  class KaminariAlreadyPaginated < Base
7
7
  def required_methods
8
8
  # Methods Kaminari adds to ActiveRecord relations:
9
- ### [:padding, :per, :total_pages, :num_pages, :current_page, :first_page?,
9
+ ### [:padding, :per, :total_pages, :current_page, :first_page?,
10
10
  ### :prev_page, :last_page?, :next_page, :out_of_range?, :total_count, :entry_name]
11
- %i[padding total_count num_pages current_page prev_page]
11
+ # num_pages was removed upstream (kaminari-core 1.2.2 only defines
12
+ # total_pages); checking for it made adapt? always fail.
13
+ %i[padding total_count total_pages current_page prev_page]
12
14
  end
13
15
 
14
16
  def paginated_content
@@ -6,11 +6,22 @@ module Wor
6
6
  default_page: 1,
7
7
  page_param: :page,
8
8
  per_page_param: :limit,
9
- formatter: Wor::Paginate::Formatter,
9
+ formatter: Wor::Paginate::Formatters::Base,
10
10
  max_limit: 50,
11
11
  default_adapter: nil
12
12
  }.freeze
13
13
 
14
+ DEFAULT_ADAPTERS = {
15
+ kaminari_paginated: Adapters::KaminariAlreadyPaginated,
16
+ will_paginate_paginated: Adapters::WillPaginateAlreadyPaginated,
17
+ will_paginate: Adapters::WillPaginate,
18
+ kaminari: Adapters::Kaminari,
19
+ active_record: Adapters::ActiveRecord,
20
+ enumerable: Adapters::Enumerable
21
+ }.freeze
22
+
23
+ @adapters = DEFAULT_ADAPTERS.values
24
+
14
25
  module_function
15
26
 
16
27
  DEFAULTS_CONFIGS.each do |key, value|
@@ -23,10 +34,30 @@ module Wor
23
34
  end
24
35
  end
25
36
 
37
+ def add_adapter(adapter)
38
+ @adapters << adapter
39
+ end
40
+
41
+ def remove_adapter(adapter)
42
+ @adapters.delete(adapter)
43
+ end
44
+
45
+ def clear_adapters
46
+ @adapters.clear
47
+ end
48
+
49
+ def adapters
50
+ @adapters
51
+ end
52
+
26
53
  # This is mostly useful for the tests
27
54
  def reset!
28
55
  DEFAULTS_CONFIGS.each { |k, v| send("#{k}=", v) }
29
56
  end
57
+
58
+ def reset_adapters!
59
+ @adapters = DEFAULT_ADAPTERS.values
60
+ end
30
61
  end
31
62
  end
32
63
  end
@@ -0,0 +1,11 @@
1
+ module Wor
2
+ module Paginate
3
+ module Exceptions
4
+ class DependencyError < StandardError
5
+ def initialize(msg = 'Serializer dependency error')
6
+ super(msg)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module Wor
2
+ module Paginate
3
+ module Formatters
4
+ class AmsFormatter < Base
5
+ def serialized_content
6
+ return serializable_resource.new(paginated_content).as_json unless serializer.present?
7
+
8
+ raise_dependency_error unless serializer.respond_to?('_attributes_data')
9
+ paginated_content.map { |item| serializer.new(item, options) }
10
+ end
11
+
12
+ private
13
+
14
+ def serializable_resource
15
+ ActiveModelSerializers::SerializableResource
16
+ end
17
+
18
+ def raise_dependency_error
19
+ raise Wor::Paginate::Exceptions::DependencyError
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ require 'wor/paginate/utils/uri_helper'
2
+
3
+ module Wor
4
+ module Paginate
5
+ module Formatters
6
+ class Base
7
+ include Utils::UriHelper
8
+ attr_accessor :adapter, :content, :formatter, :options
9
+
10
+ def initialize(adapter, options = {})
11
+ @adapter = adapter
12
+ @options = options
13
+ end
14
+
15
+ def format # rubocop: disable Metrics/MethodLength
16
+ {
17
+ page: serialized_content,
18
+ count: count,
19
+ total_pages: total_pages,
20
+ total_count: options[:total_count] || total_count,
21
+ current_page: current_page,
22
+ previous_page: previous_page,
23
+ next_page: next_page,
24
+ next_page_url: page_url(next_page),
25
+ previous_page_url: page_url(previous_page)
26
+ }
27
+ end
28
+
29
+ protected
30
+
31
+ delegate :count, :total_count, :total_pages, :previous_page, :next_page, to: :adapter
32
+
33
+ def current_page
34
+ adapter.page.to_i
35
+ end
36
+
37
+ def paginated_content
38
+ # rubocop:disable Naming/MemoizedInstanceVariableName -- @content matches the public `content` attr_accessor above; renaming it would silently change what `content` returns.
39
+ @content ||= adapter.paginated_content
40
+ # rubocop:enable Naming/MemoizedInstanceVariableName
41
+ end
42
+
43
+ def serialized_content
44
+ paginated_content.as_json
45
+ end
46
+
47
+ def serializer
48
+ options[:each_serializer]
49
+ end
50
+
51
+ def page_url(page)
52
+ return nil unless page
53
+
54
+ replace_query_params(current_url, page: page)
55
+ end
56
+
57
+ def current_url
58
+ options[:_current_url]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,21 @@
1
+ module Wor
2
+ module Paginate
3
+ module Formatters
4
+ class PankoFormatter < Base
5
+ def serialized_content
6
+ raise Wor::Paginate::Exceptions::DependencyError unless valid_serializer
7
+
8
+ ActiveRecord::Base.transaction do
9
+ Panko::ArraySerializer.new(paginated_content, each_serializer: serializer).to_a
10
+ end
11
+ rescue ActiveRecord::StatementInvalid
12
+ retry
13
+ end
14
+
15
+ def valid_serializer
16
+ serializer.respond_to?('_descriptor') && defined?(Panko::Serializer)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -2,17 +2,6 @@ require_relative 'utils/preserve_records_helper'
2
2
 
3
3
  module Wor
4
4
  module Paginate
5
- # The order of this array is important!
6
- # In a future release we'll provide an interface to manipulate it
7
- ADAPTERS = [
8
- Adapters::KaminariAlreadyPaginated,
9
- Adapters::WillPaginateAlreadyPaginated,
10
- Adapters::WillPaginate,
11
- Adapters::Kaminari,
12
- Adapters::ActiveRecord,
13
- Adapters::Enumerable
14
- ].freeze
15
-
16
5
  def render_paginated(content, options = {})
17
6
  return render_paginate_with_include(content, options) if includes?(options)
18
7
 
@@ -21,18 +10,15 @@ module Wor
21
10
 
22
11
  def paginate(content, options = {})
23
12
  current_url = request.original_url
24
-
25
13
  if (preserve_records = options[:preserve_records])
26
14
  content, current_url = Wor::Paginate::Utils::PreserveRecordsHelper
27
15
  .new(content, current_url,
28
16
  preserve_records.is_a?(Hash) ? preserve_records : {}).call
29
17
  end
30
-
31
18
  adapter = find_adapter_for_content(content, options)
32
19
  raise Exceptions::NoPaginationAdapter if adapter.blank?
33
20
 
34
- formatter_class(options).new(adapter, options.merge(_current_url: current_url))
35
- .format
21
+ formatter_class(options).new(adapter, options.merge(_current_url: current_url)).format
36
22
  end
37
23
 
38
24
  def render_paginate_with_include(content, options)
@@ -40,15 +26,20 @@ module Wor
40
26
  end
41
27
 
42
28
  def formatter_class(options)
43
- options[:formatter].presence || Formatter
29
+ options[:formatter].presence || Config.formatter
44
30
  end
45
31
 
46
32
  def find_adapter_for_content(content, options)
33
+ return instance_adapter(options[:adapter], content, options) unless options[:adapter].nil?
34
+
47
35
  adapters = []
48
36
  adapters << Config.default_adapter if Config.default_adapter.present?
49
- adapters += ADAPTERS
50
- adapters.map { |adapter| adapter.new(content, page(options), limit(options)) }
51
- .find(&:adapt?)
37
+ adapters += Config.adapters
38
+ adapters.map { |adapter| instance_adapter(adapter, content, options) }.find(&:adapt?)
39
+ end
40
+
41
+ def instance_adapter(adapter, content, options)
42
+ adapter.new(content, page(options), limit(options))
52
43
  end
53
44
 
54
45
  def page(options)
@@ -1,5 +1,7 @@
1
1
  class MockedAdapter < Wor::Paginate::Adapters::Base
2
- def initialize; end
2
+ def initialize
3
+ super(nil, 1, 1)
4
+ end
3
5
 
4
6
  def count
5
7
  3
@@ -29,7 +31,7 @@ end
29
31
  RSpec::Matchers.define :be_paginated do
30
32
  match do |actual_response|
31
33
  response = parse_response(actual_response)
32
- formatter = @custom_formatter || Wor::Paginate::Formatter
34
+ formatter = @custom_formatter || Wor::Paginate::Formatters::Base
33
35
  @formatted_keys = formatter.new(MockedAdapter.new, _current_url: 'http://exaple.com/').format.as_json.keys
34
36
  response.keys == @formatted_keys
35
37
  end
@@ -11,8 +11,6 @@ module Wor
11
11
  query_param_value ? Time.parse(query_param_value) : now_timestamp
12
12
  end
13
13
 
14
- private_class_method
15
-
16
14
  def self.now_timestamp
17
15
  Time.zone.now.iso8601(10)
18
16
  end
@@ -25,6 +25,7 @@ module Wor
25
25
  by = options[:by]&.to_s || 'timestamp'
26
26
  raise ArgumentError, "'by' option should be 'id' or 'timestamp'" unless
27
27
  %w[timestamp id].include? by
28
+
28
29
  "Wor::Paginate::Utils::PreserveModes::#{by.classify}".constantize
29
30
  end
30
31
  end
@@ -1,5 +1,5 @@
1
1
  module Wor
2
2
  module Paginate
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
data/lib/wor/paginate.rb CHANGED
@@ -5,10 +5,13 @@ require_relative 'paginate/adapters/kaminari'
5
5
  require_relative 'paginate/adapters/will_paginate'
6
6
  require_relative 'paginate/adapters/kaminari_already_paginated'
7
7
  require_relative 'paginate/adapters/will_paginate_already_paginated'
8
+ require_relative 'paginate/exceptions/dependency_error'
8
9
  require_relative 'paginate/exceptions/no_pagination_adapter'
9
10
  require_relative 'paginate/exceptions/invalid_page_number'
10
11
  require_relative 'paginate/exceptions/invalid_limit_number'
11
- require_relative 'paginate/formatter'
12
+ require_relative 'paginate/formatters/base'
13
+ require_relative 'paginate/formatters/panko_formatter'
14
+ require_relative 'paginate/formatters/ams_formatter'
12
15
  require_relative 'paginate/config'
13
16
  require_relative 'paginate/paginate'
14
17
 
@@ -17,7 +20,7 @@ module Wor
17
20
  def self.configure
18
21
  yield Config
19
22
 
20
- return if ADAPTERS.include?(Config.default_adapter) || Config.default_adapter.nil?
23
+ return if Config.adapters.any? || Config.default_adapter.present?
21
24
 
22
25
  raise Exceptions::NoPaginationAdapter
23
26
  end
data/wor-paginate.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.date = Date.today
11
11
  s.authors = ["icoluccio", "mnmallea", "holywyvern", "lucasVoboril"]
12
12
  s.email = ["ignacio.coluccio@wolox.com.ar", "martin.mallea@wolox.com.ar", "ramiro.rojo@wolox.com.ar", "lucas.voboril@wolox.com.ar"]
13
- s.homepage = "https://github.com/Wolox/wor-paginate"
13
+ s.homepage = "https://github.com/icoluccio/wor-paginate"
14
14
  s.summary = "Simplified pagination for Rails API controllers"
15
15
  s.description = "Wor::Paginate is a gem for Rails that simplifies pagination, particularly for controller methods, while standardizing JSON output for APIs. It's meant to work both as a standalone pagination gem and as an extra layer over Kaminari and will_paginate"
16
16
  s.license = "MIT"
@@ -19,6 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ['lib']
20
20
 
21
21
 
22
- s.add_dependency 'railties', '>= 4.1.0'
23
- s.add_dependency 'rails', '>= 4.0'
22
+ s.add_dependency 'railties', '>= 6.1', '< 9'
23
+ s.add_dependency 'rails', '>= 6.1', '< 9'
24
24
  end
metadata CHANGED
@@ -1,17 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wor-paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - icoluccio
8
8
  - mnmallea
9
9
  - holywyvern
10
10
  - lucasVoboril
11
- autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2020-02-13 00:00:00.000000000 Z
13
+ date: 2026-08-01 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: railties
@@ -19,28 +18,40 @@ dependencies:
19
18
  requirements:
20
19
  - - ">="
21
20
  - !ruby/object:Gem::Version
22
- version: 4.1.0
21
+ version: '6.1'
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '9'
23
25
  type: :runtime
24
26
  prerelease: false
25
27
  version_requirements: !ruby/object:Gem::Requirement
26
28
  requirements:
27
29
  - - ">="
28
30
  - !ruby/object:Gem::Version
29
- version: 4.1.0
31
+ version: '6.1'
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '9'
30
35
  - !ruby/object:Gem::Dependency
31
36
  name: rails
32
37
  requirement: !ruby/object:Gem::Requirement
33
38
  requirements:
34
39
  - - ">="
35
40
  - !ruby/object:Gem::Version
36
- version: '4.0'
41
+ version: '6.1'
42
+ - - "<"
43
+ - !ruby/object:Gem::Version
44
+ version: '9'
37
45
  type: :runtime
38
46
  prerelease: false
39
47
  version_requirements: !ruby/object:Gem::Requirement
40
48
  requirements:
41
49
  - - ">="
42
50
  - !ruby/object:Gem::Version
43
- version: '4.0'
51
+ version: '6.1'
52
+ - - "<"
53
+ - !ruby/object:Gem::Version
54
+ version: '9'
44
55
  description: Wor::Paginate is a gem for Rails that simplifies pagination, particularly
45
56
  for controller methods, while standardizing JSON output for APIs. It's meant to
46
57
  work both as a standalone pagination gem and as an extra layer over Kaminari and
@@ -54,9 +65,11 @@ executables: []
54
65
  extensions: []
55
66
  extra_rdoc_files: []
56
67
  files:
68
+ - ".github/workflows/ci.yml"
57
69
  - ".gitignore"
70
+ - ".overcommit.yml"
58
71
  - ".rubocop.yml"
59
- - ".travis.yml"
72
+ - Appraisals
60
73
  - CHANGELOG.md
61
74
  - Gemfile
62
75
  - LICENSE.md
@@ -74,10 +87,13 @@ files:
74
87
  - lib/wor/paginate/adapters/will_paginate.rb
75
88
  - lib/wor/paginate/adapters/will_paginate_already_paginated.rb
76
89
  - lib/wor/paginate/config.rb
90
+ - lib/wor/paginate/exceptions/dependency_error.rb
77
91
  - lib/wor/paginate/exceptions/invalid_limit_number.rb
78
92
  - lib/wor/paginate/exceptions/invalid_page_number.rb
79
93
  - lib/wor/paginate/exceptions/no_pagination_adapter.rb
80
- - lib/wor/paginate/formatter.rb
94
+ - lib/wor/paginate/formatters/ams_formatter.rb
95
+ - lib/wor/paginate/formatters/base.rb
96
+ - lib/wor/paginate/formatters/panko_formatter.rb
81
97
  - lib/wor/paginate/paginate.rb
82
98
  - lib/wor/paginate/rspec.rb
83
99
  - lib/wor/paginate/utils/preserve_modes.rb
@@ -85,13 +101,11 @@ files:
85
101
  - lib/wor/paginate/utils/uri_helper.rb
86
102
  - lib/wor/paginate/version.rb
87
103
  - pull_request_template.md
88
- - test.sqlite3
89
104
  - wor-paginate.gemspec
90
- homepage: https://github.com/Wolox/wor-paginate
105
+ homepage: https://github.com/icoluccio/wor-paginate
91
106
  licenses:
92
107
  - MIT
93
108
  metadata: {}
94
- post_install_message:
95
109
  rdoc_options: []
96
110
  require_paths:
97
111
  - lib
@@ -106,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
120
  - !ruby/object:Gem::Version
107
121
  version: '0'
108
122
  requirements: []
109
- rubygems_version: 3.1.2
110
- signing_key:
123
+ rubygems_version: 4.0.6
111
124
  specification_version: 4
112
125
  summary: Simplified pagination for Rails API controllers
113
126
  test_files: []
data/.travis.yml DELETED
@@ -1,24 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.3.8
4
- - 2.4.7
5
- - 2.5.6
6
- - 2.6.4
7
- - 2.7.0
8
- - ruby-head
9
-
10
- before_install:
11
- - gem install bundler -v 2.0.1
12
-
13
- install:
14
- - bundle _2.0.1_ install --retry=3
15
- - bundle exec rake db:migrate RAILS_ENV=test -f spec/dummy/Rakefile
16
-
17
- script:
18
- - bundle exec rubocop lib spec --format simple
19
- - bundle exec rspec
20
- - bundle exec codeclimate-test-reporter
21
-
22
- matrix:
23
- allow_failures:
24
- - rvm : ruby-head
@@ -1,66 +0,0 @@
1
- require_relative 'utils/uri_helper'
2
-
3
- module Wor
4
- module Paginate
5
- class Formatter
6
- include Utils::UriHelper
7
- attr_accessor :adapter, :content, :formatter, :options
8
-
9
- def initialize(adapter, options = {})
10
- @adapter = adapter
11
- @options = options
12
- end
13
-
14
- def format # rubocop: disable Metrics/MethodLength
15
- {
16
- page: serialized_content,
17
- count: count,
18
- total_pages: total_pages,
19
- total_count: options[:total_count] || total_count,
20
- current_page: current_page,
21
- previous_page: previous_page,
22
- next_page: next_page,
23
- next_page_url: page_url(next_page),
24
- previous_page_url: page_url(previous_page)
25
- }
26
- end
27
-
28
- protected
29
-
30
- delegate :count, :total_count, :total_pages, :previous_page, :next_page, to: :adapter
31
-
32
- def current_page
33
- adapter.page.to_i
34
- end
35
-
36
- def paginated_content
37
- @content ||= adapter.paginated_content
38
- end
39
-
40
- def serialized_content
41
- if serializer.present?
42
- return paginated_content.map { |item| serializer.new(item, options) }
43
- end
44
- if defined? ActiveModelSerializers::SerializableResource
45
- ActiveModelSerializers::SerializableResource.new(paginated_content).as_json
46
- else
47
- paginated_content.as_json
48
- end
49
- end
50
-
51
- def serializer
52
- options[:each_serializer]
53
- end
54
-
55
- def page_url(page)
56
- return nil unless page
57
-
58
- replace_query_params(current_url, page: page)
59
- end
60
-
61
- def current_url
62
- options[:_current_url]
63
- end
64
- end
65
- end
66
- end
data/test.sqlite3 DELETED
File without changes