Skip to content

How to define UserBooksResource for grouped JSON output by status? #388

Closed Answered by okuramasafumi
reckyy asked this question in Q&A
Discussion options

You must be logged in to vote

@reckyy OK, how about this?

require 'alba'

Book = Data.define(:id, :title, :author, :status)

class BookResource
  include Alba::Resource

  attributes :id, :title, :author, :status
end

book1 = Book.new(1, 'Ruby Book', 'Matz', 'finished')
book2 = Book.new(2, 'Rails Book', 'DHH', 'reading')
book3 = Book.new(3, 'Elixir Book', 'Jose Valim', 'unread')
book4 = Book.new(4, 'Phoenix Book', 'Chris McCord', 'reading')

BookCollection = Data.define(:unread_books, :reading_books, :finished_books)
book_collection = BookCollection.new([book3], [book2, book4], [book1])

class BookCollectionResource
  include Alba::Resource

  many :unread_books, resource: BookResource
  many :reading_books, resource: B…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@okuramasafumi
Comment options

Answer selected by reckyy
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants