Skip to content

Commit ea22c4a

Browse files
authored
Merge pull request #453 from seanpdoyle/singleton-inline-find-singleton
Singletons: Inline `find_singleton` back into `find`
2 parents f5eb8f2 + 7139ba3 commit ea22c4a

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-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

113106
##

test/cases/finder_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ def test_find_single_by_from
303303
assert_equal "David", david.name
304304
end
305305

306+
def test_find_single_by_from_with_prefix
307+
max = { id: 1, name: "Max" }.to_json
308+
ActiveResource::HttpMock.respond_to { |m| m.get "/person/1/pets/favorite.json", {}, max }
309+
310+
pet = Pet.find(:one, from: "/person/1/pets/favorite.json", params: { person_id: 1 })
311+
assert_equal "Max", pet.name
312+
assert_equal ({ person_id: 1 }), pet.prefix_options
313+
end
314+
306315
def test_find_single_by_symbol_from
307316
ActiveResource::HttpMock.respond_to { |m| m.get "/people/leader.json", {}, @david }
308317

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)