File tree Expand file tree Collapse file tree 5 files changed +38
-0
lines changed Expand file tree Collapse file tree 5 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -285,6 +285,8 @@ def errors
285
285
# Runs all the specified validations and returns +true+ if no errors were
286
286
# added otherwise +false+.
287
287
#
288
+ # Aliased as validate.
289
+ #
288
290
# class Person
289
291
# include ActiveModel::Validations
290
292
#
@@ -319,6 +321,8 @@ def valid?(context = nil)
319
321
self . validation_context = current_context
320
322
end
321
323
324
+ alias_method :validate , :valid?
325
+
322
326
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
323
327
# added, +false+ otherwise.
324
328
#
Original file line number Diff line number Diff line change @@ -295,6 +295,15 @@ def test_validations_on_the_instance_level
295
295
assert auto . valid?
296
296
end
297
297
298
+ def test_validate
299
+ auto = Automobile . new
300
+
301
+ assert_empty auto . errors
302
+
303
+ auto . validate
304
+ assert_not_empty auto . errors
305
+ end
306
+
298
307
def test_strict_validation_in_validates
299
308
Topic . validates :title , strict : true , presence : true
300
309
assert_raises ActiveModel ::StrictValidationFailed do
Original file line number Diff line number Diff line change
1
+ * Introduce ` validate ` as an alias for ` valid? ` .
2
+
3
+ This is more intuitive when you want to run validations but don't care about the return value.
4
+
5
+ * Henrik Nyh*
6
+
1
7
* Create indexes inline in CREATE TABLE for MySQL.
2
8
3
9
This is important, because adding an index on a temporary table after it has been created
Original file line number Diff line number Diff line change @@ -60,6 +60,8 @@ def save!(options={})
60
60
# Runs all the validations within the specified context. Returns +true+ if
61
61
# no errors are found, +false+ otherwise.
62
62
#
63
+ # Aliased as validate.
64
+ #
63
65
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
64
66
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
65
67
#
@@ -71,6 +73,8 @@ def valid?(context = nil)
71
73
errors . empty? && output
72
74
end
73
75
76
+ alias_method :validate , :valid?
77
+
74
78
protected
75
79
76
80
def perform_validations ( options = { } ) # :nodoc:
Original file line number Diff line number Diff line change @@ -52,6 +52,21 @@ def test_error_on_given_context
52
52
assert r . save ( :context => :special_case )
53
53
end
54
54
55
+ def test_validate
56
+ r = WrongReply . new
57
+
58
+ r . validate
59
+ assert_empty r . errors [ :author_name ]
60
+
61
+ r . validate ( :special_case )
62
+ assert_not_empty r . errors [ :author_name ]
63
+
64
+ r . author_name = "secret"
65
+
66
+ r . validate ( :special_case )
67
+ assert_empty r . errors [ :author_name ]
68
+ end
69
+
55
70
def test_invalid_record_exception
56
71
assert_raise ( ActiveRecord ::RecordInvalid ) { WrongReply . create! }
57
72
assert_raise ( ActiveRecord ::RecordInvalid ) { WrongReply . new . save! }
You can’t perform that action at this time.
0 commit comments