diff options
author | Matthew Draper <matthew@trebex.net> | 2016-12-16 23:14:46 +1030 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-16 23:14:46 +1030 |
commit | a47efcfcaee01c1143de459d3864f2c35fb566d4 (patch) | |
tree | 5d81a2a630f9e36a21ffe771a9c540d8e3278ddb /activesupport/test | |
parent | b89f1aa7601fb6e8f24f33ed1cbdb590023c5282 (diff) | |
parent | 833ea903a9591b3b7d805981c0d0dbadf53cb58d (diff) | |
download | rails-a47efcfcaee01c1143de459d3864f2c35fb566d4.tar.gz rails-a47efcfcaee01c1143de459d3864f2c35fb566d4.tar.bz2 rails-a47efcfcaee01c1143de459d3864f2c35fb566d4.zip |
Merge pull request #27368 from matthewd/doubled-callbacks
Support double-yield inside an around callback
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/callbacks_test.rb | 83 |
1 files changed, 68 insertions, 15 deletions
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index aadc40ab84..22f66978f8 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -224,10 +224,51 @@ module CallbacksTest define_callbacks :save end - class AroundPerson < MySuper + class MySlate < MySuper attr_reader :history attr_accessor :save_fails + def initialize + @history = [] + end + + def save + run_callbacks :save do + raise "inside save" if save_fails + @history << "running" + end + end + + def no; false; end + def yes; true; end + + def method_missing(sym, *) + case sym + when /^log_(.*)/ + @history << $1 + nil + when /^wrap_(.*)/ + @history << "wrap_#$1" + yield + @history << "unwrap_#$1" + nil + when /^double_(.*)/ + @history << "first_#$1" + yield + @history << "second_#$1" + yield + @history << "third_#$1" + else + super + end + end + + def respond_to_missing?(sym) + sym =~ /^(log|wrap)_/ || super + end + end + + class AroundPerson < MySlate set_callback :save, :before, :nope, if: :no set_callback :save, :before, :nope, unless: :yes set_callback :save, :after, :tweedle @@ -242,9 +283,6 @@ module CallbacksTest set_callback :save, :around, :w0tno, if: :no set_callback :save, :around, :tweedle_deedle - def no; false; end - def yes; true; end - def nope @history << "boom" end @@ -283,17 +321,6 @@ module CallbacksTest yield @history << "tweedle deedle post" end - - def initialize - @history = [] - end - - def save - run_callbacks :save do - raise "inside save" if save_fails - @history << "running" - end - end end class AroundPersonResult < MySuper @@ -408,6 +435,32 @@ module CallbacksTest end end + class DoubleYieldTest < ActiveSupport::TestCase + class DoubleYieldModel < MySlate + set_callback :save, :around, :wrap_outer + set_callback :save, :around, :double_trouble + set_callback :save, :around, :wrap_inner + end + + def test_double_save + double = DoubleYieldModel.new + double.save + assert_equal [ + "wrap_outer", + "first_trouble", + "wrap_inner", + "running", + "unwrap_inner", + "second_trouble", + "wrap_inner", + "running", + "unwrap_inner", + "third_trouble", + "unwrap_outer", + ], double.history + end + end + class CallStackTest < ActiveSupport::TestCase def test_tidy_call_stack around = AroundPerson.new |