dewski/json_builder

Draper-decorated object loses decoration in json_builder

Closed this issue · 2 comments

Not sure where the culprit is but I'm going to post this here and hope we can figure out what the issue is :)

I'm using json_builder in a rails project. Simple enough.

My controller:

class ProductsController < ApplicationController
  def index
    @products = current_user.products.decorate

    respond_to do |format|
      format.json
    end
  end
end

And my index view:

array(@products) do |product|
  product_id product.id
  quantity 1
  tax product.tax
  unit_cost product.unit_cost
  name product.name
  description product.description
end

Yes, this example is very basic. I'm just trying to display the issue. I'm using the draper gem to decorate my products collection: https://github.com/drapergem/draper

@products is correctly a ProductDecorator. So is @products[0]. However, in that view inside the block product is just a Product and is no longer decorated. Any idea why that might be?

Not really sure what may be the issue here as I am not familiar with draper. Could you check to see what type @products is? Does draper wrap the collection with a single ProductDecorator object or is each element a ProductDecorator object?

When array is called, internally json_builder just calls .each on it and passes in each element to the block, see here.

Could you possibly setup an example app (or single .rb file) that reproduces this?

Closing due to no response, please re-open when you can get me a few pieces of info as above.