wor-paginate 0.2.0 → 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: 2465192c522659e0f6e779d73cda2dfaa3fa47aca0966ebc9fd4292587143cea
4
- data.tar.gz: d71ab589396a9f76e25849e63bec31a1fe5aafcd1dd54c3b9ca79104957cbee7
3
+ metadata.gz: 616213ea69272167621606631c90c60e4c14bdb5a2e31f933576ccc5d8dc04e8
4
+ data.tar.gz: 24486490fa441d027284f1ef4d53ec686d849dc21f0d12bbc81116ed51865c4e
5
5
  SHA512:
6
- metadata.gz: 8ed75b64b43ffe168a35a09a054532c384d51470e3942964b8a60ac191644b264e8d42a59e73ceea3485a0cc7680162a74dcd9d81b5581a110bb8510f965dfcc
7
- data.tar.gz: 93143a274cdbd3bbaffe3898fa4038de9173b6d916eab033e5ae13c8fcaceb63baefc8c7a4d0a6c6ffbb4fc2f6513c2d68769a533fdc05416d3d5336c256dfd8
6
+ metadata.gz: d3eb17ff497fae64cdc0edd117591ac02bc03b74669f82271475202e7d7d0d7c341c85804bbc93f7220751d28c45659265b1c68fd122557d8b558e00d7822d11
7
+ data.tar.gz: 8a76d0a58b6a8b9fb5af30e450de603a91fa8efd9de0f1e4a881cf813be5d19386b65fd60c7ed4d40d7b63c7a3ea4dd8d570f2a0f5ba08a7ebee3ce166e1fd82
@@ -1,5 +1,9 @@
1
1
  ## Change log
2
2
 
3
+ ### V0.3.0
4
+ * [#94](https://github.com/Wolox/wor-paginate/pull/94) Add Panko formatter - [@blacksam07](https://github.com/blacksam07).
5
+ * [#96](https://github.com/Wolox/wor-paginate/pull/96) Dynamic Adapters - [@juanpablo-rojas](https://github.com/juanpablo-rojas).
6
+
3
7
  ### V0.2.0
4
8
  * [#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
9
  * [#93](https://github.com/Wolox/wor-paginate/pull/93) Infinite scroll - [@mnmallea](https://github.com/mnmallea).
data/Gemfile CHANGED
@@ -23,6 +23,7 @@ group :development, :test do
23
23
  gem 'faker', '~> 1.7.0'
24
24
  gem 'generator_spec', '~> 0.9.0'
25
25
  gem 'kaminari', '~> 0.17.0'
26
+ gem 'panko_serializer', '~> 0.7.2'
26
27
  gem 'rake', '~> 10.0'
27
28
  gem 'rspec', '~> 3.0'
28
29
  gem 'rspec-rails', '~> 3.6.0'
data/README.md CHANGED
@@ -14,6 +14,7 @@ Wor::Paginate
14
14
  - [Custom serializers](#custom-serializers)
15
15
  - [Custom options](#custom-options)
16
16
  - [Custom formatters](#custom-formatters)
17
+ - [Custom adapters](#custom-adapters)
17
18
  - [Working with Kaminari or will_paginate](#working-with-kaminari-or-will_paginate)
18
19
  - [Test helpers](#test-helpers)
19
20
  - [Contributing](#contributing)
@@ -170,7 +171,7 @@ end
170
171
  ```
171
172
 
172
173
 
173
- #### Custom formatters
174
+ ### Custom formatters
174
175
  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
176
  ```ruby
176
177
  render_paginated DummyModel, formatter: CustomFormatter
@@ -180,7 +181,7 @@ or it can also be set as a default in the initializer.
180
181
  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
182
 
182
183
  ```ruby
183
- class CustomFormatter < Wor::Paginate::Formatter
184
+ class CustomFormatter < Wor::Paginate::Formatters::Base
184
185
  def format
185
186
  { page: serialized_content, current: current_page }
186
187
  end
@@ -195,6 +196,74 @@ Available helper methods are:
195
196
  * `paginated_content`: its class depends on the original content passed to render_paginated, it's the paginated but not yet serialized content.
196
197
  * `serialized_content`: array with all the items after going through the serializer (either the default or a supplied one)
197
198
 
199
+
200
+ ### Custom adapters
201
+ 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:
202
+ ```ruby
203
+ render_paginated DummyModel, adapter: CustomAdapter
204
+ ```
205
+ or it can also be set as a default in the initializer.
206
+
207
+ 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.
208
+ Below is an example of a simple posible CustomAdapter that extends from the base Adapter.
209
+
210
+ ```ruby
211
+ class CustomAdapter < Wor::Paginate::Adapters::Base
212
+ def required_methods
213
+ %i[count]
214
+ end
215
+
216
+ def paginated_content
217
+ @paginated_content ||= @content.first(5)
218
+ end
219
+
220
+ def total_pages
221
+ (total_count / @limit.to_f).ceil
222
+ end
223
+
224
+ def total_count
225
+ @content.count
226
+ end
227
+
228
+ delegate :count, to: :paginated_content
229
+ end
230
+ ```
231
+ Here's a brief explanation on every overwritten method in this CustomAdapter example:
232
+
233
+ ##### `#required_methods`
234
+ 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.
235
+
236
+ ##### `#paginated_content`
237
+ 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, will always be shown the first 5 records.
238
+
239
+ ##### `#total_pages`
240
+ 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.
241
+
242
+ ##### `#total_count`
243
+ 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`.
244
+
245
+ ##### `#count` method as delegate
246
+ In 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 is a method definition or a delegate).
247
+
248
+ 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 we want a custom behaviour of a known method.
249
+
250
+ ##### Other available methods to overwrite
251
+ `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.
252
+
253
+ 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/Wolox/wor-paginate/blob/master/lib/wor/paginate/adapters/base.rb).
254
+ 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.
255
+
256
+ #### Adapters Operations
257
+ There are also helper methods available to dynamically operate the gem's adapters, so you can configurate 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:
258
+ * `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).
259
+ * `Wor::Paginate::Config.remove_adapter(adapter)`: Remove an specific adapter from the array of the gem's adapters.
260
+ * `Wor::Paginate::Config.clear_adapters`: This method empties the array of the gem's adapters.
261
+ * `Wor::Paginate::Config.adapters`: Returns all the current internal adapters inside the gem.
262
+ * `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 beggining of the `Wor::Paginate::Config` module: [Config module](https://github.com/Wolox/wor-paginate/blob/master/lib/wor/paginate/config.rb).
263
+
264
+ 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) are you leaving in the `Wor::Paginate::Config.adapters` array, because depending on those, the gem will try to adapt the content.
265
+
266
+
198
267
  ### Working with Kaminari or will_paginate
199
268
  If either Kaminari or will_paginate are required in the project, Wor::Paginate will use them for pagination with no code or configuration change.
200
269
 
@@ -232,6 +301,24 @@ describe YourController do
232
301
  end
233
302
  ```
234
303
 
304
+ ### Working with panko-serializer
305
+
306
+ The default formatter is [Active Model Serializer](https://github.com/rails-api/active_model_serializers).
307
+ If you want to change it, you should replace the formatter to another one. In this section, we are going to work with `PankoFormatter`
308
+
309
+ #### example
310
+ ```ruby
311
+ Wor::Paginate.configure do |config
312
+ config.formatter = Wor::Paginate::Formatters::PankoFormatter
313
+ end
314
+ ```
315
+ and next pass the specific serializer that you can use in the specific endpoint
316
+
317
+ ```ruby
318
+ def index
319
+ render_paginated DummyModel, each_serializer: DummyModelPankoSerializer
320
+ end
321
+ ```
235
322
  ## Contributing
236
323
 
237
324
  1. Fork it
@@ -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
@@ -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
@@ -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,31 @@
1
+ module Wor
2
+ module Paginate
3
+ module Formatters
4
+ class AmsFormatter < Base
5
+ include ActiveSupport::Callbacks
6
+ define_callbacks :raise_dependency_error
7
+ set_callback :raise_dependency_error, :before, :serialized_content, unless: :ams_defined?
8
+
9
+ def serialized_content
10
+ return serializable_resource.new(paginated_content).as_json unless serializer.present?
11
+ raise_dependency_error unless serializer.respond_to?('_attributes_data')
12
+ paginated_content.map { |item| serializer.new(item, options) }
13
+ end
14
+
15
+ private
16
+
17
+ def serializable_resource
18
+ ActiveModelSerializers::SerializableResource
19
+ end
20
+
21
+ def ams_defined?
22
+ defined? serializable_resource
23
+ end
24
+
25
+ def raise_dependency_error
26
+ raise Wor::Paginate::Exceptions::DependencyError
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
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
+ @content ||= adapter.paginated_content
39
+ end
40
+
41
+ def serialized_content
42
+ paginated_content.as_json
43
+ end
44
+
45
+ def serializer
46
+ options[:each_serializer]
47
+ end
48
+
49
+ def page_url(page)
50
+ return nil unless page
51
+
52
+ replace_query_params(current_url, page: page)
53
+ end
54
+
55
+ def current_url
56
+ options[:_current_url]
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
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
+ ActiveRecord::Base.transaction do
8
+ Panko::ArraySerializer.new(paginated_content, each_serializer: serializer).to_a
9
+ end
10
+ rescue ActiveRecord::StatementInvalid
11
+ retry
12
+ end
13
+
14
+ def valid_serializer
15
+ serializer.respond_to?('_descriptor') && defined?(Panko::Serializer)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ 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)
@@ -29,7 +29,7 @@ end
29
29
  RSpec::Matchers.define :be_paginated do
30
30
  match do |actual_response|
31
31
  response = parse_response(actual_response)
32
- formatter = @custom_formatter || Wor::Paginate::Formatter
32
+ formatter = @custom_formatter || Wor::Paginate::Formatters::Base
33
33
  @formatted_keys = formatter.new(MockedAdapter.new, _current_url: 'http://exaple.com/').format.as_json.keys
34
34
  response.keys == @formatted_keys
35
35
  end
@@ -1,5 +1,5 @@
1
1
  module Wor
2
2
  module Paginate
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.3.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - icoluccio
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2020-02-13 00:00:00.000000000 Z
14
+ date: 2020-09-17 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: railties
@@ -74,10 +74,13 @@ files:
74
74
  - lib/wor/paginate/adapters/will_paginate.rb
75
75
  - lib/wor/paginate/adapters/will_paginate_already_paginated.rb
76
76
  - lib/wor/paginate/config.rb
77
+ - lib/wor/paginate/exceptions/dependency_error.rb
77
78
  - lib/wor/paginate/exceptions/invalid_limit_number.rb
78
79
  - lib/wor/paginate/exceptions/invalid_page_number.rb
79
80
  - lib/wor/paginate/exceptions/no_pagination_adapter.rb
80
- - lib/wor/paginate/formatter.rb
81
+ - lib/wor/paginate/formatters/ams_formatter.rb
82
+ - lib/wor/paginate/formatters/base.rb
83
+ - lib/wor/paginate/formatters/panko_formatter.rb
81
84
  - lib/wor/paginate/paginate.rb
82
85
  - lib/wor/paginate/rspec.rb
83
86
  - lib/wor/paginate/utils/preserve_modes.rb
@@ -106,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  - !ruby/object:Gem::Version
107
110
  version: '0'
108
111
  requirements: []
109
- rubygems_version: 3.1.2
112
+ rubygems_version: 3.0.3
110
113
  signing_key:
111
114
  specification_version: 4
112
115
  summary: Simplified pagination for Rails API controllers
@@ -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