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

Pagination Link Do Not Outputted on JSON #1466

Closed
edwinlunando opened this issue Jan 25, 2016 · 6 comments
Closed

Pagination Link Do Not Outputted on JSON #1466

edwinlunando opened this issue Jan 25, 2016 · 6 comments

Comments

@edwinlunando
Copy link
Contributor

Hi, I'm using master branch and JSON adapter. I want to show pagination links, this is my pagination serializer.

# pagination collection serializer
class PaginationSerializer < ActiveModel::Serializer::CollectionSerializer

  def initialize(object, options = {})
    meta_key = options[:meta_key] || :meta
    options[meta_key] ||= {}
    options[meta_key][:pagination] = {
      current_page: object.current_page,
      next_page: object.next_page,
      prev_page: object.prev_page,
      total_pages: object.total_pages,
      total_count: object.total_count
    }
    super(object, options)
  end

end

This is my render code

render json: formulas, serializer: PaginationSerializer, each_serializer: FormulaSimpleSerializer

There no meta key on the result. This happens after upgrading from 0.10.RC3 to master. Any idea?

@edwinlunando
Copy link
Contributor Author

I tried to dig the old ArraySerializer

def initialize(resources, options = {})
  @root = options[:root]
  @object = resources
  @serializers = resources.map do |resource|
    serializer_class = options.fetch(:serializer) {
      ActiveModel::Serializer.serializer_for(resource)
    }

    if serializer_class.nil?
      fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
    else
      serializer_class.new(resource, options.except(:serializer))
    end
  end
  @meta     = options[:meta]
  @meta_key = options[:meta_key]
end

and compare it to the new CollectionSerializer

def initialize(resources, options = {})
  @root = options[:root]
  @object = resources
  @serializers = resources.map do |resource|
    serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
    serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }

    if serializer_class.nil?
      fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
    else
      serializer_class.new(resource, options.except(:serializer))
    end
  end
end

Why there is no meta declaration? Is this expected? What I must do in order to add pagination links?

@edwinlunando
Copy link
Contributor Author

Come to this rendering page, does it means we cannot use something like PaginationSerializer anymore? Do we need to explicitly put it in render method?

@edwinlunando
Copy link
Contributor Author

Come to this pull request, seems the pagination documentation on master branch is no longer valid. I can make a PR if you want to fix the docs?

@beauby
Copy link
Contributor

beauby commented Jan 25, 2016

@edwinlunando Yes, a docs PR would be great!

@edwinlunando
Copy link
Contributor Author

request, pulled. #1472

@bf4
Copy link
Member

bf4 commented Feb 12, 2016

Closed by #1472

@bf4 bf4 closed this as completed Feb 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants