Module: Mongoid::Multitenancy::Document::ClassMethods
- Defined in:
- lib/mongoid/multitenancy/document.rb
Instance Attribute Summary collapse
-
#tenant_field ⇒ Object
Returns the value of attribute tenant_field.
Instance Method Summary collapse
-
#delete_all(conditions = nil) ⇒ Object
Redefine ‘delete_all’ to take in account the default scope.
-
#index(spec, options = nil) ⇒ Object
Redefine ‘index’ to include the tenant field in first position.
- #tenant(association = :account, options = {}) ⇒ Object
-
#validates_with(*args, &block) ⇒ Object
Redefine ‘validates_with’ to add the tenant scope when using a UniquenessValidator.
Instance Attribute Details
#tenant_field ⇒ Object
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
61 62 63 |
# File 'lib/mongoid/multitenancy/document.rb', line 61 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
55 56 57 58 |
# File 'lib/mongoid/multitenancy/document.rb', line 55 def index(spec, = nil) spec = { self.tenant_field => 1 }.merge(spec) super(spec, ) 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 |
# File 'lib/mongoid/multitenancy/document.rb', line 9 def tenant(association = :account, ={}) = { optional: .delete(:optional), immutable: .delete(:immutable) { true } } # Setup the association between the class and the tenant class # TODO: should index this association if no other indexes are defined => , index: true belongs_to association, # Get the tenant model and its foreign key tenant_field = reflect_on_association(association).foreign_key self.tenant_field = tenant_field # Validates the presence of the association key validates_presence_of tenant_field unless [:optional] validates tenant_field, immutable: { field: tenant_field } if [:immutable] # Set the current_tenant on newly created objects after_initialize lambda { |m| if Multitenancy.current_tenant #and !self.class.tenant_options[:optional] 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 [: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 } end |
#validates_with(*args, &block) ⇒ Object
Redefine ‘validates_with’ to add the tenant scope when using a UniquenessValidator
47 48 49 50 51 52 |
# File 'lib/mongoid/multitenancy/document.rb', line 47 def validates_with(*args, &block) if args.first == Validations::UniquenessValidator args.last[:scope] = Array(args.last[:scope]) << self.tenant_field end super(*args, &block) end |