Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (33 sloc) 1.14 KB
module Devise
module Orm
module DataMapper
module ActiveModelCompatibility
# include ActiveModel::Validations does not make save check valid?.
# This may not be the best solution, but it seems to work. Note that
# Compatibility is included after this module; its #save method handles
# the :validate => false option.
def save(*args)
retval = valid? && super(*args)
assert_save_successful(:save, retval)
retval
end
end
module ActiveModelUniquenessCompatibility
def validates_uniqueness_of(*fields)
validates_with UniquenessValidator, _merge_attributes(fields)
end
end
class UniquenessValidator < ActiveModel::EachValidator
def validate_each(target, attribute, value)
resource = ::DataMapper.repository(target.repository.name) { target.model.first(attribute => value) }
if resource.nil? || (target.saved? && resource.key == target.key)
return true
else
target.errors.add(attribute, :taken)
return false
end
end
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.