Skip to content

Instantly share code, notes, and snippets.

@matheusca
Last active November 30, 2015 18:07
Show Gist options
  • Save matheusca/af70795b1f7abca24db7 to your computer and use it in GitHub Desktop.
Save matheusca/af70795b1f7abca24db7 to your computer and use it in GitHub Desktop.

This gist shows an approach how I think Roar JSONAPI render should be work. I appreciacte any thought.

Compound

JSONAPI specs described included data MAY include along with primary requester avoiding many HTTP request to the server. So, I think Roar could be implement it having two options, IMO.

1'st option(representer created with included schema)

You may need representer ever include compound in your JSON, so ROAR could be a way to interprete it. I think that can be doing as follow:

class FooRepresenter < Roar::Decorator
  include Roar::JSON::JSONAPI
  
  #... properties and so on
  
  compound do
    property :test_with_included, included: true do
      property :name
      property :other_name
    end
    
    property :test_without_included do
      property :name
      property :other_name
    end
  end
end

How you've looked test_with_included always will be embed into JSON data, but test_without_included will not be.

I don't know at the moment how property is implemented, maybe could be difficult to do it in property, because it can be use outside of json api scope, so it could be implement in compound as follow:

class FooRepresenter < Roar::Decorator
  include Roar::JSON::JSONAPI
  
  #... properties and so on
  
  compound included: true do
    property :test_with_included, included: true do
      property :name
      property :other_name
    end
  end
  
  compound do
    property :test_without_included do
      property :name
      property :other_name
    end
  end
end

Anyway how it'll implement, I think guarantees what specs described.

2'nd option(passing options to_json)

A second functionality would be passing options to to_json, I've explaned how It would work:

class FooRepresenter < Roar::Decorator
  compound included: true do
    property :test_with_included, included: true do
      property :name
      property :other_name
    end
  end
  
  compound do
    property :test_without_included do
      property :name
      property :other_name
    end
  end
end

# Called would be
FooRepresenter.new(representer).to_json(compound: [:test_without_included])
# In this case JSON returns test_without_included into JSON.

Otherwise I think roar wouldn't implement some functionality to remove one set up compound as included: true. One time set up and you need one way to not included one time, you'll need remove included:true at compound method, so you'll need called to_json as it've described above.

Relations link

Roar doesn't create relation links when relation object with entity is nil

For example, imagine existing an object called Test and relation with another object called OtherTest, this relation is has_one for both and it were implement as following:

class Test
  
  attr_reader :name, :other_test
  
  def initialize(attributes)
    @name = attributes[:name]
    @other_test = attributes[:other_test]
  end
end

class OtherTest
  
  attr_reader :other_name
  
  def initialize(attributes)
    @other_name = attributes[:other_name]
  end
end

class TestRepresenter < Roar::Decorator
  include Roar::JSON::JSONAPI
  
  has_one :other_test
  
  property :name
end

# When create a relation
other_test = OtherTest.new(other_name: 'test')
test = Test.new(name: 'test', other_test: other_test)
TestRepresenter.new(test).to_json
# As above it returns relations links.

# But when it passed without relation
test = Test.new(name: 'test')
TestRepresenter.new(test).to_json
# it will not return relation links into json.

JSONAPI specs described that if you have a relation always relation links should be present. So the whether my object has a relation other_test setting as nil relation links should be including into json, but roar doesn't return it yet now.

@apotonick
Copy link

  1. Option: http://trailblazer.to/gems/roar/jsonapi.html#sparse-fieldsets

You can say what should go into the :included:

decorator.to_hash(
  include: [:id, :title, :author] # author is a compound fragment.
)

@apotonick
Copy link

You can also disable rendering of the entire compound document: http://trailblazer.to/gems/roar/jsonapi.html#compound-document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment