Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 92 lines (74 sloc) 2.803 kb
ed6ed65 John Nunemaker Ruby 1.9 encoding comments.
jnunemaker authored
1 # encoding: UTF-8
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
2 module MongoMapper
3 module Plugins
4 module Validations
7c69e08 Brandon Keepers Deprecate plugin structure in favor of using ActiveSupport::Concern
bkeepers authored
5 extend ActiveSupport::Concern
6
288f432 Brandon Keepers ActiveSupport::Concern tweaks
bkeepers authored
7 include ::ActiveModel::Validations
48284d8 Brandon Keepers Move validation code into the Validation plugin
bkeepers authored
8 include ::ActiveModel::Validations::Callbacks
527ae95 Brian Ryckbost Fixing on create and on update validation callbacks.
bryckbost authored
9
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
10 module ClassMethods
11 def validates_uniqueness_of(*attr_names)
12 validates_with UniquenessValidator, _merge_attributes(attr_names)
13 end
e9d2bf5 Brandon Keepers Merge branch 'master' into rails3
bkeepers authored
14
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
15 def validates_associated(*attr_names)
16 validates_with AssociatedValidator, _merge_attributes(attr_names)
17 end
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
18 end
2bc2277 John Nunemaker The great white white-space zapper!
jnunemaker authored
19
d2333d9 Steve Richert ActiveSupport::Concern's automatic inclusion of the InstanceMethods modu...
laserlemon authored
20 def save(options = {})
016fa6e Nicolas Viennot reverse_merge! -> reverse_merge
nviennot authored
21 options = options.reverse_merge(:validate => true)
d2333d9 Steve Richert ActiveSupport::Concern's automatic inclusion of the InstanceMethods modu...
laserlemon authored
22 !options[:validate] || valid? ? super : false
23 end
48284d8 Brandon Keepers Move validation code into the Validation plugin
bkeepers authored
24
d2333d9 Steve Richert ActiveSupport::Concern's automatic inclusion of the InstanceMethods modu...
laserlemon authored
25 def valid?(context = nil)
26 context ||= (new_record? ? :create : :update)
27 super(context)
48284d8 Brandon Keepers Move validation code into the Validation plugin
bkeepers authored
28 end
29
12c1797 Brian Ryckbost Extract ActiveModel includes to a plugin
bryckbost authored
30 class UniquenessValidator < ::ActiveModel::EachValidator
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
31 def initialize(options)
4d5bb9e Frederick Cheung validatior#setup is deprecated in activemodel 4.1
fcheung authored
32 @klass = options[:class] #only provided in ActiveModel 4.1 and higher
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
33 super(options.reverse_merge(:case_sensitive => true))
34 end
35
4d5bb9e Frederick Cheung validatior#setup is deprecated in activemodel 4.1
fcheung authored
36 if ::ActiveModel::VERSION::MAJOR < 4 ||
518738b Chris Heald Remove trailing whitespace
cheald authored
37 (::ActiveModel::VERSION::MAJOR == 4 && ::ActiveModel::VERSION::MINOR == 0)
4d5bb9e Frederick Cheung validatior#setup is deprecated in activemodel 4.1
fcheung authored
38
39 def setup(klass)
40 @klass = klass
41 end
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
42 end
e9d2bf5 Brandon Keepers Merge branch 'master' into rails3
bkeepers authored
43
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
44 def validate_each(record, attribute, value)
45 conditions = scope_conditions(record)
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
46
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
47 if options[:case_sensitive]
48 conditions[attribute] = value
49 else
50 conditions[attribute] = /^#{Regexp.escape(value.to_s)}$/i
51 end
52
53 # Make sure we're not including the current document in the query
54 conditions[:_id.ne] = record._id if record._id
e9d2bf5 Brandon Keepers Merge branch 'master' into rails3
bkeepers authored
55
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
56 if @klass.exists?(conditions)
8a50bec Jonathan Hoyt Added I18n support for uniqueness validation.
jonmagic authored
57 record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
58 end
59 end
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
60
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
61 def message(instance)
62 super || "has already been taken"
63 end
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
64
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
65 def scope_conditions(instance)
66 Array(options[:scope] || []).inject({}) do |conditions, key|
67 conditions.merge(key => instance[key])
68 end
69 end
70 end
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
71
12c1797 Brian Ryckbost Extract ActiveModel includes to a plugin
bryckbost authored
72 class AssociatedValidator < ::ActiveModel::EachValidator
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
73 def validate_each(record, attribute, value)
d6b10f9 Jason Staten added :context option to validates_associated
statianzo authored
74 if !Array.wrap(value).all? { |c| c.nil? || c.valid?(options[:context]) }
d46ed59 avoid deprecation warning from ActiveModel::Errors#generate_message
Jacob Maine & Travis Bell authored
75 record.errors.add(attribute, :invalid, :message => options[:message], :value => value)
67a44ad Brian Ryckbost Begin refactoring of callbacks for ActiveModel support
bryckbost authored
76 end
77 end
78 end
e9d2bf5 Brandon Keepers Merge branch 'master' into rails3
bkeepers authored
79
5d5590f John Nunemaker Moved validations to plugin and only adding validates uniqueness of to d...
jnunemaker authored
80 end
81 end
67583fd Ryan Angilly new documents should not validate on uniquness if allow_blank || allow_n...
ryana authored
82 end
48284d8 Brandon Keepers Move validation code into the Validation plugin
bkeepers authored
83
84 # Need to monkey patch ActiveModel for now since it uses the internal
85 # _run_validation_callbacks, which is impossible to override due to the
86 # way ActiveSupport::Callbacks is implemented.
87 ActiveModel::Validations::Callbacks.class_eval do
88 def run_validations!
89 run_callbacks(:validation) { super }
90 end
91 end
Something went wrong with that request. Please try again.