Module: Multitenant::ActiveRecordExtensions

Defined in:
lib/multitenant.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_multitenant(association = :tenant) ⇒ Object

configure the current model to automatically query and populate objects based on the current tenant see Multitenant#current_tenant



21
22
23
24
25
26
27
28
29
30
# File 'lib/multitenant.rb', line 21

def belongs_to_multitenant(association = :tenant)
  reflection = reflect_on_association association
  before_validation Proc.new {|m|
    return unless Multitenant.current_tenant
    m.send "#{association}=".to_sym, Multitenant.current_tenant
  }, :on => :create
  default_scope lambda {
    where({reflection.foreign_key => Multitenant.current_tenant.id}) if Multitenant.current_tenant
  }
end