-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add support for relationship-level links and meta attributes. #1454
Conversation
796a105
to
4239e50
Compare
4239e50
to
061f1c0
Compare
Travis is failing because of the rubocop config (https://github.com/rails-api/active_model_serializers/blob/master/.rubocop.yml#L40 – we should allow hash[association.key] = JsonApi::Association.new(serializer,
association.serializer,
association.options,
association.links,
association.meta) Expected by rubocop: hash[association.key] = JsonApi::Association.new(serializer,
association.serializer,
association.options,
association.links,
association.meta) |
|
||
hash | ||
[@primary, @included] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for the instance variables here?
Looks pretty good, 😄 though, since both there are boundary changes along with refactoring and new behavior, I'll want to come back and look at what has 'changed' |
Any update on this PR? Is there something needed to merge this PR? I could work on it if needed. |
end | ||
end | ||
|
||
def id_for(serializer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be serializer.read_attribute_for_serialization(:id)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be on the same page here, that would change the current behaviour, right? (see https://github.com/beauby/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json_api.rb#L125)
And I guess you meant serializer.object.read_attribute_for_serialization(:id)
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, I did indeedy mean to call it on the serializer, but @groyoh thanks for pointing out my error that this was just a direct port of the method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you're right, I didn't realized that read_attribute_for_serialization
was now defined on the serializer.
Merged. Added Needs follow up tag. |
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
is there a way to use Rails URL helpers in links? like this: has_many :posts do
link :related do
href api_author_posts_url(author: object)
end
include_data false
end |
@janvarljen I did the following hack to get what you're after. module ActiveModel
class Serializer
module Adapter
class JsonApi
class Link
def routes
Rails.application.routes.url_helpers
end
end
end
end
end
end has_many :posts do
link :related do
href routes.api_author_posts_url(author: object)
end
include_data false
end |
@sheldonnbbaker Nice hack but I'd recommend: UrlHelpers = Rails.application.routes.url_helpers has_many :posts do
link :related do
href UrlHelpers.api_author_posts_url(author: object)
# or
# href Rails.application.routes.url_helpers.api_author_posts_url(author: object)
end
include_data false
end if you want to avoid monkey patching AMS. @janvarljen I don't think there is a cleaner way at the moment. |
@sheldonnbbaker @janvarljen @remear @groyoh the url_helpers issue is #1269 just needs a PR.. good stuff above |
@sheldonnbbaker @groyoh thx for the ideas. I'm doing the same thing atm but wanted to discuss a more elegant solution. One more thing I think we're missing in these solutions is url_options context like :host, :protocol etc... we need for url_helpers to work as expected. They way I do it: in api_controller serialization_scope :scope
def scope
{
url_options: url_options
}
end in base_serializer include Rails.application.routes.url_helpers
def url_options
scope[:url_options]
end then I can use url helpers in serializer methods but not inside link block because we don't have access to the serializer methods |
I'm working on adding the url_helpers to 0.10 so you won't need a workarounds like these. If all goes well, that means you should have the ability to do things like: link :self { user_url(object) }
link :self { url_for([object, :users, @user]) }
link :self { "http://example.com/posts/#{object.id}" }
link :self, "http://example.com/user" |
@remear that would be great. Ping me if you need any help |
ref: #1269 |
Maybe a 'wip' pr? On Thu, Feb 25, 2016 at 12:37 PM Jan Varljen notifications@github.com
|
@janvarljen, @sheldonnbbaker Feedback is welcome on #1550, which aims to add url_helpers for links. |
Does anyone know when this functionality will be available in active_model_serializers gem? |
you can install from our master branch until we cut another release -- would that work? |
I am not seeing these changes on |
This PR and #1550, which adds Rails url_helpers, have been merged into the master branch. They are not yet available in a version available from RubyGems. You'll need to have your application use the master branch of this repository until the next release. If you're having other troubles, I suggest opening a new issue with more details. |
👍 |
Based on top of #1447.
Allows the following: