trailblazer / roar

Parse and render REST API documents using representers.
http://www.trailblazer.to/gems/roar
MIT License
1.85k stars 138 forks source link

[JSON-API] JsonApi decorator does not have attributes hash #181

Closed caseymct closed 8 years ago

caseymct commented 8 years ago

Using roar (1.0.4) representable (2.3.0) uber (~> 0.0.7)

I have an Event model - let's say for simplicity it just has a title.

class Event < ActiveRecord::Base
end
# == Schema Information
#
# Table name: events
#
#  id                 :integer          not null, primary key
#  title              :string(255)

I would like to return a JSON api compliant payload for this, e.g

{ 
  data: {
    id: 1,
    type: 'events',
    attributes: {
      title: 'New event'
    }
  }
}

here is what I have written (edited for simplicity):

require 'roar/representer'
require 'roar/decorator'
require 'roar/json/json_api'

class ApplicationDecorator < Roar::Decorator
  include Roar::JSON::JSONAPI

  property :id, render_nil: true, if: proc { respond_to? :id }
end

and

class EventDecorator < ApplicationDecorator
  type :events
  property :title
end

When I run EventDecorator.prepare(Event.last).to_json

I get the following: {"events"=>{"id"=>20, "title"=>"New event"}}

no attributes hash, no data property, no type, etc. what am I doing wrong? thanks!

apotonick commented 8 years ago

:grimacing:

caseymct commented 8 years ago

@apotonick it didn't work on 1.0.4 gem, I had to get the code from master :/

apotonick commented 8 years ago

Ah yeah, the 1.0 JSONAPI implementation is completely wrong!!! I should remove it. Or release 1.1!

vinceferro commented 8 years ago

Hi, same here, using the master with roar-grape works fine, but no chances with roar-rails! Are you planning to update roar-rails to match this fix on json-api? Thanks