Module: Mongoid::Multitenancy::Document::ClassMethods

Defined in:
lib/mongoid/multitenancy/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#full_indexesObject

Returns the value of attribute full_indexes.



7
8
9
# File 'lib/mongoid/multitenancy/document.rb', line 7

def full_indexes
  @full_indexes
end

#tenant_fieldObject

Returns the value of attribute tenant_field.



7
8
9
# File 'lib/mongoid/multitenancy/document.rb', line 7

def tenant_field
  @tenant_field
end

Instance Method Details

#delete_all(conditions = nil) ⇒ Object

Redefine ‘delete_all’ to take in account the default scope



79
80
81
# File 'lib/mongoid/multitenancy/document.rb', line 79

def delete_all(conditions = nil)
  scoped.where(conditions).delete
end

#index(spec, options = nil) ⇒ Object

Redefine ‘index’ to include the tenant field in first position



73
74
75
76
# File 'lib/mongoid/multitenancy/document.rb', line 73

def index(spec, options = nil)
  spec = { self.tenant_field => 1 }.merge(spec) if self.full_indexes
  super(spec, options)
end

#tenant(association = :account, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongoid/multitenancy/document.rb', line 9

def tenant(association = :account, options = {})
  options = { full_indexes: true }.merge(options)
  active_model_options = options.clone
  to_index = active_model_options.delete(:index)
  tenant_options = { optional: active_model_options.delete(:optional), immutable: active_model_options.delete(:immutable) { true } }
  self.full_indexes = active_model_options.delete(:full_indexes)

  # Setup the association between the class and the tenant class
  belongs_to association, active_model_options

  # Get the tenant model and its foreign key
  tenant_field = reflect_on_association(association).foreign_key
  self.tenant_field = tenant_field

  # Validates the tenant field
  validates tenant_field, tenant: tenant_options

  # Set the current_tenant on newly created objects
  before_validation lambda { |m|
    if Multitenancy.current_tenant and !tenant_options[:optional] and m.send(association.to_sym).nil?
      m.send "#{association}=".to_sym, Multitenancy.current_tenant
    end
    true
  }

  # Set the default_scope to scope to current tenant
  default_scope lambda {
    criteria = if Multitenancy.current_tenant
      if tenant_options[:optional]
        #any_of({ self.tenant_field => Multitenancy.current_tenant.id }, { self.tenant_field => nil })
        where({ tenant_field.to_sym.in => [Multitenancy.current_tenant.id, nil] })
      else
        where({ tenant_field => Multitenancy.current_tenant.id })
      end
    else
      where(nil)
    end
  }

  self.define_singleton_method(:inherited) do |child|
    child.tenant association, options
    super(child)
  end

  if to_index
    index({self.tenant_field => 1}, { background: true })
  end
end

#validates_with(*args, &block) ⇒ Object

Redefine ‘validates_with’ to add the tenant scope when using a UniquenessValidator



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongoid/multitenancy/document.rb', line 59

def validates_with(*args, &block)
  validator = if Mongoid::Multitenancy.mongoid4?
    Validatable::UniquenessValidator
  else
    Validations::UniquenessValidator
  end

  if args.first.ancestors.include?(validator)
    args.last[:scope] = Array(args.last[:scope]) << self.tenant_field
  end
  super(*args, &block)
end