References represented inside of link block using JSONAPI implementation
From @matheusca on November 19, 2015 22:15
Hi guys,
I've trying create association links using Roar::JSON::JSONAPI but I can't reference represented method inside of the has_one block.
For instance:
class RequesterRepresenter < Roar::Decorator
include Roar::JSON::JSONAPI
type :requester
has_one :residence_address do
type :address
link :self do
# I need way to get requester id here.
"http://example.com/requester/#{requester.id}/relationships/residence_address"
end
end
property :id
# ... other properties
end
How I described, I would need some way to get requester.id inside of has_one. Anyway to do it?
Copied from original issue: trailblazer/roar#168
From @apotonick on November 25, 2015 2:28
Does represented work for you? That's the official API!
From @matheusca on November 27, 2015 17:35
I can't use represented as requester inside of has_one block, because when I used it, the represented is the instance of residence_address like example above.
From @apotonick on December 1, 2015 1:41
That is correct behaviour.
The represented method refers to the currently rendered object, which is a residence_address in your case.
You can do represented.requester if that's what your data model allows. Alternatively, you can pass the requester as a :user_options to the block. Representable/Roar does not support accessing parent decorators right now (because that's data modelling and not representing). Let me think about that!
From @apotonick on December 1, 2015 2:21
As this might be a common problem in JSON API, we could introduce a :parent_represented option, which'd work as follows.
link :self do |options|
options[:parent_represented].id
end