Account Scopper
Account Scopper aims to make conversion from a single account to multi-accounts application as easy as possible.
Simply set your current_account before each request and the scoping will be done on it’s own.
Usage
To convert your application, you will need to make few changes :
-
Create an Account model and Accounts table
-
Add account_id to your existing models (except Account of course)
-
Make relationship between Account and other models
-
Add the class variable “current_account” to the model Account
-
Initialize Account.current_account before every request (eg: app/controllers/application.rb)
eg:
# app/model/account.rb
class Account < ActiveRecord::Base
cattr_accessor :current_account
has_many :users
end
# app/model/user.rb
class User < ActiveRecord::Base
belongs_to :account
end
# app/controllers/application.rb
class ApplicationController < ActionController::Base
before_filter :set_current_account
# ...
private
# Don't forget that @current_account should be properly setup to be an Account object
# for this purpose you can use the plugin AccountLocation from David Heinemeier Hansson
def set_current_account
Account.current_account = @current_account
end
end
What it does
The plugin overwrite few methods from ActiveRecord::Base
To have a better understanding of what it does, the best way is still to look at the code itself and the tests.
Warning
If using manually generated database requests like find_by_sql, … you’ll have to do your own scoping
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright
Copyright © 2009 Sebastien Grosjean. See LICENSE for details.