Comments (5)
From @lasseebert on April 15, 2016 8:40
I have run into the same issue. I have a polymorphic relation: An AccountUser
has one Account
which can be one of several types of accounts.
I want to render the same properties for each account, but I need the type
to be different.
Something like this:
class AccountUserView < Roar::Decorator
include Roar::JSON::JSONAPI
type "account-users"
property :id
property :another_property
has_one :account do
type { |account| account.type } # <-- this must be variable depending on the type of account
property :id
property :some_other_property
end
end
Can this be done somehow?
from roar-jsonapi.
From @lasseebert on April 15, 2016 9:23
I have messed around in the roar code and solved this so that type
now accepts both a string and a block (mutually exclusive) like my above example.
def type(name=nil, &block)
@apotonick, if you think this is a good idea, I will make some tests and a proper pull request ;)
from roar-jsonapi.
From @apotonick on April 16, 2016 2:29
The example @caseymct gives is more complicated, @lasseebert! You only need the type
dynamic, in the other issue, the entire representer must be interchangable.
I believe it takes a bit more work to cover both of the cases.
from roar-jsonapi.
From @lasseebert on April 17, 2016 20:45
@apotonick, yes I see now that my issue is not related at all to the original issue posted here, since dynamically deciding the type
is not only related to has_one
, but could also be useful-ish in the root of a representer.
E.g. in my above example code, I could have an AccountRepresentable
that has different jsonapi types, but similar data structure.
The use case for this is pretty thin, so I'll take down my suggestion again ;)
from roar-jsonapi.
Any news on this topic ? Polymorphism is mandatory in any non basic api. (and I don't say that because I'm working on a huge/urgent project that needs this feature 😅)
from roar-jsonapi.
Related Issues (20)
- JSON-API support for :extends/:decorator with has_one
- [JSON-API] Relationship links HOT 11
- JSON API dasherize property names HOT 4
- References represented inside of link block using JSONAPI implementation HOT 4
- JSON-API representer does not work with Roar::Client HOT 1
- Properties convert as camelize(:lower) HOT 6
- Representers: `defaults` block is lost in nested declarations HOT 3
- Collection representation HOT 8
- Update Operation with Roar JSONAPI representer forces contract to open up id HOT 1
- double included fields HOT 4
- Compound Documents not rendered per JSONAPI standard? HOT 2
- Performance issue when decorator files are loaded multiple times HOT 4
- Collections `to_json(included: 'xxx')` doesn't output included records HOT 9
- Representer attributes with null values are excluded from resource document
- Should be able to return relationship links without including the related representer
- How to set the type of included items? HOT 5
- Using JSONAPI HOT 4
- [JSON-API] Rendering an empty relationship HOT 3
- [JSON-API] Recursive has_many :children (include tree) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roar-jsonapi.