Skip to content

Add controller lookup. #1265

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

Closed
wants to merge 1 commit into from
Closed

Conversation

beauby
Copy link
Contributor

@beauby beauby commented Oct 10, 2015

This PR adds the controller's namespace to the lookup chain, such that the following works:

class User < ActiveModel::Base
  ...
end

module Api
  module V1
    class UsersController < ApplicationController
      def index
        render json: User.all
      end
    end
    class UserSerializer < ActiveModel::Serializer
      ...
    end
  end
end

@NullVoxPopuli
Copy link
Contributor

Nice!! I'm really looking forward to this. 'automatic' api versioning will be a huge help to a lot of people.

@@ -41,7 +41,7 @@ def serializer
@serializer ||=
begin
@serializer = serializer_opts.delete(:serializer)
@serializer ||= ActiveModel::Serializer.serializer_for(resource)
@serializer ||= ActiveModel::Serializer.serializer_for(resource, serializer_opts.slice(:_controller_class_name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think passing the controller name to serializer_for seems weird. Would just passing the namespace make more sense? so that way you don't need to deconstantize that later?

@NullVoxPopuli
Copy link
Contributor

related: #886, #671, #562 (@beauby, I actually had a version of this going a long time ago, before that project gave up on the idea of having a versioned API), #539, #499, #381, #144, #108

@bf4
Copy link
Member

bf4 commented Oct 12, 2015

So, is the controller being used as a serializer lookup hint? If so, I'd rather allow people to add 'hints' (I think this is was Grape calls them). I think passing the controller class all the way through AMS has pretty low value, since it adds a lot of noise for what could be resolved by specifying the class name.

I'd rather work on the JSON API unless there's a lot of pressure for something like this.

@bf4 bf4 added the S: Hold label Oct 12, 2015
@beauby
Copy link
Contributor Author

beauby commented Oct 12, 2015

@bf4 Agreed, I made this PR because I knew how to do it without much effort, but it's not among the top priority stuff.
The one use-case I think this is really helpful is when handling two versions of a same API (say in Api::V1 and Api::V2) where the models do not change, but the serializers do.

@bf4
Copy link
Member

bf4 commented Oct 12, 2015

Yeah, def something to figure out, maybe with a serializer version param or namespace param (which could be derived from controller or specufied)

@NullVoxPopuli
Copy link
Contributor

@bf4 could you elaborate?

I really like @beauby's approach, as it allows for a mirrored file/folder structure of api versions between the 'serializers' and controllers

@bf4
Copy link
Member

bf4 commented Oct 12, 2015

Yeah, as controllers mirror resource routes, i agree serializers should reflect those paths...

Let's look at how other libs do it

@NullVoxPopuli
Copy link
Contributor

Do you know of other libs that do this?

I think other than how the namespace (re: not the namespace, but the controller class) is passed to the serializable resource, the implementation is pretty good.

@NullVoxPopuli
Copy link
Contributor

needs rebase

@beauby
Copy link
Contributor Author

beauby commented Oct 28, 2015

@NullVoxPopuli I'll eventually rebase this if we end up deciding to include it. Currently, it is more meant as a proof of concept than anything else.

@NullVoxPopuli
Copy link
Contributor

Well, my vote is for including it :-)

@beauby
Copy link
Contributor Author

beauby commented Oct 28, 2015

I wouldn't mind including it either, but I'd also like for one of @hut8's (I believe) @tchak's PRs that creates a context object, in which we could stick the controller class so that this feature needs less hacking.

@NullVoxPopuli
Copy link
Contributor

fair enough :-)

@tchak
Copy link
Contributor

tchak commented Oct 30, 2015

I would prefer a way to pass in an api version... I feel like the lookup (serializer_for) should take the whole options hash, in order to make possible some advanced use cases.

@NullVoxPopuli
Copy link
Contributor

there are many ways to handle api versioning.

Some people like to have it namespaced in their routes, others like to have the version specified in a header.

We may need to support both.

@bf4
Copy link
Member

bf4 commented Oct 30, 2015

@beauby This PR should be superseded by #1289

@beauby
Copy link
Contributor Author

beauby commented Oct 30, 2015

@bf4 I don't think so, as you don't want to replace, say the current_user with current_controller_class or something. Instead, I think it would be nice to rebuild this PR on top of @tchak's PR introducing the serialization_context object.

@bf4
Copy link
Member

bf4 commented Oct 30, 2015

@beauby yeah, I pasted the wrong link.. edited comment above to refer to #1289 (comment)

@bf4
Copy link
Member

bf4 commented Nov 19, 2015

Should this PR still be open?

@elfassy
Copy link

elfassy commented Jan 11, 2016

Will this get merged? Or is there something else taking its place?

@beauby
Copy link
Contributor Author

beauby commented Jan 11, 2016

@elfassy This PR will probably not get merged, as it was meant as a proof of concept and much has changed in the meantime. I still think the feature would be useful, and would support a PR doing this in a clean way, or maybe issue one myself when I have a bit of free time.

@beauby
Copy link
Contributor Author

beauby commented Jan 18, 2016

Closing in favor of #1436.

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

Successfully merging this pull request may close these issues.

5 participants