Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable pagination links via config #1917

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Features:
- [#1791](https://github.com/rails-api/active_model_serializers/pull/1791) (@bf4, @youroff, @NullVoxPopuli)
- Added `jsonapi_namespace_separator` config option.
- [#1889](https://github.com/rails-api/active_model_serializers/pull/1889) Support key transformation for Attributes adapter (@iancanderson, @danbee)
- [#1917](https://github.com/rails-api/active_model_serializers/pull/1917) Add `jsonapi_pagination_links_enabled` configuration option (@richmolj)

Fixes:

Expand Down
3 changes: 2 additions & 1 deletion lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def json_key
# rubocop:enable Metrics/CyclomaticComplexity

def paginated?
object.respond_to?(:current_page) &&
ActiveModelSerializers.config.jsonapi_pagination_links_enabled &&
object.respond_to?(:current_page) &&
object.respond_to?(:total_pages) &&
object.respond_to?(:size)
end
Expand Down
1 change: 1 addition & 0 deletions lib/active_model/serializer/concerns/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def config.array_serializer
config.default_includes = '*'
config.adapter = :attributes
config.key_transform = nil
config.jsonapi_pagination_links_enabled = true
config.jsonapi_resource_type = :plural
config.jsonapi_namespace_separator = '-'.freeze
config.jsonapi_version = '1.0'
Expand Down
19 changes: 17 additions & 2 deletions test/adapter/json_api/pagination_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def last_page_links
}
end

def expected_response_without_pagination_links
def expected_response_when_unpaginatable
data
end

Expand All @@ -87,6 +87,12 @@ def expected_response_with_pagination_links
end
end

def expected_response_without_pagination_links
{}.tap do |hash|
hash[:data] = data.values.flatten[2..3]
end
end

def expected_response_with_pagination_links_and_additional_params
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
{}.tap do |hash|
Expand Down Expand Up @@ -159,7 +165,7 @@ def test_last_page_pagination_links_using_will_paginate
def test_not_showing_pagination_links
adapter = load_adapter(@array, mock_request)

assert_equal expected_response_without_pagination_links, adapter.serializable_hash
assert_equal expected_response_when_unpaginatable, adapter.serializable_hash
end

def test_raises_descriptive_error_when_serialization_context_unset
Expand All @@ -172,6 +178,15 @@ def test_raises_descriptive_error_when_serialization_context_unset
assert_equal exception_class, exception.class
assert_match(/CollectionSerializer#paginated\?/, exception.message)
end

def test_pagination_links_not_present_when_disabled
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = false
adapter = load_adapter(using_kaminari, mock_request)

assert_equal expected_response_without_pagination_links, adapter.serializable_hash
ensure
ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = true
end
end
end
end
Expand Down