Skip to content

Commit 2d1345c

Browse files
committed
Singletons: Fix broken reload method
Prior to this commit, singleton resources were unable to invoke `reload` to re-fetch themselves and load new data. To resolve that issue, this commit introduces a protected `Base#find_self` method that can be overridden by the `Singleton#find_self` version.
1 parent f5eb8f2 commit 2d1345c

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

lib/active_resource/base.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ def encode(options = {})
15531553
# my_branch.name # => "Wilson Road"
15541554
def reload
15551555
run_callbacks :reload do
1556-
self.load(self.class.find(to_param, params: @prefix_options).attributes, false, true)
1556+
self.load(find_self.attributes, false, true)
15571557
end
15581558
end
15591559

@@ -1809,6 +1809,10 @@ def find_or_create_resource_for(name)
18091809
end
18101810
end
18111811

1812+
def find_self
1813+
self.class.find(to_param, params: @prefix_options)
1814+
end
1815+
18121816
def const_valid?(*const_args)
18131817
self.class.const_defined?(*const_args)
18141818
true

lib/active_resource/singleton.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,9 @@ def create(path = singleton_path)
141141
def singleton_path(options = nil)
142142
self.class.singleton_path(options || prefix_options)
143143
end
144+
145+
def find_self
146+
self.class.find(@prefix_options)
147+
end
144148
end
145149
end

test/cases/callbacks_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ def test_reload
164164
], developer.history
165165
end
166166

167+
def test_reload_singleton
168+
weather = Weather.find
169+
weather.reload
170+
assert_equal [
171+
[ :before_reload, :method ],
172+
[ :before_reload, :proc ],
173+
[ :before_reload, :object ],
174+
[ :before_reload, :block ],
175+
[ :after_reload, :method ],
176+
[ :after_reload, :proc ],
177+
[ :after_reload, :object ],
178+
[ :after_reload, :block ]
179+
], weather.history
180+
end
181+
167182
def test_update
168183
developer = Developer.find(1)
169184
developer.save

test/singleton_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,18 @@ def test_update
143143
weather.status = "Rainy"
144144
weather.save
145145
end
146+
147+
def test_reload
148+
setup_weather
149+
150+
# First Create the Weather
151+
weather = Weather.create!(status: "Sunny", temperature: 67)
152+
153+
# Then reload it
154+
weather.reload
155+
156+
request = ActiveResource::HttpMock.requests.last
157+
assert_equal :get, request.method
158+
assert_equal "/weather.json", request.path
159+
end
146160
end

0 commit comments

Comments
 (0)