Module: Flipper
- Defined in:
- lib/flipper.rb
Overview
Include this module in your Rails controller to change database schema on each HTTP request:
class ApplicationController < ActionController::Base
include Flipper
before_filter :switch_database # if RAILS_ENV == "selenium"
...
end
Instance Method Summary collapse
-
#switch_database ⇒ Object
Switch current database based on
flipper_db_config
HTTP parameter, if any, and store database configuration in session.
Instance Method Details
#switch_database ⇒ Object
Switch current database based on flipper_db_config
HTTP parameter, if any, and store database configuration in session.
If there is no flipper_db_config
parameter, switch to the database configuration stored in session.
If anything goes wrong while switching database, attempts to rollback to the previous database configuration before raising an exception.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/flipper.rb', line 22 def switch_database database_configuration = params[:flipper_db_config] || session[:flipper_db_config] return unless database_configuration prior_configuration = ActiveRecord::Base.connection.instance_variable_get(:@config) ActiveRecord::Base.clear_active_connections! begin ActiveRecord::Base.establish_connection database_configuration session[:flipper_db_config] = database_configuration rescue => ex ActiveRecord::Base.establish_connection prior_configuration raise end end |