0

(Ruby 2.3.1, Rails 5.0.0.1)

I've already implemented an API versioning in the URI and added devise token auth like this:

constraints subdomain: 'api' do
    scope module: 'api' do  
        namespace :v1 do
            # V1 stuff
            resources :users
            mount_devise_token_auth_for 'User', at: 'auth'
        end
    end
end

The result of 'rake routes' (example row) therefore is:

v1_user GET /v1/users/:id(.:format) api/v1/users#show {:subdomain=>"api"}

Now in my controller, any helper related to the user contains the "v1" versioning. E.g.:

before_action :authenticate_v1_user!

I'm new to this concept, but should this not be avoided? If I'll ever upgrade to a v2, I'd have to go through all those helpers and make sure I'll change it to prevent it from breaking? Or how would the upgrade process look like?

Thanks for your feedback! This might be a very easy question, but I'm a bit confused.

Shuhari
  • 111
  • 1
  • 7
  • I'd avoid using versioning in names until you at least have 2 different versions... so just call it `authenticate_user!` until you have two different ways of authenticating, then you could have `authenticate_v1_user` vs `authenticate_v2_user` to differentiate... though tbh I'd handle different ways of doing things using `roles` instead (still keep the v1 etc in the routes). – Taryn East Sep 27 '16 at 22:51

1 Answers1

0

Okay, I've kept searching a bit and found a solution, which works (even though it feels a bit too "hacky" for my taste):

namespace :v1, as: '' do
    # V1 stuff
    resources :users
    mount_devise_token_auth_for 'User', at: 'auth'
end

So the as: '' does the trick. (Answer taken from: Adding prefix to a named route helper under namespace)

If I'd ever want the "v1" back into my helper names, I guess I could manually add them per route with for example: as: 'new_v1_user_sesssion'

Community
  • 1
  • 1
Shuhari
  • 111
  • 1
  • 7