OauthMultiDb
Switch rails app's db based on incoming oauth client
Installation
Add this line to your application's Gemfile:
gem 'oauth_multi_db'
And then execute:
$ bundle
Or install it yourself as:
$ gem install oauth_multi_db
Usage
For each request, if oauth client is found. it retrieves client.client_symbol value (from notes field),
and uses it to switch to corresponding db.
To set client_symbol value on Oauth2 client, do it when your register it:
Rack::OAuth2::Server.register(
:display_name => 'oauth client 1',
:notes => {'client_symbol' => 'client_1'}.to_json
)
Setup
- In rails app, create config/initializers/oauth_multi_db_config.rb, with code below:
require 'oauth_multi_db'
OauthMultiDb.configure do |config|
config.domain_models = [User, Article]
config.db_switcher = lambda {|client_symbol|
new_db = "db_" + client_symbol.to_s
OauthMultiDb.config.domain_models.each do |klass|
end
}
end
- in your controller, use the macro
class ApplicationController
oauth_db_switch
oauth_db_swith, :only => [:create, :update]
end
This gem requires rack-oauth2-server gem. see https://github.com/assaf/rack-oauth2-server for more info.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request