Skip to content

Commit

Permalink
Add unit test to demonstrate issue with similar callback methods over…
Browse files Browse the repository at this point in the history
…riding each other in distinct contexts
  • Loading branch information
Michael Andrews committed Apr 14, 2011
1 parent 38df020 commit d683be0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions activerecord/test/cases/callbacks_test.rb
Expand Up @@ -138,6 +138,34 @@ def history
end
end

class ContextualCallbacksDeveloper < ActiveRecord::Base
set_table_name 'developers'

before_validation { history << :before_validation }
before_validation :before_validation_on_create_and_update, :on => :create
before_validation :before_validation_on_create_and_update, :on => :update

validate do
history << :validate
end

after_validation { history << :after_validation }
after_validation :after_validation_on_create_and_update, :on => :create
after_validation :after_validation_on_create_and_update, :on => :update

def before_validation_on_create_and_update
history << "before_validation_on_#{self.validation_context}".to_sym
end

def after_validation_on_create_and_update
history << "after_validation_on_#{self.validation_context}".to_sym
end

def history
@history ||= []
end
end

class CallbackCancellationDeveloper < ActiveRecord::Base
set_table_name 'developers'

Expand Down Expand Up @@ -286,6 +314,17 @@ def test_validate_on_create
], david.history
end

def test_validate_on_contextual_create
david = ContextualCallbacksDeveloper.create('name' => 'David', 'salary' => 1000000)
assert_equal [
:before_validation,
:before_validation_on_create,
:validate,
:after_validation,
:after_validation_on_create
], david.history
end

def test_update
david = CallbackDeveloper.find(1)
david.save
Expand Down Expand Up @@ -345,6 +384,18 @@ def test_validate_on_update
], david.history
end

def test_validate_on_contextual_update
david = ContextualCallbacksDeveloper.find(1)
david.save
assert_equal [
:before_validation,
:before_validation_on_update,
:validate,
:after_validation,
:after_validation_on_update
], david.history
end

def test_destroy
david = CallbackDeveloper.find(1)
david.destroy
Expand Down

0 comments on commit d683be0

Please sign in to comment.