ohm-contrib 0.0.33 → 0.0.34
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/callbacks.rb +14 -2
- data/test/callbacks_lint.rb +37 -1
- data/test/instance_callbacks_test.rb +2 -0
- data/test/macro_callbacks_test.rb +5 -0
- metadata +2 -2
data/lib/ohm/contrib.rb
CHANGED
@@ -10,6 +10,8 @@ module Ohm
|
|
10
10
|
# - after_create
|
11
11
|
# - before_save
|
12
12
|
# - after_save
|
13
|
+
# - before_update
|
14
|
+
# - after_update
|
13
15
|
# - before_delete
|
14
16
|
# - after_delete
|
15
17
|
#
|
@@ -176,10 +178,18 @@ module Ohm
|
|
176
178
|
# If the save also succeeds, all after :save callbacks are
|
177
179
|
# executed.
|
178
180
|
def save
|
179
|
-
|
181
|
+
existing = !new?
|
182
|
+
|
183
|
+
if valid?
|
184
|
+
execute_callback(:before, :save)
|
185
|
+
execute_callback(:before, :update) if existing
|
186
|
+
end
|
180
187
|
|
181
188
|
super.tap do |is_saved|
|
182
|
-
|
189
|
+
if is_saved
|
190
|
+
execute_callback(:after, :save)
|
191
|
+
execute_callback(:after, :update) if existing
|
192
|
+
end
|
183
193
|
end
|
184
194
|
end
|
185
195
|
|
@@ -198,6 +208,8 @@ module Ohm
|
|
198
208
|
def after_save() end
|
199
209
|
def before_create() end
|
200
210
|
def after_create() end
|
211
|
+
def before_update() end
|
212
|
+
def after_update() end
|
201
213
|
def before_delete() end
|
202
214
|
def after_delete() end
|
203
215
|
|
data/test/callbacks_lint.rb
CHANGED
@@ -9,6 +9,8 @@ test "save on invalid" do
|
|
9
9
|
assert ! post.did?(:do_after_create)
|
10
10
|
assert ! post.did?(:do_before_save)
|
11
11
|
assert ! post.did?(:do_after_save)
|
12
|
+
assert ! post.did?(:do_before_update)
|
13
|
+
assert ! post.did?(:do_after_update)
|
12
14
|
end
|
13
15
|
|
14
16
|
test "saving a valid model" do
|
@@ -21,6 +23,8 @@ test "saving a valid model" do
|
|
21
23
|
assert post.did?(:do_after_create)
|
22
24
|
assert post.did?(:do_before_save)
|
23
25
|
assert post.did?(:do_after_save)
|
26
|
+
assert ! post.did?(:do_before_update)
|
27
|
+
assert ! post.did?(:do_after_update)
|
24
28
|
end
|
25
29
|
|
26
30
|
test "ensure create / save callbacks are only called once" do
|
@@ -33,7 +37,7 @@ test "ensure create / save callbacks are only called once" do
|
|
33
37
|
assert 1 == post.count(:do_after_create)
|
34
38
|
end
|
35
39
|
|
36
|
-
test "
|
40
|
+
test "Post::create on a valid model invokes all callbacks (except update)" do
|
37
41
|
post = Post.create(:body => "The Body")
|
38
42
|
|
39
43
|
assert post.did?(:do_before_validate)
|
@@ -42,6 +46,8 @@ test "using Post::create on a valid model invokes all callbacks" do
|
|
42
46
|
assert post.did?(:do_after_create)
|
43
47
|
assert post.did?(:do_before_save)
|
44
48
|
assert post.did?(:do_after_save)
|
49
|
+
assert ! post.did?(:do_before_update)
|
50
|
+
assert ! post.did?(:do_after_update)
|
45
51
|
end
|
46
52
|
|
47
53
|
test "ensure Post::create only invokes save / create once" do
|
@@ -65,9 +71,34 @@ test "on successful save of existing record" do
|
|
65
71
|
assert post.did?(:do_after_validate)
|
66
72
|
assert post.did?(:do_before_save)
|
67
73
|
assert post.did?(:do_after_save)
|
74
|
+
assert post.did?(:do_before_update)
|
75
|
+
assert post.did?(:do_after_update)
|
68
76
|
|
69
77
|
assert 1 == post.count(:do_before_save)
|
70
78
|
assert 1 == post.count(:do_after_save)
|
79
|
+
assert 1 == post.count(:do_before_update)
|
80
|
+
assert 1 == post.count(:do_after_update)
|
81
|
+
end
|
82
|
+
|
83
|
+
test "on successful save of existing record (using #update)" do
|
84
|
+
post = Post.create(:body => "The Body")
|
85
|
+
post = Post[post.id]
|
86
|
+
post.update(:body => "New Body")
|
87
|
+
|
88
|
+
assert ! post.did?(:do_before_create)
|
89
|
+
assert ! post.did?(:do_after_create)
|
90
|
+
|
91
|
+
assert post.did?(:do_before_validate)
|
92
|
+
assert post.did?(:do_after_validate)
|
93
|
+
assert post.did?(:do_before_save)
|
94
|
+
assert post.did?(:do_after_save)
|
95
|
+
assert post.did?(:do_before_update)
|
96
|
+
assert post.did?(:do_after_update)
|
97
|
+
|
98
|
+
assert 1 == post.count(:do_before_save)
|
99
|
+
assert 1 == post.count(:do_after_save)
|
100
|
+
assert 1 == post.count(:do_before_update)
|
101
|
+
assert 1 == post.count(:do_after_update)
|
71
102
|
end
|
72
103
|
|
73
104
|
test "on failed save of existing record" do
|
@@ -85,6 +116,9 @@ test "on failed save of existing record" do
|
|
85
116
|
|
86
117
|
assert ! post.did?(:do_before_save)
|
87
118
|
assert ! post.did?(:do_after_save)
|
119
|
+
|
120
|
+
assert ! post.did?(:do_before_update)
|
121
|
+
assert ! post.did?(:do_after_update)
|
88
122
|
end
|
89
123
|
|
90
124
|
test "on delete" do
|
@@ -101,5 +135,7 @@ test "on delete" do
|
|
101
135
|
assert ! post.did?(:do_after_create)
|
102
136
|
assert ! post.did?(:do_before_save)
|
103
137
|
assert ! post.did?(:do_after_save)
|
138
|
+
assert ! post.did?(:do_before_update)
|
139
|
+
assert ! post.did?(:do_after_update)
|
104
140
|
end
|
105
141
|
|
@@ -28,6 +28,8 @@ protected
|
|
28
28
|
def after_create() incr(:do_after_create) end
|
29
29
|
def before_save() incr(:do_before_save) end
|
30
30
|
def after_save() incr(:do_after_save) end
|
31
|
+
def before_update() incr(:do_before_update) end
|
32
|
+
def after_update() incr(:do_after_update) end
|
31
33
|
def before_delete() incr(:do_before_delete) end
|
32
34
|
def after_delete() incr(:do_after_delete) end
|
33
35
|
|
@@ -18,6 +18,9 @@ class Post < Ohm::Model
|
|
18
18
|
|
19
19
|
before :delete, :do_before_delete
|
20
20
|
after :delete, :do_after_delete
|
21
|
+
|
22
|
+
before :update, :do_before_update
|
23
|
+
after :update, :do_after_update
|
21
24
|
|
22
25
|
def validate
|
23
26
|
super
|
@@ -40,6 +43,8 @@ protected
|
|
40
43
|
def do_after_create() incr(:do_after_create) end
|
41
44
|
def do_before_save() incr(:do_before_save) end
|
42
45
|
def do_after_save() incr(:do_after_save) end
|
46
|
+
def do_before_update() incr(:do_before_update) end
|
47
|
+
def do_after_update() incr(:do_after_update) end
|
43
48
|
def do_before_delete() incr(:do_before_delete) end
|
44
49
|
def do_after_delete() incr(:do_after_delete) end
|
45
50
|
|