Sha256: 29e945ae25c2e17d6a12ae5ca2810de9c605f14988bbeb8ab5c8420ef01d17cb
Contents?: true
Size: 1.93 KB
Versions: 1
Compression:
Stored size: 1.93 KB
Contents
module DataMapper module Validate ## # # @author Guy van den Berg # @since 0.9 class UniquenessValidator < GenericValidator include Assertions def initialize(field_name, options = {}) assert_kind_of 'scope', options[:scope], Array, Symbol if options.has_key?(:scope) super @field_name, @options = field_name, options end def call(target) scope = Array(@options[:scope]) return true if @options[:allow_nil] && target.send(field_name).nil? repository_name = target.repository.name opts = { field_name => target.validation_property_value(field_name) } scope.each do |item| if !target.model.properties(repository_name)[item].nil? opts[item] = target.validation_property_value(item) elsif target.model.relationships(repository_name).has_key?(item) target.validation_association_keys(item).each do |key| opts[key] = target.validation_property_value(key) end end end resource = repository(repository_name) { target.model.first(opts) } return true if resource.nil? # is target and found resource identic? same instance... but not == return true if resource.repository.name == repository_name && resource.model == target.model && resource.key == target.key error_message = @options[:message] || "%s is already taken".t(Extlib::Inflection.humanize(field_name)) add_error(target, error_message , field_name) return false end end # class UniquenessValidator module ValidatesIsUnique # Validate the uniqueness of a field # def validates_is_unique(*fields) opts = opts_from_validator_args(fields) add_validator_to_context(opts, fields, DataMapper::Validate::UniquenessValidator) end end # module ValidatesIsUnique end # module Validate end # module DataMapper
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dm-validations-0.9.2 | lib/dm-validations/uniqueness_validator.rb |