Sha256: fd150ece3fe0513934a3f324e9cc9c1e8c3ed80dda9bb26fa8c6d2cb23551215
Contents?: true
Size: 1.18 KB
Versions: 1
Compression:
Stored size: 1.18 KB
Contents
require 'active_record' # Multitenant: making cross tenant data leaks a thing of the past...since 2011 module Multitenant class << self def current_tenant=(value) Thread.current[:current_tenant] = value end def current_tenant Thread.current[:current_tenant] end # execute a block scoped to the current tenant # unsets the current tenant after execution def with_tenant(tenant, &block) Multitenant.current_tenant = tenant yield ensure Multitenant.current_tenant = nil end end module ActiveRecordExtensions # configure the current model to automatically query and populate objects based on the current tenant # see Multitenant#current_tenant def belongs_to_multitenant(association = :tenant) reflection = reflect_on_association association before_validation Proc.new {|m| m.send("#{association}=".to_sym, Multitenant.current_tenant) if Multitenant.current_tenant }, :on => :create default_scope lambda { where({reflection.foreign_key => Multitenant.current_tenant.id}) if Multitenant.current_tenant } end end end ActiveRecord::Base.extend Multitenant::ActiveRecordExtensions
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
multitenant-1.0.0 | lib/multitenant.rb |