Module: Cell::Ext::ActiveRecord

Defined in:
lib/cell/ext/active_record.rb

Defined Under Namespace

Modules: Association, ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tenantObject (readonly)

Returns the value of attribute tenant.



41
42
43
# File 'lib/cell/ext/active_record.rb', line 41

def tenant
  @tenant
end

Class Method Details

.prepended(cls) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/cell/ext/active_record.rb', line 65

def self.prepended(cls)
  class << cls
    prepend ClassMethods
  end

  cls.after_initialize :_assign_tenant
  cls.around_save :_activate_tenant
end

Instance Method Details

#_activate_tenant(&block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/cell/ext/active_record.rb', line 49

def _activate_tenant(&block)
  if tenant && tenant != Model.current
    tenant.use(&block)
  else
    yield
  end
end

#_assign_tenantObject



43
44
45
46
47
# File 'lib/cell/ext/active_record.rb', line 43

def _assign_tenant
  unless self.class.global_model?
    @tenant = Model.current
  end
end

#run_validations!Object

undocumented, but makes sure validations are ran in the context of the object’s tenant, e.g., uniqueness.



59
60
61
62
63
# File 'lib/cell/ext/active_record.rb', line 59

def run_validations!
  _activate_tenant do
    super
  end
end