-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add a way to disable JsonApi automatic pagination #1596
Conversation
ActiveModelSerializers.config.adapter = :json_api | ||
class PostsController < ApplicationController | ||
def index | ||
posts = Post.page(page).per_page(per_page) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where do page and per_page come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe from ApplicationController
😉 I didn't want to write two methods or more code just to specify where the per_page
and page
come from. It's outside the scope of AMS or this example in my opinion. I could maybe replace it by params[:page]
and params[:per_page]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think the params would be helpful. Otherwise it might lead the reader to believe that we are monkey patching those methods in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this.
@@ -0,0 +1,89 @@ | |||
module CollectionSerializerTesting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this as a shared test for CollectionSerializer
and NonPaginatedCollectionSerializer
.
@@ -38,6 +38,7 @@ def initialize(*) | |||
super | |||
@_links = {} | |||
@_include_data = true | |||
@_meta = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix one ruby warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in master
I'm ok with this. 👍 anybody else? @rails-api/ams |
The following configuration can now be used to disable automatic pagination links when using JsonApi adapter: ActiveModelSerializer.config.collection_serializer = ActiveModel::Serializer::Nonpaginatedcollectionserializer
@@ -3,6 +3,8 @@ | |||
Breaking changes: | |||
|
|||
Features: | |||
- [#1596](https://github.com/rails-api/active_model_serializers/pull/1596) Provide a way to prevent links to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting `config.collection_serializer = NonPaginatedCollectionSerializer` now prevents the JSON API adapter from automatically rendering pagination Link objects
I'm kind of concerned at how the changeset here is much larger than the feature. If someone wants to disable json api link object generation, I'd rather just either add a condition to or redefine |
@bf4 would you rather have a boolean config such |
@groyoh yeah |
@groyoh true enough. but couldn't the diff just be (pseudocode) class CollectionSerializer
+ class NonPaginatedCollectionSerializer < CollectionSerializer
+ def paginated; false; end
+ end
end and test "pagination links are generated when `paginated?` is true" do
# blah
end
+ test "pagination links are not generated when `paginated?` is false" do
+ serialization = serialize(resource, serializer: NonPaginatedCollectionSerializer).as_json
+ assert_equal {}, serialization.fetch(:data).fetch(:links)
+ end and that's the whole PR (except for adding docs) |
That would work. So no specific config then? BTW, I wanted to ask you. What's the best practice to define test here: |
I don't know which form would better serve ams B mobile phone
|
coming from rspec, I like you can have spaces, and punctuation, and I think it makes reading the test's purpose a little easier. |
@NullVoxPopuli I also prefer it this way but never wanted to change the current test structure. |
needs rebase |
@groyoh Let's take some time to go over this together. It's basically done, just a lot of non-critical things I want to sync up on before mergin. |
@bf4 I'll have time to rework this from tomorrow night until sunday.
I'm still not sure what your final thoughts are. Should we just provide a boolean configuration (render urls or not) or change the current PR to match with #1596 (comment)? |
So... a config option to disable automatic pagination then? |
Even could be solved just with docs |
Solved with #1917 |
Purpose
Provides a way to prevent automatic links rendering when using kaminari or will_paginate. See #1549 and #1549 (comment).
Changes
Added a
NonPaginatedCollectionSerializer
with tests and documentation for it.Related GitHub issues
Solves #1549
Should solve #1268 too