Skip to content

Cleaner way to get nested data in API? #4906

Discussion options

You must be logged in to vote

I've more or less solved this by prepending a doc_builder method on Spree::Product that maps over documents and returns an object containing the data I want.

module Spree::Product::AddDocBuilder
  def doc_builder
    return nil unless variant_docs.any?

   docs = []

    variant_docs.map do |doc|
        docs.push({
          name: doc.name,
          attachment: doc.path_to_file,
          position: doc.position,
          id: doc.id
        })
        return docs
    end
  end
end

Then I just include that in the API config initializer:

Spree::Api::Config.configure do |config|
  config.product_attributes.push(, :doc_builder)
end

Seems like jbuilder just formats it correctly!

Replies: 2 comments 1 reply

Comment options

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

Comment options

You must be logged in to vote
0 replies
Answer selected by michaelmichael
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants