123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Defined in: lib/mongoid/fields/validators/macro.rb

Overview

Validates the params passed to the field macro.

Constant Summary

Instance Method Summary

Instance Method Details

#validate(klass, name, options)

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.

[ GitHub ]

  
# File 'lib/mongoid/fields/validators/macro.rb', line 35

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)

This method is for internal use only.

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:

[ GitHub ]

  
# File 'lib/mongoid/fields/validators/macro.rb', line 68

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

#validate_name_uniqueness(klass, name, options) (private)

This method is for internal use only.

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

Examples:

Check the field name.

Macro.validate_name_uniqueness(Model, :name, {})

Parameters:

  • klass (Class)

    The model class.

  • name (Symbol)

    The field name.

  • options (Hash)

    The provided options.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/fields/validators/macro.rb', line 90

def validate_name_uniqueness(klass, name, options)
  if !options[:overwrite] && klass.fields.keys.include?(name.to_s)
    if Mongoid.duplicate_fields_exception
      raise Errors::InvalidField.new(klass, name, name)
    else
      Mongoid.logger.warn("Overwriting existing field #{name} in class #{klass.name}.") if Mongoid.logger
    end
  end
end

#validate_options(klass, name, options) (private)

This method is for internal use only.

Validate that the field options are allowed.

Examples:

Validate the field options.

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

Parameters:

  • klass (Class)

    The model class.

  • name (Symbol)

    The field name.

  • options (Hash)

    The provided options.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/fields/validators/macro.rb', line 112

def validate_options(klass, name, options)
  options.keys.each do |option|
    if !OPTIONS.include?(option) && !Fields.options.include?(option)
      raise Errors::InvalidFieldOption.new(klass, name, option, OPTIONS)
    end

    if option == :type && options[option] == Symbol
      Mongoid::Warnings.warn_symbol_type_deprecated
    end
  end
end

#validate_relation(klass, name, options = {})

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) (defaults to: {})

    The provided options.

[ GitHub ]

  
# File 'lib/mongoid/fields/validators/macro.rb', line 49

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