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
27 changes: 16 additions & 11 deletions lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1063,17 +1063,22 @@ def check_prefix_options(prefix_options)
# Find every resource
def find_every(options)
begin
case from = options[:from]
when Symbol
instantiate_collection(get(from, options[:params]), options[:params])
when String
path = "#{from}#{query_string(options[:params])}"
instantiate_collection(format.decode(connection.get(path, headers).body) || [], options[:params])
else
prefix_options, query_options = split_options(options[:params])
path = collection_path(prefix_options, query_options)
instantiate_collection((format.decode(connection.get(path, headers).body) || []), query_options, prefix_options)
end
params = options[:params]
prefix_options, query_options = split_options(params)

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

instantiate_collection(response || [], query_options, prefix_options)
rescue ActiveResource::ResourceNotFound
# Swallowing ResourceNotFound exceptions and return nil - as per
# ActiveRecord.
Expand Down
3 changes: 3 additions & 0 deletions test/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def setup_response
@post = { id: 1, title: "Hello World", body: "Lorem Ipsum" }.to_json
@posts = [{ id: 1, title: "Hello World", body: "Lorem Ipsum" }, { id: 2, title: "Second Post", body: "Lorem Ipsum" }].to_json
@comments = [{ id: 1, post_id: 1, content: "Interesting post" }, { id: 2, post_id: 1, content: "I agree" }].to_json
@pets = [{ id: 1, name: 'Max'}, { id: 2, name: 'Daisy'}].to_json

# - deep nested resource -
# - Luis (Customer)
Expand Down Expand Up @@ -148,6 +149,8 @@ def setup_response
# products
mock.get "/products/1.json", {}, @product
mock.get "/products/1/inventory.json", {}, @inventory
#pets
mock.get "/people/1/pets.json", {}, @pets
end

Person.user = nil
Expand Down
26 changes: 26 additions & 0 deletions test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "fixtures/street_address"
require "fixtures/beast"
require "fixtures/proxy"
require "fixtures/pet"
require "active_support/core_ext/hash/conversions"

class FinderTest < ActiveSupport::TestCase
Expand Down Expand Up @@ -120,6 +121,7 @@ def test_find_all_sub_objects
all = StreetAddress.find(:all, params: { person_id: 1 })
assert_equal 1, all.size
assert_kind_of StreetAddress, all.first
assert_equal ({ person_id: 1 }), all.first.prefix_options
end

def test_find_all_sub_objects_not_found
Expand All @@ -144,6 +146,18 @@ def test_find_all_by_from_with_options
assert_equal "David", people.first.name
end

def test_find_all_by_from_with_prefix
ActiveResource::HttpMock.respond_to { |m| m.get "/dogs.json", {}, @pets }

pets = Pet.find(:all, from: '/dogs.json', params: { person_id: 1 })
assert_equal 2, pets.size
assert_equal "Max", pets.first.name
assert_equal ({ person_id: 1 }), pets.first.prefix_options

assert_equal "Daisy", pets.second.name
assert_equal ({ person_id: 1 }), pets.second.prefix_options
end

def test_find_all_by_symbol_from
ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.json", {}, @people_david }

Expand All @@ -152,6 +166,18 @@ def test_find_all_by_symbol_from
assert_equal "David", people.first.name
end

def test_find_all_by_symbol_from_with_prefix
ActiveResource::HttpMock.respond_to { |m| m.get "/people/1/pets/dogs.json", {}, @pets }

pets = Pet.find(:all, from: :dogs, params: { person_id: 1 })
assert_equal 2, pets.size
assert_equal "Max", pets.first.name
assert_equal ({ person_id: 1 }), pets.first.prefix_options

assert_equal "Daisy", pets.second.name
assert_equal ({ person_id: 1 }), pets.second.prefix_options
end

def test_find_first_or_last_not_found
ActiveResource::HttpMock.respond_to { |m| m.get "/people.json", {}, "", 404 }

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/pet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Pet < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
self.prefix = "/people/:person_id/"
end