Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,14 @@ def find_every(options)

# Find a single resource from a one-off URL
def find_one(options)
prefix_options, query_options = split_options(options[:params])

case from = options[:from]
when Symbol
instantiate_record(get(from, options[:params]))
instantiate_record(get(from, query_options), prefix_options)
when String
path = "#{from}#{query_string(options[:params])}"
instantiate_record(format.decode(connection.get(path, headers).body))
path = "#{from}#{query_string(query_options)}"
instantiate_record(format.decode(connection.get(path, headers).body), prefix_options)
end
end

Expand Down
15 changes: 4 additions & 11 deletions lib/active_resource/singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,11 @@ def singleton_path(prefix_options = {}, query_options = nil)
# Inventory.find
# # => raises ResourceNotFound
def find(options = {})
find_singleton(options)
end

private
# Find singleton resource
def find_singleton(options)
prefix_options, query_options = split_options(options[:params])
prefix_options, query_options = split_options(options[:params])
path = singleton_path(prefix_options, query_options)

path = singleton_path(prefix_options, query_options)
resp = self.format.decode(self.connection.get(path, self.headers).body)
instantiate_record(resp, prefix_options)
end
super(:one, options.merge(from: path, params: prefix_options))
end
end
# Deletes the resource from the remote service.
#
Expand Down
9 changes: 9 additions & 0 deletions test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def test_find_single_by_from
assert_equal "David", david.name
end

def test_find_single_by_from_with_prefix
max = { id: 1, name: "Max" }.to_json
ActiveResource::HttpMock.respond_to { |m| m.get "/person/1/pets/favorite.json", {}, max }

pet = Pet.find(:one, from: "/person/1/pets/favorite.json", params: { person_id: 1 })
assert_equal "Max", pet.name
assert_equal ({ person_id: 1 }), pet.prefix_options
end

def test_find_single_by_symbol_from
ActiveResource::HttpMock.respond_to { |m| m.get "/people/leader.json", {}, @david }

Expand Down
8 changes: 0 additions & 8 deletions test/singleton_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ def test_singleton_path_with_parameters
assert_equal "/products/5/inventory.json?sold=true", path
end

def test_find_singleton
setup_weather
weather = Weather.send(:find_singleton, Hash.new)
assert_not_nil weather
assert_equal "Sunny", weather.status
assert_equal 67, weather.temperature
end

def test_find
setup_weather
weather = Weather.find
Expand Down