Skip to content

Commit 19b5abf

Browse files
author
Lee Richmond
committed
Disable pagination links via config
1 parent 0500604 commit 19b5abf

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Features:
1313
- [#1791](https://github.com/rails-api/active_model_serializers/pull/1791) (@bf4, @youroff, @NullVoxPopuli)
1414
- Added `jsonapi_namespace_separator` config option.
1515
- [#1889](https://github.com/rails-api/active_model_serializers/pull/1889) Support key transformation for Attributes adapter (@iancanderson, @danbee)
16+
- [#1917](https://github.com/rails-api/active_model_serializers/pull/1917) Add `jsonapi_pagination_links_enabled` configuration option (@richmolj)
1617

1718
Fixes:
1819

lib/active_model/serializer/collection_serializer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def json_key
5252
# rubocop:enable Metrics/CyclomaticComplexity
5353

5454
def paginated?
55-
object.respond_to?(:current_page) &&
55+
ActiveModelSerializers.config.jsonapi_pagination_links_enabled &&
56+
object.respond_to?(:current_page) &&
5657
object.respond_to?(:total_pages) &&
5758
object.respond_to?(:size)
5859
end

lib/active_model/serializer/concerns/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def config.array_serializer
2222
config.default_includes = '*'
2323
config.adapter = :attributes
2424
config.key_transform = nil
25+
config.jsonapi_pagination_links_enabled = true
2526
config.jsonapi_resource_type = :plural
2627
config.jsonapi_namespace_separator = '-'.freeze
2728
config.jsonapi_version = '1.0'

test/adapter/json_api/pagination_links_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def last_page_links
7676
}
7777
end
7878

79-
def expected_response_without_pagination_links
79+
def expected_response_when_unpaginatable
8080
data
8181
end
8282

@@ -87,6 +87,12 @@ def expected_response_with_pagination_links
8787
end
8888
end
8989

90+
def expected_response_without_pagination_links
91+
{}.tap do |hash|
92+
hash[:data] = data.values.flatten[2..3]
93+
end
94+
end
95+
9096
def expected_response_with_pagination_links_and_additional_params
9197
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
9298
{}.tap do |hash|
@@ -159,7 +165,7 @@ def test_last_page_pagination_links_using_will_paginate
159165
def test_not_showing_pagination_links
160166
adapter = load_adapter(@array, mock_request)
161167

162-
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
168+
assert_equal expected_response_when_unpaginatable, adapter.serializable_hash
163169
end
164170

165171
def test_raises_descriptive_error_when_serialization_context_unset
@@ -172,6 +178,15 @@ def test_raises_descriptive_error_when_serialization_context_unset
172178
assert_equal exception_class, exception.class
173179
assert_match(/CollectionSerializer#paginated\?/, exception.message)
174180
end
181+
182+
def test_pagination_links_not_present_when_disabled
183+
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = false
184+
adapter = load_adapter(using_kaminari, mock_request)
185+
186+
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
187+
ensure
188+
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = true
189+
end
175190
end
176191
end
177192
end

0 commit comments

Comments
 (0)