ohm-contrib 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/callbacks.rb +14 -1
- data/ohm-contrib.gemspec +1 -1
- data/test/test_ohm_contrib_callbacks.rb +24 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.22
|
data/lib/ohm/contrib.rb
CHANGED
@@ -39,6 +39,7 @@ module Ohm
|
|
39
39
|
module Callbacks
|
40
40
|
def self.included(base)
|
41
41
|
base.extend Macros
|
42
|
+
base.extend Overrides
|
42
43
|
end
|
43
44
|
|
44
45
|
module Macros
|
@@ -109,6 +110,18 @@ module Ohm
|
|
109
110
|
@callbacks ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] }}
|
110
111
|
end
|
111
112
|
end
|
113
|
+
|
114
|
+
# This module is for class method overrides. As of now
|
115
|
+
# it only overrides Ohm::Model::create to force calling save
|
116
|
+
# instead of calling create so that Model.create will call
|
117
|
+
# not only before/after :create but also before/after :save
|
118
|
+
module Overrides
|
119
|
+
def create(*args)
|
120
|
+
model = new(*args)
|
121
|
+
model.save
|
122
|
+
model
|
123
|
+
end
|
124
|
+
end
|
112
125
|
|
113
126
|
# Overrides the validate method of Ohm::Model. This is a bit tricky,
|
114
127
|
# since typically you override this. Make sure you do something like:
|
@@ -170,4 +183,4 @@ module Ohm
|
|
170
183
|
end
|
171
184
|
end
|
172
185
|
end
|
173
|
-
end
|
186
|
+
end
|
data/ohm-contrib.gemspec
CHANGED
@@ -94,6 +94,29 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
+
context "on create when valid state" do
|
98
|
+
setup do
|
99
|
+
@post = Post.create(:body => "The Body")
|
100
|
+
end
|
101
|
+
|
102
|
+
should "call all callbacks" do
|
103
|
+
assert @post.did?(:do_before_validate)
|
104
|
+
assert @post.did?(:do_after_validate)
|
105
|
+
assert @post.did?(:do_before_create)
|
106
|
+
assert @post.did?(:do_after_create)
|
107
|
+
assert @post.did?(:do_before_save)
|
108
|
+
assert @post.did?(:do_after_save)
|
109
|
+
end
|
110
|
+
|
111
|
+
should "call create / save callbacks only once" do
|
112
|
+
assert_equal 1, @post.count(:do_before_create)
|
113
|
+
assert_equal 1, @post.count(:do_after_create)
|
114
|
+
assert_equal 1, @post.count(:do_before_save)
|
115
|
+
assert_equal 1, @post.count(:do_after_create)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
|
97
120
|
context "on save of an existing object" do
|
98
121
|
setup do
|
99
122
|
@post = Post.create(:body => "The Body")
|
@@ -167,4 +190,4 @@ class OhmContribCallbacksTest < Test::Unit::TestCase
|
|
167
190
|
assert ! @post.did?(:do_after_save)
|
168
191
|
end
|
169
192
|
end
|
170
|
-
end
|
193
|
+
end
|