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

Add a way to disable JsonApi automatic pagination #1596

Closed
wants to merge 1 commit into from

Conversation

groyoh
Copy link
Member

@groyoh groyoh commented Mar 16, 2016

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

ActiveModelSerializers.config.adapter = :json_api
class PostsController < ApplicationController
def index
posts = Post.page(page).per_page(per_page)
Copy link
Contributor

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?

Copy link
Member Author

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].

Copy link
Contributor

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

Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix one ruby warning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in master

@NullVoxPopuli
Copy link
Contributor

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
Copy link
Member

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

@bf4
Copy link
Member

bf4 commented Mar 27, 2016

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 paginated? and we can move on.

@groyoh
Copy link
Member Author

groyoh commented Mar 27, 2016

@bf4 would you rather have a boolean config such config.jsonapi_pagination_links_enabled? Not sure I understood your last comment.

@bf4
Copy link
Member

bf4 commented Mar 27, 2016

@groyoh yeah config.jsonapi_pagination_links_enabled? or config.jsonapi_automatic_pagination_links? would be simplest. And this is kind of handling the case where a popular library isn't following the golden path. If that can affect our code base the least, I think that would be best. My alternative would be to create the NonPaginating serializer in test, and document that as a way to do it, but not have it as an actual feature.

@groyoh
Copy link
Member Author

groyoh commented Mar 27, 2016

@bf4 Actually that was my goal at first. Then I probably misinterpreted your comments in #1549 and added these changes. I'll rework the PR then. Which of the two config names do you prefer?

@bf4
Copy link
Member

bf4 commented Mar 27, 2016

@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)

@groyoh
Copy link
Member Author

groyoh commented Mar 27, 2016

That would work. So no specific config then?

BTW, I wanted to ask you. What's the best practice to define test here: def test_something or test "something" do ?

@bf4
Copy link
Member

bf4 commented Mar 28, 2016

I don't know which form would better serve ams

B mobile phone

On Mar 27, 2016, at 12:21 PM, Yohan Robert [email protected] wrote:

That would work. So no specific config then?

BTW, I wanted to ask you. What's the best practice to define test here:

def test_something
test "something" do ?

You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

@NullVoxPopuli
Copy link
Contributor

coming from rspec, I like test 'description' do... better

you can have spaces, and punctuation, and I think it makes reading the test's purpose a little easier.

@groyoh
Copy link
Member Author

groyoh commented Mar 28, 2016

@NullVoxPopuli I also prefer it this way but never wanted to change the current test structure.

@bf4 bf4 added the C: Feature label Mar 29, 2016
@NullVoxPopuli
Copy link
Contributor

needs rebase

@bf4
Copy link
Member

bf4 commented Apr 1, 2016

@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 bf4 self-assigned this Apr 1, 2016
@groyoh
Copy link
Member Author

groyoh commented Apr 6, 2016

@bf4 I'll have time to rework this from tomorrow night until sunday.

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.

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)?

@remear
Copy link
Member

remear commented May 26, 2016

So... a config option to disable automatic pagination then?

@bf4
Copy link
Member

bf4 commented May 27, 2016

Even could be solved just with docs

@richmolj
Copy link
Contributor

richmolj commented Sep 6, 2016

Solved with #1917

@richmolj richmolj closed this Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants