Module: Mongoid::Multitenancy::Document::ClassMethods
- Defined in:
- lib/mongoid/multitenancy/document.rb
Constant Summary collapse
- MULTITENANCY_OPTIONS =
List of authorized options
[:optional, :immutable, :full_indexes, :index, :scopes].freeze
Instance Attribute Summary collapse
-
#tenant_field ⇒ Object
Returns the value of attribute tenant_field.
-
#tenant_options ⇒ Object
Returns the value of attribute tenant_options.
Instance Method Summary collapse
-
#delete_all(conditions = {}) ⇒ 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 = {}) ⇒ Field
Defines the tenant field for the document.
-
#validates_tenancy_of(*args) ⇒ Object
Validates whether or not a tenant field is correct.
-
#validates_tenant_uniqueness_of(*args) ⇒ Object
Validates whether or not a field is unique against the documents in the database.
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 |
#tenant_options ⇒ Object
Returns the value of attribute tenant_options.
7 8 9 |
# File 'lib/mongoid/multitenancy/document.rb', line 7 def @tenant_options end |
Instance Method Details
#delete_all(conditions = {}) ⇒ Object
Redefine ‘delete_all’ to take in account the default scope
100 101 102 |
# File 'lib/mongoid/multitenancy/document.rb', line 100 def delete_all(conditions = {}) scoped.where(conditions).delete end |
#index(spec, options = nil) ⇒ Object
Redefine ‘index’ to include the tenant field in first position
89 90 91 92 93 94 95 96 97 |
# File 'lib/mongoid/multitenancy/document.rb', line 89 def index(spec, = nil) = ( || {}).dup full_index = .delete(:full_index) if full_index.nil? ? [:full_indexes] : full_index spec = { tenant_field => 1 }.merge(spec) end super(spec, ) end |
#tenant(association = :account, options = {}) ⇒ Field
Defines the tenant field for the document.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/multitenancy/document.rb', line 32 def tenant(association = :account, = {}) = { full_indexes: true, immutable: true, scopes: true }.merge!() , = () # Setup the association between the class and the tenant class belongs_to association, # Get the tenant model and its foreign key self.tenant_field = reflect_on_association(association).foreign_key.to_sym self. = # Validates the tenant field validates_tenancy_of tenant_field, define_scopes if [:scopes] define_initializer association define_inherited association, define_index if [:index] end |
#validates_tenancy_of(*args) ⇒ Object
Validates whether or not a tenant field is correct.
84 85 86 |
# File 'lib/mongoid/multitenancy/document.rb', line 84 def validates_tenancy_of(*args) validates_with(TenancyValidator, _merge_attributes(args)) end |
#validates_tenant_uniqueness_of(*args) ⇒ Object
Validates whether or not a field is unique against the documents in the database.
66 67 68 |
# File 'lib/mongoid/multitenancy/document.rb', line 66 def validates_tenant_uniqueness_of(*args) validates_with(TenantUniquenessValidator, _merge_attributes(args)) end |