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 #14

Open
myabc opened this issue Dec 10, 2016 · 4 comments
Open
Assignees

Comments

@myabc
Copy link
Collaborator

myabc commented Dec 10, 2016

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

@myabc
Copy link
Collaborator Author

myabc commented Dec 10, 2016

From @apotonick on November 25, 2015 2:28

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

@myabc
Copy link
Collaborator Author

myabc commented Dec 10, 2016

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.

@myabc
Copy link
Collaborator Author

myabc commented Dec 10, 2016

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!

@myabc
Copy link
Collaborator Author

myabc commented Dec 10, 2016

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

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

No branches or pull requests

2 participants