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
-
OPTIONS =
# File 'lib/mongoid/fields/validators/macro.rb', line 12[ :as, :default, :identity, :label, :localize, :fallbacks, :association, :pre_processed, :subtype, :type, :overwrite, :encrypt ]
Instance Method Summary
-
#validate(klass, name, options)
Validate the field definition.
-
#validate_field_name(klass, name)
Internal use only
Internal use only
Determine if the field name is valid, if not raise an error.
-
#validate_relation(klass, name, options = {})
Validate the association definition.
-
#validate_name_uniqueness(klass, name, options)
private
Internal use only
Internal use only
Determine if the field name is unique, if not raise an error.
-
#validate_options(klass, name, options)
private
Internal use only
Internal use only
Validate that the field options are allowed.
Instance Method Details
#validate(klass, name, options)
Validate the field definition.
# File 'lib/mongoid/fields/validators/macro.rb', line 35
def validate(klass, name, ) validate_field_name(klass, name) validate_name_uniqueness(klass, name, ) (klass, name, ) 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.
# 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.
# File 'lib/mongoid/fields/validators/macro.rb', line 90
def validate_name_uniqueness(klass, name, ) if ! [: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.
# File 'lib/mongoid/fields/validators/macro.rb', line 112
def (klass, name, ) .keys.each do |option| if !OPTIONS.include?(option) && !Fields. .include?(option) raise Errors::InvalidFieldOption.new(klass, name, option, OPTIONS) end if option == :type && [option] == Symbol Mongoid::Warnings.warn_symbol_type_deprecated end end end
#validate_relation(klass, name, options = {})
Validate the association definition.
# File 'lib/mongoid/fields/validators/macro.rb', line 49
def validate_relation(klass, name, = {}) [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