aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/new_callbacks_test.rb
blob: e0edf262fc6c89a1a81e6c4a268dfed00fa879d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# require 'abstract_unit'
require 'test/unit'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'

class Record
  include ActiveSupport::Callbacks
  define_callbacks :save
end

class AroundPerson < Record
  attr_reader :history
  
  save_callback :before, :nope,           :if =>     :no
  save_callback :before, :nope,           :unless => :yes
  save_callback :after,  :tweedle
  save_callback :before, "tweedle_dee"
  save_callback :before, proc {|m| m.history << "yup" }
  save_callback :before, :nope,           :if =>     proc { false }
  save_callback :before, :nope,           :unless => proc { true }
  save_callback :before, :yup,            :if =>     proc { true }
  save_callback :before, :yup,            :unless => proc { false }
  save_callback :around, :tweedle_dum
  save_callback :around, :w0tyes,         :if =>     :yes
  save_callback :around, :w0tno,          :if =>     :no
  save_callback :around, :tweedle_deedle
  
  def no; false; end
  def yes; true; end
  
  def nope
    @history << "boom"
  end
  
  def yup
    @history << "yup"
  end
  
  def w0tyes
    @history << "w0tyes before"
    yield
    @history << "w0tyes after"
  end
  
  def w0tno
    @history << "boom"
    yield
  end
  
  def tweedle_dee
    @history << "tweedle dee"
  end
  
  def tweedle_dum
    @history << "tweedle dum pre"
    yield
    @history << "tweedle dum post"
  end
  
  def tweedle
    @history << "tweedle"
  end
  
  def tweedle_deedle
    @history << "tweedle deedle pre"
    yield
    @history << "tweedle deedle post"
  end
  
  def initialize
    @history = []
  end
  
  def save
    _run_save_callbacks do
      @history << "running"
    end
  end
end

class Foo
  include ActiveSupport::Callbacks
  define_callbacks :save
end

class Bar < Foo
  save_callback(:before) {|s| puts "Before" }
end

class Baz < Bar
  save_callback(:after) {|s| puts "After"}
end

class Bat < Baz
  def inside
    _run_save_callbacks do
      puts "Inside"
    end
  end
end

Bat.new.inside

# class AroundCallbacksTest < Test::Unit::TestCase
#   def test_save_around
#     around = AroundPerson.new
#     around.save
#     assert_equal [
#       "tweedle dee",
#       "yup", "yup", "yup",
#       "tweedle dum pre",
#       "w0tyes before",
#       "tweedle deedle pre",
#       "running",
#       "tweedle deedle post",
#       "w0tyes after",
#       "tweedle dum post",
#       "tweedle"
#     ], around.history
#   end
# end