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
Make a url generator available in serializers #481
Make a url generator available in serializers #481
Conversation
@@ -60,6 +61,10 @@ def serializer_for(resource) | |||
end | |||
end | |||
|
|||
def url_generator(*args) |
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.
We might want to remove this facade to keep ActiveModel::Serializer
ignorant of the actual ActiveModel::Serializer:UrlGenerator
implementation when not running in a Rails app.
This kind of feature is desperately needed for building hypermedia APIs, and will most certainly be required for JSON API support. Any thoughts on it? Is there a different approach that would be preferred? |
The tests are all passing for Rails 4+, but I something about the routing, or how we're hooking the RouteSet up in Rails 3.2 is not working as I'd expect. Anyone have any ideas what might be going wrong? |
@@ -10,11 +10,13 @@ | |||
module TestHelper | |||
Routes = ActionDispatch::Routing::RouteSet.new | |||
Routes.draw do | |||
resource :post |
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.
I remember having trouble with url helpers for singular resources in Rails 3. Would switching to the plural resources :posts
routes make sense here? Might fix your Rails 3 test failures.
Can this be rebased, please? |
This technique ties this usage to Rails, which but makes the helpers directly accessible w/in any instance of ActiveModel::Serializer, including any associations.
This is done automagically for Rails, but non-Rails apps can pass along their own url generators/helpers to be used within serialzers.
This is useful when running either outside of Rails, or in a background job where you won't have access to the current url options for a request.
This also meant defining our own Model#to_param to we could correctly serialize, rather than falling back on the default #to_s implementation.
I cannot seem to get the specs passing on Rails 3.2 - it's as if my |
I restarted the build, and it's still failing on 3.2 😢 I'm giving this a close because it would have to be re-done against the new |
This is built for 0.9, as an alternative to #480, and is an attempt to allow generation of URLs within serializers. This makes a
url_generator
(aliased asurls
) object available from within any instance ofActiveModel::Serializer
, and any associated serializers.In a Rails app, we will automatically inject an instance of
ActiveModel::Serializer::UrlGenerator
when building a serializer from theActionController
. The same seam can be used to inject a url generator/helper in non-Rails apps.