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

New for_collection method on Serializers that is pagination aware #866

Open
jwoertink opened this issue Aug 28, 2024 · 0 comments
Open

New for_collection method on Serializers that is pagination aware #866

jwoertink opened this issue Aug 28, 2024 · 0 comments
Labels
feature request A new requested feature / option hacktoberfest Valid Issue for Hacktoberfest

Comments

@jwoertink
Copy link
Member

This one may be stepping in a little too deep, but I have this overload method added in my app that helps me serialize a collection with pagination data added in. This helps with an API where the front-end handles the pagination.

def self.for_collection(collection : Enumerable, pages : Lucky::Paginator, *args, **named_args)
    {
      "items" => for_collection(collection, *args, **named_args),
      "pagination" => PaginationSerializer.new(pages),
    }
end

Then the PaginationSerializer is also generic enough to be added in by default

class PaginationSerializer < BaseSerializer
  def initialize(@pages : Lucky::Paginator)
  end

  def render
    {
      next_page:     @pages.path_to_next,
      previous_page: @pages.path_to_previous,
      total_items:   @pages.item_count,
      total_pages:   @pages.total,
    }
  end
end

It would be added in to this class

abstract class BaseSerializer < Lucky::Serializer

@jwoertink jwoertink added the feature request A new requested feature / option label Aug 28, 2024
@jwoertink jwoertink added the hacktoberfest Valid Issue for Hacktoberfest label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new requested feature / option hacktoberfest Valid Issue for Hacktoberfest
Projects
None yet
Development

No branches or pull requests

1 participant