Class: TenantValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mongoid/validators/tenant_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mongoid/validators/tenant_validator.rb', line 2

def validate_each(object, attribute, value)
  # Immutable Check
  if options[:immutable]
    if object.send(:attribute_changed?, attribute) and object.send(:attribute_was, attribute)
      object.errors.add(attribute, 'is immutable and cannot be updated')
    end
  end

  # Ownership check
  if value and Mongoid::Multitenancy.current_tenant and value != Mongoid::Multitenancy.current_tenant.id
    object.errors.add(attribute, "not authorized")
  end

  # Optional Check
  if !options[:optional] and value.nil?
    object.errors.add(attribute, 'is mandatory')
  end
end