Module: Mongoid::Fields::Validators::Macro

Extended by:
Macro
Included in:
Macro
Defined in:
lib/mongoid/fields/validators/macro.rb

Overview

Validates the params passed to the field macro.

Constant Summary collapse

OPTIONS =
%i[
  as
  default
  identity
  label
  localize
  fallbacks
  association
  pre_processed
  subtype
  type
  overwrite
  encrypt
]

Instance Method Summary collapse

Instance Method Details

#validate(klass, name, options) ⇒ Object

Validate the field definition.

Examples:

Validate the field definition.

Macro.validate(Model, :name, { localized: true })

Parameters:

  • klass (Class)

    The model class.

  • name (Symbol)

    The field name.

  • options (Hash)

    The provided options.



33
34
35
36
37
# File 'lib/mongoid/fields/validators/macro.rb', line 33

def validate(klass, name, options)
  validate_field_name(klass, name)
  validate_name_uniqueness(klass, name, options)
  validate_options(klass, name, options)
end

#validate_field_name(klass, name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determine if the field name is valid, if not raise an error.

Examples:

Check the field name.

Macro.validate_field_name(Model, :name)

Parameters:

  • klass (Class)

    The model class.

  • name (Symbol)

    The field name.

Raises:



64
65
66
67
68
# File 'lib/mongoid/fields/validators/macro.rb', line 64

def validate_field_name(klass, name)
  [ name, :"#{name}?", :"#{name}=" ].each do |n|
    raise Errors::InvalidField.new(klass, name, n) if Mongoid.destructive_fields.include?(n)
  end
end

#validate_relation(klass, name, _options = {}) ⇒ Object

Validate the association definition.

Examples:

Validate the association definition.

Macro.validate(Model, :name)

Parameters:

  • klass (Class)

    The model class.

  • name (Symbol)

    The field name.

  • options (Hash)

    The provided options.



47
48
49
50
51
# File 'lib/mongoid/fields/validators/macro.rb', line 47

def validate_relation(klass, name, _options = {})
  [ name, :"#{name}?", :"#{name}=" ].each do |n|
    raise Errors::InvalidRelation.new(klass, n) if Mongoid.destructive_fields.include?(n)
  end
end