Reputation: 85
We have a versioned API that follows all the conventions of Rails API versioning. i.e
module API::V4
class Test < ApiController
end
end
But is there a proper way to version helpers? The current helpers directory looks like something like this:
app
|__helpers
|__helper_a.rb
|__helper_b.rb
and all helpers are defined modules.
Is there a way to do this?
module Helpers::V2
class HelperA
end
end
I tried creating a directory app/helpers/v2/helper_a.rb
, adding app/helpers/helpers.rb
and defining module Helpers
But for some reason rails always fails to see Helpers::V2::HelperA
Upvotes: 1
Views: 216
Reputation: 164859
Rails searches each subdirectory of app
starting from the subdirectory. app/models/foo/bar.rb
contains Foo::Bar
not Models::Foo::Bar
. app/controllers/api/v4/test.rb
contains Api::V4::Test
, not Controllers::Api::V4::Test
.
So app/helpers/v2/helper_a.rb
contains V2::HelperA
.
module V2
module HelperA
end
end
If you want to relate the helper version with your API version, it makes sense to mirror the directory structure.
# app/helpers/api/v2/helper_a.rb
# A helper for API v2.
module Api
module V2
module HelperA
end
end
end
Note that it's Api
to follow Rails conventions. The autoloader will map app/helpers/api/v2/helper_a.rb
to Api::V2::HelperA
. It might work with the Rails 5 autoloader, but not the Rails 6 autoloader.
If we use API::V2::HelperA
...
[1] pry(main)> API::V2::HelperA
NameError: uninitialized constant API
Did you mean? Api
Upvotes: 2