Skip to content

Commit 55248a1

Browse files
committed
Singleton: Inline find_singleton back into find
When the Singleton module was originally introduced in [#4][], there was a suggestion about the implementation for `.find` that used the `:one` modifier option with the `:from` keyword argument. ```ruby class Account < ActiveResource::Base def self.find_singleton find :one, from: '/account.xml' end end ``` This commit modifies the `Singleton.find` method by inlining the private `find_singleton` method. By doing so, the implementation can invoke `super`, and rely on the built-in `instantiate_record` call provided by `Base`. This reduces the amount of duplication involved in transforming a response payload into a resource instance. [#4]: #4 [#4]: #4 (comment)
1 parent bdb7f20 commit 55248a1

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

lib/active_resource/base.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,12 +1217,14 @@ def find_every(options)
12171217

12181218
# Find a single resource from a one-off URL
12191219
def find_one(options)
1220+
prefix_options, query_options = split_options(options[:params])
1221+
12201222
case from = options[:from]
12211223
when Symbol
1222-
instantiate_record(get(from, options[:params]))
1224+
instantiate_record(get(from, query_options), prefix_options)
12231225
when String
1224-
path = "#{from}#{query_string(options[:params])}"
1225-
instantiate_record(format.decode(connection.get(path, headers).body))
1226+
path = "#{from}#{query_string(query_options)}"
1227+
instantiate_record(format.decode(connection.get(path, headers).body), prefix_options)
12261228
end
12271229
end
12281230

lib/active_resource/singleton.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,11 @@ def singleton_path(prefix_options = {}, query_options = nil)
9696
# Inventory.find
9797
# # => raises ResourceNotFound
9898
def find(options = {})
99-
find_singleton(options)
100-
end
101-
102-
private
103-
# Find singleton resource
104-
def find_singleton(options)
105-
prefix_options, query_options = split_options(options[:params])
99+
prefix_options, query_options = split_options(options[:params])
100+
path = singleton_path(prefix_options, query_options)
106101

107-
path = singleton_path(prefix_options, query_options)
108-
resp = self.format.decode(self.connection.get(path, self.headers).body)
109-
instantiate_record(resp, prefix_options)
110-
end
102+
super(:one, options.merge(from: path, params: prefix_options))
103+
end
111104
end
112105
# Deletes the resource from the remote service.
113106
#

test/singleton_test.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ def test_singleton_path_with_parameters
6969
assert_equal "/products/5/inventory.json?sold=true", path
7070
end
7171

72-
def test_find_singleton
73-
setup_weather
74-
weather = Weather.send(:find_singleton, Hash.new)
75-
assert_not_nil weather
76-
assert_equal "Sunny", weather.status
77-
assert_equal 67, weather.temperature
78-
end
79-
8072
def test_find
8173
setup_weather
8274
weather = Weather.find

0 commit comments

Comments
 (0)