This gist shows an approach how I think Roar JSONAPI render should be work. I appreciacte any thought.
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.
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.
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.
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.
You can say what should go into the
:included
: