class ROM::Model::Validator::UniquenessValidator
Uniqueness validation
@api public
Attributes
klass[R]
Relation validator class
@api private
message[R]
error message
@return [String, Symbol]
@api private
Public Class Methods
new(options)
click to toggle source
@api private
Calls superclass method
# File lib/rom/model/validator/uniqueness_validator.rb, line 23 def initialize(options) super @klass = options.fetch(:class) @message = options.fetch(:message) { :taken } @scope_keys = options[:scope] end
Public Instance Methods
validate_each(validator, name, value)
click to toggle source
Hook called by ActiveModel internally
@api private
# File lib/rom/model/validator/uniqueness_validator.rb, line 33 def validate_each(validator, name, value) scope = Array(@scope_keys).each_with_object({}) do |key, scope| scope[key] = validator.to_model[key] end validator.errors.add(name, message) unless unique?(name, value, scope) end
Private Instance Methods
relation()
click to toggle source
Get relation object from the rom env
@api private
# File lib/rom/model/validator/uniqueness_validator.rb, line 45 def relation if relation_name rom.relations[relation_name] else raise "relation must be specified to use uniqueness validation" end end
relation_name()
click to toggle source
Relation name defined on the validator class
@api private
# File lib/rom/model/validator/uniqueness_validator.rb, line 56 def relation_name klass.relation end
rom()
click to toggle source
Shortcut to access global rom env
@return [ROM::Env]
@api private
# File lib/rom/model/validator/uniqueness_validator.rb, line 65 def rom ROM.env end
unique?(name, value, scope)
click to toggle source
Ask relation if a given attribute value is unique
This uses `Relation#unique?` interface that not all adapters can implement.
@return [TrueClass,FalseClass]
@api private
# File lib/rom/model/validator/uniqueness_validator.rb, line 77 def unique?(name, value, scope) relation.unique?({name => value}.merge(scope)) end