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

Instance Method Summary collapse

Instance Attribute Details

#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

#tenant_optionsObject

Returns the value of attribute tenant_options.



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

def tenant_options
  @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, options = nil)
  super_options = (options || {}).dup
  full_index = super_options.delete(:full_index)
  if full_index.nil? ? tenant_options[:full_indexes] : full_index
    spec = { tenant_field => 1 }.merge(spec)
  end

  super(spec, super_options)
end

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

Defines the tenant field for the document.

Examples:

Define a tenant.

tenant :client, optional: false, immutable: true, full_indexes: true

Parameters:

  • name (Symbol)

    The name of the relation.

  • options (Hash) (defaults to: {})

    The relation options. All the belongs_to options are allowed plus the following ones:

Options Hash (options):

  • :full_indexes (Boolean)

    If true the tenant field will be added for each index.

  • :immutable (Boolean)

    If true changing the tenant wil raise an Exception.

  • :optional (Boolean)

    If true allow the document to be shared among all the tenants.

  • :index (Boolean)

    If true build an index for the tenant field itself.

  • :scopes (Boolean)

    If true create scopes :shared and :unshared.

Returns:

  • (Field)

    The generated field



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, options = {})
  options = { full_indexes: true, immutable: true, scopes: true }.merge!(options)
  assoc_options, multitenant_options = build_options(options)

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

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

  # Validates the tenant field
  validates_tenancy_of tenant_field, multitenant_options

  define_scopes if multitenant_options[:scopes]
  define_initializer association
  define_inherited association, options
  define_index if multitenant_options[:index]
end

#validates_tenancy_of(*args) ⇒ Object

Validates whether or not a tenant field is correct.

Examples:

Define the tenant validator


class Person
  include Mongoid::Document
  include Mongoid::Multitenancy::Document
  field :title
  tenant :client

  validates_tenant_of :client
end

Parameters:

  • *args (Array)

    The arguments to pass to the validator.



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.

Examples:


class Person
  include Mongoid::Document
  include Mongoid::Multitenancy::Document
  field :title

  validates_tenant_uniqueness_of :title
end

Parameters:

  • *args (Array)

    The arguments to pass to the validator.



66
67
68
# File 'lib/mongoid/multitenancy/document.rb', line 66

def validates_tenant_uniqueness_of(*args)
  validates_with(TenantUniquenessValidator, _merge_attributes(args))
end