Skip to content

Commit

Permalink
Added failing test for adding multiple save/validate callbacks for th…
Browse files Browse the repository at this point in the history
…e same method. This shouldn't happen at all so should maybe be fixed in callbacks.rb
  • Loading branch information
alloy committed Feb 20, 2009
1 parent 7dba46e commit 587237b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -157,6 +157,10 @@ def #{type}(name, options = {})
def add_autosave_association_callbacks(reflection)
save_method = "autosave_associated_records_for_#{reflection.name}"
validation_method = "validate_associated_records_for_#{reflection.name}"

# TODO: Actually there's no point at all in allowing multiple callbacks for the same method
# in both save and validation callbacks. So this should maybe be fixed in callbacks.rb
# @validate_callbacks.try(:delete_if) { |callback| callback.method == validation_method }
validate validation_method

case reflection.macro
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -33,6 +33,16 @@ def test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
assert base.valid_keys_for_has_and_belongs_to_many_association.include?(:autosave)
end

# def test_autosave_callbacks_should_not_be_added_multiple_times
# valdation_callbacks = Pirate.instance_variable_get(:@validate_callbacks)
# valdations = "valdation_callbacks.select { |c| c.method == 'validate_associated_records_for_birds' }.length"
#
# assert_no_difference(valdations) do
# reflection = Pirate.reflect_on_association(:birds)
# Pirate.send(:add_autosave_association_callbacks, reflection)
# end
# end

private

def base
Expand Down

0 comments on commit 587237b

Please sign in to comment.