Open ysyyork opened 8 years ago
It's certainly possible to support this, but clearly it's not handling this correctly currently.
Is there any walk around to achieve this for now?
I tried a few things but couldn't get it to work. You'll need to deal with this manually or just don't use the generators.
Thanks. I will do it manually.
On Monday, August 15, 2016, Brian Ploetz notifications@github.com wrote:
I tried a few things but couldn't get it to work. You'll need to deal with this manually or just don't use the generators.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bploetz/versionist/issues/77#issuecomment-239804167, or mute the thread https://github.com/notifications/unsubscribe-auth/AGYi5ZwYt6gx-nF8SBQxSdByVIWX6-32ks5qgGxYgaJpZM4JjO9i .
I would like to do the inverse(?) of this and use a super-namespace. ie. support routes like /api/v1/my_action
. Problem is that if you set api_version(:module => "API::V1",...
then the "api" isn't included in the routes and you end up with just /v1/my_action
If you wrap the api_version in a namespace like this:
namespace :api do
api_version(:module => "V1", :default => true,
:header => {:name => "Accept", :value => "application/vnd.api+json;version=1"},
:path => {:value => "v1"}) do
resources :users
end
end
Then while you do get nice routes like: api_v1_users GET /api/v1/users(.:format) api/v1/users#index
you can no longer use generators:
> rails g versionist:new_controller clients V1 -p
/Users/gingitsune/.rvm/gems/ruby-2.4.1/gems/versionist-1.5.0/lib/generators/versionist/new_controller/new_controller_generator.rb:13:in `block in new_controller': API module namespace V1 doesn't exist
The same is true for running the generator with various combinations of API::V1
and whatnot.
Any thoughts on how I could make this work cleanly?
ah... nm. I just discovered that the case for the super-namespace can be achieved like this:
api_version(:module => "Api::V1",:path => {:value => "api/v1"}) do
resources :users
end
This lets you generate using rails g versionist:new_controller users Api::V1
which works fine.
Note though that the casing is capitalized not upcase on "Api". This is because if you have a class like class API::V1::UsersController < API::V1::BaseController
in the file /app/controllers/api/v1/users_controller
it breaks rails naming convention. It should instead be: class Api::V1::UsersController < Api::V1::BaseController
even though API is an acronym.
PS. Sorry to hijack the sub-namespace thread. It would also be an excellent feature in the generators, as would traversing namespaces rather than requiring the namespace be baked into the module param.
@Genkilabs re: API vs Api, welcome to the wonderful world of the Rails inflector. :) I'm pretty sure you can override this behavior if you prefer all uppercase. See http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html#method-i-acronym
My use case is like below:
I use the generator to do this like:
rails g versionist:new_controller stores/registrations V1
But it will not count that stores as a module. Is it possible to support this use case? Or there is other better way to organize the structure? Thanks.