forked from mongomapper/mongomapper
/
validations.rb
88 lines (72 loc) · 2.54 KB
/
validations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# encoding: UTF-8
module MongoMapper
module Plugins
module Validations
extend ActiveSupport::Concern
include ::ActiveModel::Validations
include ::ActiveModel::Validations::Callbacks
module ClassMethods
def validates_uniqueness_of(*attr_names)
validates_with UniquenessValidator, _merge_attributes(attr_names)
end
def validates_associated(*attr_names)
validates_with AssociatedValidator, _merge_attributes(attr_names)
end
end
module InstanceMethods
def save(options = {})
options.reverse_merge!(:validate => true)
!options[:validate] || valid? ? super : false
end
def valid?(context = nil)
context ||= (new_record? ? :create : :update)
super(context)
end
end
class UniquenessValidator < ::ActiveModel::EachValidator
def initialize(options)
super(options.reverse_merge(:case_sensitive => true))
end
def setup(klass)
@klass = klass
end
def validate_each(record, attribute, value)
conditions = scope_conditions(record)
if options[:case_sensitive]
conditions[attribute] = value
else
conditions[attribute] = /^#{Regexp.escape(value.to_s)}$/i
end
# Make sure we're not including the current document in the query
conditions[:_id.ne] = record._id if record._id
if @klass.exists?(conditions)
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
end
def message(instance)
super || "has already been taken"
end
def scope_conditions(instance)
Array(options[:scope] || []).inject({}) do |conditions, key|
conditions.merge(key => instance[key])
end
end
end
class AssociatedValidator < ::ActiveModel::EachValidator
def validate_each(record, attribute, value)
if !Array.wrap(value).all? { |c| c.nil? || c.valid? }
record.errors.add(attribute, :invalid, :message => options[:message], :value => value)
end
end
end
end
end
end
# Need to monkey patch ActiveModel for now since it uses the internal
# _run_validation_callbacks, which is impossible to override due to the
# way ActiveSupport::Callbacks is implemented.
ActiveModel::Validations::Callbacks.class_eval do
def run_validations!
run_callbacks(:validation) { super }
end
end