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

Question about Resource / Serializer, can you provide some examples ? #28

Closed
bcotteret opened this issue Sep 16, 2020 · 2 comments
Closed

Comments

@bcotteret
Copy link

bcotteret commented Sep 16, 2020

Hi,
I can't figure out how to implement the serializer class
I understand the ressource, but how look like the Serializer class ?

Here is what my serializer from AMS

class IssueSerializer < ActiveModel::Serializer  
  attributes :title, :subject, :description, :conversations, :due_at, :assigned_to
  belongs_to :company
end

and in controller
I would like to use with metadata pagination Pagy

 class IssuesController < ApiController
    include Pagy::Backend
    after_action { pagy_headers_merge(@pagy) if @pagy }
    rescue_from Pagy::OverflowError, with: :redirect_to_last_page

    def index
      @pagy, @isssues = Issue.scope(....)
      render json: { issues: @issues, pagination: pagy_metadata( @pagy, url: true )
   end

end

How would you advise me using Alba in this case ?
Thanks

@okuramasafumi
Copy link
Owner

It's kind of confusing, but in Alba, Resource class is responsible for converting objects into a Hash, and Serializer is responsible for dumping actual JSON string with some modification like key and metadata.
For now, please take a look at test directory and there are many working examples. I'll add more examples later.

For pagination, Alba doesn't provide a support for pagy gem or other gems. For now, you can do something like this:

# Assume that IssueResource includes Alba::Resource

def index
  @pagy, @issues = pagy(Issue.all)
  pagy = @pagy # This is dirty trick to pass data to `metadata` block
  pagination_metadata_method = method(:pagy_metadata)
  with = proc do
    metadata :pagination do
      pagination_metadata_method.call(pagy, {url: true})
    end
  end
  render json: IssueResource.new(@issues).serialize(with: with)
end

Because Alba doesn't provide support for passing extra data to metadata block (I'm considering what is the best way to do so), we must do some dirty stuff here.
First, serializer part must be defined in a Proc. Then, pagy must be present as local variable. Finally, you must use method method to call pagination method in metadata block.
I surely know that this is not how it should be, so I'll improve support for pagination soon.

@bcotteret
Copy link
Author

Hi @okuramasafumi,
Thanks for your comments
Sure, I'll have a depper look at your examples and code
Keep the good work, Alba looks as a good fit for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants