22
33require "abstract_unit"
44require "active_support/core_ext/hash/conversions"
5+ require "active_support/concern"
6+ require "fixtures/weather"
57
6- class Developer < ActiveResource :: Base
7- self . site = "http://37s.sunrise.i:3000"
8+ module CallbackHistory
9+ extend ActiveSupport :: Concern
810
9- class << self
11+ class_methods do
1012 def callback_string ( callback_method )
1113 "history << [#{ callback_method . to_sym . inspect } , :string]"
1214 end
@@ -31,28 +33,44 @@ def callback_object(callback_method)
3133 end
3234 end
3335
34- ActiveResource ::Callbacks ::CALLBACKS . each do |callback_method |
35- next if callback_method . to_s =~ /^around_/
36- define_callback_method ( callback_method )
37- send ( callback_method , callback_proc ( callback_method ) )
38- send ( callback_method , callback_object ( callback_method ) )
39- send ( callback_method ) { |model | model . history << [ callback_method , :block ] }
36+ included do
37+ ActiveResource ::Callbacks ::CALLBACKS . each do |callback_method |
38+ next if callback_method . to_s =~ /^around_/
39+ define_callback_method ( callback_method )
40+ send ( callback_method , callback_proc ( callback_method ) )
41+ send ( callback_method , callback_object ( callback_method ) )
42+ send ( callback_method ) { |model | model . history << [ callback_method , :block ] }
43+ end
4044 end
4145
4246 def history
4347 @history ||= [ ]
4448 end
4549end
4650
51+ class Developer < ActiveResource ::Base
52+ include CallbackHistory
53+
54+ self . site = "http://37s.sunrise.i:3000"
55+ end
56+
57+ Weather . include CallbackHistory
58+
4759class CallbacksTest < ActiveSupport ::TestCase
4860 def setup
4961 @developer_attrs = { id : 1 , name : "Guillermo" , salary : 100_000 }
5062 @developer = { "developer" => @developer_attrs } . to_json
63+ @weather_attrs = { status : "Sunny" , temperature : 67 }
64+ @weather = { weather : @weather_attrs } . to_json
5165 ActiveResource ::HttpMock . respond_to do |mock |
5266 mock . post "/developers.json" , { } , @developer , 201 , "Location" => "/developers/1.json"
5367 mock . get "/developers/1.json" , { } , @developer
5468 mock . put "/developers/1.json" , { } , nil , 204
5569 mock . delete "/developers/1.json" , { } , nil , 200
70+ mock . get "/weather.json" , { } , @weather
71+ mock . post "/weather.json" , { } , @weather , 201 , "Location" => "/weather.json"
72+ mock . delete "/weather.json" , { } , nil
73+ mock . put "/weather.json" , { } , nil , 204
5674 end
5775 end
5876
@@ -101,6 +119,36 @@ def test_create
101119 ] , developer . history
102120 end
103121
122+ def test_create_singleton
123+ weather = Weather . create ( @weather_attrs )
124+ assert_equal [
125+ [ :before_validation , :method ] ,
126+ [ :before_validation , :proc ] ,
127+ [ :before_validation , :object ] ,
128+ [ :before_validation , :block ] ,
129+ [ :after_validation , :method ] ,
130+ [ :after_validation , :proc ] ,
131+ [ :after_validation , :object ] ,
132+ [ :after_validation , :block ] ,
133+ [ :before_save , :method ] ,
134+ [ :before_save , :proc ] ,
135+ [ :before_save , :object ] ,
136+ [ :before_save , :block ] ,
137+ [ :before_create , :method ] ,
138+ [ :before_create , :proc ] ,
139+ [ :before_create , :object ] ,
140+ [ :before_create , :block ] ,
141+ [ :after_create , :method ] ,
142+ [ :after_create , :proc ] ,
143+ [ :after_create , :object ] ,
144+ [ :after_create , :block ] ,
145+ [ :after_save , :method ] ,
146+ [ :after_save , :proc ] ,
147+ [ :after_save , :object ] ,
148+ [ :after_save , :block ]
149+ ] , weather . history
150+ end
151+
104152 def test_reload
105153 developer = Developer . find ( 1 )
106154 developer . reload
@@ -147,6 +195,37 @@ def test_update
147195 ] , developer . history
148196 end
149197
198+ def test_update_singleton
199+ weather = Weather . find
200+ weather . save
201+ assert_equal [
202+ [ :before_validation , :method ] ,
203+ [ :before_validation , :proc ] ,
204+ [ :before_validation , :object ] ,
205+ [ :before_validation , :block ] ,
206+ [ :after_validation , :method ] ,
207+ [ :after_validation , :proc ] ,
208+ [ :after_validation , :object ] ,
209+ [ :after_validation , :block ] ,
210+ [ :before_save , :method ] ,
211+ [ :before_save , :proc ] ,
212+ [ :before_save , :object ] ,
213+ [ :before_save , :block ] ,
214+ [ :before_update , :method ] ,
215+ [ :before_update , :proc ] ,
216+ [ :before_update , :object ] ,
217+ [ :before_update , :block ] ,
218+ [ :after_update , :method ] ,
219+ [ :after_update , :proc ] ,
220+ [ :after_update , :object ] ,
221+ [ :after_update , :block ] ,
222+ [ :after_save , :method ] ,
223+ [ :after_save , :proc ] ,
224+ [ :after_save , :object ] ,
225+ [ :after_save , :block ]
226+ ] , weather . history
227+ end
228+
150229 def test_destroy
151230 developer = Developer . find ( 1 )
152231 developer . destroy
@@ -162,6 +241,21 @@ def test_destroy
162241 ] , developer . history
163242 end
164243
244+ def test_destroy_singleton
245+ weather = Weather . find
246+ weather . destroy
247+ assert_equal [
248+ [ :before_destroy , :method ] ,
249+ [ :before_destroy , :proc ] ,
250+ [ :before_destroy , :object ] ,
251+ [ :before_destroy , :block ] ,
252+ [ :after_destroy , :method ] ,
253+ [ :after_destroy , :proc ] ,
254+ [ :after_destroy , :object ] ,
255+ [ :after_destroy , :block ]
256+ ] , weather . history
257+ end
258+
165259 def test_delete
166260 developer = Developer . find ( 1 )
167261 Developer . delete ( developer . id )
0 commit comments