dewski / json_builder

Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.
http://garrettbjerkhoel.com/json_builder/
MIT License
244 stars 52 forks source link

Draper-decorated object loses decoration in json_builder #38

Closed nzifnab closed 11 years ago

nzifnab commented 11 years ago

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?

dewski commented 11 years ago

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?

dewski commented 11 years ago

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