Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Account Scopper: Automatically scope your ActiveRecord's model by account. Ideal for multi-account applications.
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
account_scopper.gemspec
init.rb

README.rdoc

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.

Account Scopper is NOT working with Rails 3.x

Due the the heavy lifting that Rails 3 had, account_scopper is NOT working with Rails 3.x and later

Install

If new to gemcutter first run

sudo gem install gemcutter
sudo gemcutter tumble

Then run

sudo gem install account_scopper

If using rails, in your `config/environment.rb`

config.gem 'account_scopper'

Usage

To convert your application, you will need to make few changes :

  1. Create an Account model and Accounts table

  2. Add account_id to your existing models (except Account of course)

  3. Define relationship between Account and other models

  4. Add the class variable “current_account” to the model Account

  5. 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.

Helpers

As account_scopper scopes all database requests, `validates_uniqueness_of` will also scope to the current account.

In some cases, you may like to make sure that somethings remain unique to your whole system.

(eg: the user login or email if logging in from a common page for all accounts)

For this purpose, you can use `validates_global_uniqueness_of`. This validations will make sure your attribute is unique over all accounts.

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.

Something went wrong with that request. Please try again.