Created
March 14, 2014 04:35
-
-
Save csexton/9542200 to your computer and use it in GitHub Desktop.
Extend the jbuilder api to support JSON API conventions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Jbuilder | |
def links! model, *args | |
args.each do |arg| | |
if model.reflections[arg].collection? | |
_set_value arg, model.send(arg).map(&:id) | |
else | |
_set_value arg, model.send(arg).id | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"posts": [ | |
{ | |
"links": { | |
"comments": [1,2,3,4,5], | |
"user": 2, | |
"tags": [30,31,32,33] | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json.posts @posts do |post| | |
json.links do | |
json.links! post, :comments, :user, :tags | |
end | |
end |
π π₯ π π₯ π π₯ π π₯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes please!