Skip to content

Commit

Permalink
after_validation should be called irrespective of the result of valid…
Browse files Browse the repository at this point in the history
…ation.

I confirmed that this is the behavior on 2.3.x .

[5419 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Aug 20, 2010
1 parent 0d0fbf1 commit 2ffa50f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/callbacks.rb
Expand Up @@ -40,7 +40,7 @@ def after_validation(*args, &block)
options = args.extract_options!
options[:prepend] = true
options[:if] = Array.wrap(options[:if])
options[:if] << "!halted && value != false"
options[:if] << "!halted"
options[:if] << "self.validation_context == :#{options[:on]}" if options[:on]
set_callback(:validation, :after, *(args << options), &block)
end
Expand Down
2 changes: 2 additions & 0 deletions activemodel/test/cases/validations_test.rb
Expand Up @@ -25,9 +25,11 @@ def test_single_field_validation
r = Reply.new
r.title = "There's no content!"
assert r.invalid?, "A reply without content shouldn't be saveable"
assert r.after_validation_performed, "after_validation callback should be called"

r.content = "Messa content!"
assert r.valid?, "A reply with content should be saveable"
assert r.after_validation_performed, "after_validation callback should be called"
end

def test_single_attr_validation_and_error_msg
Expand Down
9 changes: 9 additions & 0 deletions activemodel/test/models/topic.rb
@@ -1,7 +1,11 @@
class Topic
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks

attr_accessor :title, :author_name, :content, :approved
attr_accessor :after_validation_performed

after_validation :perform_after_validation

def initialize(attributes = {})
attributes.each do |key, value|
Expand All @@ -16,4 +20,9 @@ def condition_is_true
def condition_is_true_but_its_not
false
end

def perform_after_validation
self.after_validation_performed = true
end

end

0 comments on commit 2ffa50f

Please sign in to comment.