Multiview
Support multiple versions for Rails views
Installation
Add this line to your application's Gemfile:
gem 'multiview'
And then execute:
$ bundle
Usage
Example Project
app/controllers/topics_controller.rb
class TopicsController < ApplicationController
def index
render plain: 'hello'
end
end
app/controllers/v2/topics_controller.rb
class V2::TopicsController < ApplicationController
def index
render plain: 'hello v2'
end
end
config/routes.rb
resources :topics
Init
config/initializers/multiview.rb
$multiview = Multiview::Manager.new({
'topics' => 'v2'
})
# or use default manager: Multiview.manager
Redispatch request by filter of controller
When get /topics
, should redispatch to V2::TopicsController#index
, rether than TopicsController#index
If delete V2::TopicsController
or change that config {'topics' => nil}
, it will call TopicsController#index
class ApplicationController < ActionController::Base
before_aciton :set_view_version_filter
.
def set_view_version_filter
$multiview.redispatch(self)
# or
# $multiview.redispatch(self, params[:controller], params[:action])
# if exist V3::XxxController, should to call V3::XxxController
# if not, should to call XxxController and prepend v3 views path
# when you only want to change something for views, you should not to create V3::XxxController for the version, just to make a view folder `app/views/v3/xxx`
# $multiview.redispatch(self, params[:controller], params[:action], 'v3')
end
end
Limitation: We only overwrite those action about already defined.
Dispatch request by middleware
class MultiviewMiddleware
def initialize(app)
@app = @app
end
def call(env)
path_info = Rails.application.routes.recognize_path(env['PATH_INFO'])
if path_info && path_info[:contorller]
$multiview.dispatch(env, path_info[:controller], path_info[:action])
# or
# $multiview.dispatch(env, path_info[:controller], path_info[:action], 'v2')
else
@app.call(env)
end
end
end
config/application.rb
config.middleware.use MultiviewMiddleware
Limitation: Depend Rails.application.routes.recognize_path, if you have some specific routes, might it cannot work.
Anyway, it soloved that problem of before_action
Testing
Beacuse we not define a route to our controller, when send test request, we should get a error like 'No route matches ...'.
RSpec
config.before(:each, type: :controller, multiview: true) do
# Fix testing route
route_path = described_class.controller_path.gsub(/^(v2|xxx|my_version)\//, '')
allow(described_class).to receive(:controller_path).and_return(route_path)
# disable redispatch
request.env['multiview'] = {'version' => 'xxx'}
end
spec/xxx/yyy_controller_spec.rb
RSpec.describe Xxx::YyyController, multiview: true do
# write some testing
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/xiejiangzhi/multiview. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Multiview project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.