Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References represented inside of link block using JSONAPI implementation #168

Closed
matheusca opened this issue Nov 19, 2015 · 5 comments
Closed

Comments

@matheusca
Copy link

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?

@matheusca matheusca changed the title References represented inside of link block References represented inside of link block using JSONAPI implementation Nov 19, 2015
@apotonick
Copy link
Member

Does represented work for you? That's the official API!

@matheusca
Copy link
Author

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.

@apotonick
Copy link
Member

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!

@apotonick
Copy link
Member

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

@myabc
Copy link
Contributor

myabc commented Dec 10, 2016

This issue was moved to trailblazer/roar-jsonapi#14

@myabc myabc closed this as completed Dec 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants