bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
972 stars 51 forks source link

What's my version? #49

Closed softwaregravy closed 8 years ago

softwaregravy commented 11 years ago

I'm not sure if this is a feature request or a help request.

I've decided to use versionist for my api routing and active_model_serializers to manage my presentation of my objects in the json object. I'm using https://github.com/hookercookerman/active_model_version_serializers so that I can version my model serialization (no context with that gem needed to understand this issue).

I have a common base class for all my controllers of a given version, and right now it looks like this:

class Api::V20130701::BaseController < Api::ApiController
  def default_serializer_options
    {version: :V20130701}
  end 
end

The version arg there tells active_model_version_serializers what version to use unless told otherwise. It matches my version from my routes file.

I would love to be able to get the current versionist module from the request somehow. I would like to get this from a standard place. That is agnostic to what options I've set up versionist with (header vs url param).

As an example, I'd be able to do something like:

class Api::V20130701::BaseController < Api::ApiController
  def default_serializer_options
    {version: api_version}
  end 
end

Where api_version is a helper method added to ActionController::Base. I'm pretty indifferent to the mechanism used (params? env?), but I would like to get the version of the current request.

Thanks,

Adding routes.rb snippet just in case:

  namespace :api do  
    api_version(:module => "V20130701", 
                :header => {:name => "API-VERSION", :value => "20130701"}, 
                :defaults => {:format => "json"}, 
                :default => true) do
bploetz commented 11 years ago

Where do you need this version, and why? The example you give above is in your BaseController, but if that controller gets called, you know your version: it's the version of that controller (V20130701 in the example).

Let me know....

softwaregravy commented 11 years ago

More of a code-duplication thought.

If I could pull it out of the request and not hard-code it, then I could define the versions in one place -- the routes file. It's just another moving piece that would be nice to remove.

dan-corneanu commented 6 years ago

Was there any resolution on this issue?

bploetz commented 6 years ago

@dan-corneanu Not really. I think it would be trivial to add something like this, but I'm still curious as to the use cases where you'd need this.

softwaregravy commented 6 years ago

The goal was just not to have the String in the routes file, and also in the class. It's a minor point, but requires having the same string in a couple of places.

It's been a long time since I opened this. Since I never followed up answering Brian's very prompt response to my original question, it must not have been that important.

(But I will add a thank you to @bploetz for providing the package and maintaining it)