Differentiate development environment from production. You can choose themes to differentiate the page.
The running sample is available.
You can also try this gem on Heroku.
Add the rack-dev-mark gem to your Gemfile.
gem "rack-dev-mark"
And run bundle install
.
require 'rack/dev-mark'
use Rack::DevMark::Middleware
run MyApp
Add the settings in config.rb
.
require 'rack/dev-mark'
Rack::DevMark.env = "Your Env"
use Rack::DevMark::Middleware
In config/environments/development.rb
Rails.application.configure do
config.rack_dev_mark.enable = true
end
Or
In config/application.rb
module MyApp
class Application < Rails::Application
config.rack_dev_mark.enable = !Rails.env.production?
end
end
Or Alternatively, use generator
bundle exec rails g rack:dev-mark:install
The middleware sets title and github_fork_ribbon themes as default.
Show the dev mark except env1, env2, env3.
In config/application.rb
module MyApp
class Application < Rails::Application
config.rack_dev_mark.enable = !%w(env1 env2 env3).include?(Rails.env)
end
end
Since Heroku uses production env for staging. You can't use the settings above. However, the gem provide an easier way to set it up on Heroku. Just set the environment variable on the environment in which you want to show the mark.
heroku config:set RACK_DEV_MARK_ENV=staging
That's it!
rack-dev-mark
should be inserted before ActionDispatch::ShowExceptions
becase we want to show the dev mark on the error pages as well. However, it does not work well if it's inserted by incorrect order with some other rack middlewares. So, it provides the setting of inserted place.
module MyApp
class Application < Rails::Application
config.rack_dev_mark.insert_before SomeOtherMiddleware
end
end
config.rack_dev_mark.insert_after
is also available to insert rack-dev-mark
after a middleware.
Here is the compatibility list which many people often ask.
Set the custom env string maually.
module MyApp
class Application < Rails::Application
config.rack_dev_mark.env = 'foo'
end
end
skip_rack_dev_mark
controller helper works like around_filter
.
class FooController < ApplicationController
skip_rack_dev_mark only: [:iframe]
def index
# Do something
end
def iframe
# Do something
end
end
In this case, only index
action will insert the dev mark.
Get i18n string with rack_dev_mark locale strings.
e.g. In config/locale/rack_dev_mark.ja.yml
ja:
rack_dev_mark:
development: '開発中'
staging: 'ステージング'
Then, you will get translated string on the pages!
Although the default themes are title
and github_fork_ribbon
, you can create your own themes inheriting Rack::DevMark::Theme::Base
.
require 'rack/dev-mark/theme/base'
class NewTheme < Rack::DevMark::Theme::Base
def insert_into(html, env, params = {})
# Do something for your theme
html
end
end
class AnotherTheme < Rack::DevMark::Theme::Base
def insert_into(html, env, params = {})
# Do something for your theme
html
end
end
Then, insert them in your app.
use Rack::DevMark::Middleware, [NewTheme.new, AnotherTheme.new]
In config/application.rb
module MyApp
class Application < Rails::Application
config.rack_dev_mark.theme = [NewTheme.new, AnotherTheme.new]
end
end
You can add any combination of themes. See more about themes.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright (c) 2014 Daisuke Taniwaki. See LICENSE for details.