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

Make a url generator available in serializers #481

Closed

Conversation

stevenharman
Copy link

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 as urls) object available from within any instance of ActiveModel::Serializer, and any associated serializers.

In a Rails app, we will automatically inject an instance of ActiveModel::Serializer::UrlGenerator when building a serializer from the ActionController. The same seam can be used to inject a url generator/helper in non-Rails apps.

@@ -60,6 +61,10 @@ def serializer_for(resource)
end
end

def url_generator(*args)
Copy link
Author

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.

@stevenharman
Copy link
Author

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?

@stevenharman
Copy link
Author

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
Copy link
Contributor

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.

@steveklabnik
Copy link
Contributor

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.
@stevenharman
Copy link
Author

I cannot seem to get the specs passing on Rails 3.2 - it's as if my resources :posts routes don't end up in the RouteSet used by url_for. Does anyone have an idea what I can do to fix that?

@steveklabnik
Copy link
Contributor

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 0-9-stable branch anyway. If you figure this out, let me know 😦 😢

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

Successfully merging this pull request may close these issues.

None yet

3 participants