Skip to content

Commit 740c274

Browse files
authored
Merge pull request #348 from ivan05almeida/fix-find-all-prefix-options
Fix find_every instantiating records from symbol and string without prefix_options
2 parents dedecf9 + b917b0b commit 740c274

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

lib/active_resource/base.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,17 +1063,22 @@ def check_prefix_options(prefix_options)
10631063
# Find every resource
10641064
def find_every(options)
10651065
begin
1066-
case from = options[:from]
1067-
when Symbol
1068-
instantiate_collection(get(from, options[:params]), options[:params])
1069-
when String
1070-
path = "#{from}#{query_string(options[:params])}"
1071-
instantiate_collection(format.decode(connection.get(path, headers).body) || [], options[:params])
1072-
else
1073-
prefix_options, query_options = split_options(options[:params])
1074-
path = collection_path(prefix_options, query_options)
1075-
instantiate_collection((format.decode(connection.get(path, headers).body) || []), query_options, prefix_options)
1076-
end
1066+
params = options[:params]
1067+
prefix_options, query_options = split_options(params)
1068+
1069+
response =
1070+
case from = options[:from]
1071+
when Symbol
1072+
get(from, params)
1073+
when String
1074+
path = "#{from}#{query_string(query_options)}"
1075+
format.decode(connection.get(path, headers).body)
1076+
else
1077+
path = collection_path(prefix_options, query_options)
1078+
format.decode(connection.get(path, headers).body)
1079+
end
1080+
1081+
instantiate_collection(response || [], query_options, prefix_options)
10771082
rescue ActiveResource::ResourceNotFound
10781083
# Swallowing ResourceNotFound exceptions and return nil - as per
10791084
# ActiveRecord.

test/abstract_unit.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def setup_response
4040
@post = { id: 1, title: "Hello World", body: "Lorem Ipsum" }.to_json
4141
@posts = [{ id: 1, title: "Hello World", body: "Lorem Ipsum" }, { id: 2, title: "Second Post", body: "Lorem Ipsum" }].to_json
4242
@comments = [{ id: 1, post_id: 1, content: "Interesting post" }, { id: 2, post_id: 1, content: "I agree" }].to_json
43+
@pets = [{ id: 1, name: 'Max'}, { id: 2, name: 'Daisy'}].to_json
4344

4445
# - deep nested resource -
4546
# - Luis (Customer)
@@ -148,6 +149,8 @@ def setup_response
148149
# products
149150
mock.get "/products/1.json", {}, @product
150151
mock.get "/products/1/inventory.json", {}, @inventory
152+
#pets
153+
mock.get "/people/1/pets.json", {}, @pets
151154
end
152155

153156
Person.user = nil

test/cases/finder_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "fixtures/street_address"
77
require "fixtures/beast"
88
require "fixtures/proxy"
9+
require "fixtures/pet"
910
require "active_support/core_ext/hash/conversions"
1011

1112
class FinderTest < ActiveSupport::TestCase
@@ -120,6 +121,7 @@ def test_find_all_sub_objects
120121
all = StreetAddress.find(:all, params: { person_id: 1 })
121122
assert_equal 1, all.size
122123
assert_kind_of StreetAddress, all.first
124+
assert_equal ({ person_id: 1 }), all.first.prefix_options
123125
end
124126

125127
def test_find_all_sub_objects_not_found
@@ -144,6 +146,18 @@ def test_find_all_by_from_with_options
144146
assert_equal "David", people.first.name
145147
end
146148

149+
def test_find_all_by_from_with_prefix
150+
ActiveResource::HttpMock.respond_to { |m| m.get "/dogs.json", {}, @pets }
151+
152+
pets = Pet.find(:all, from: '/dogs.json', params: { person_id: 1 })
153+
assert_equal 2, pets.size
154+
assert_equal "Max", pets.first.name
155+
assert_equal ({ person_id: 1 }), pets.first.prefix_options
156+
157+
assert_equal "Daisy", pets.second.name
158+
assert_equal ({ person_id: 1 }), pets.second.prefix_options
159+
end
160+
147161
def test_find_all_by_symbol_from
148162
ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.json", {}, @people_david }
149163

@@ -152,6 +166,18 @@ def test_find_all_by_symbol_from
152166
assert_equal "David", people.first.name
153167
end
154168

169+
def test_find_all_by_symbol_from_with_prefix
170+
ActiveResource::HttpMock.respond_to { |m| m.get "/people/1/pets/dogs.json", {}, @pets }
171+
172+
pets = Pet.find(:all, from: :dogs, params: { person_id: 1 })
173+
assert_equal 2, pets.size
174+
assert_equal "Max", pets.first.name
175+
assert_equal ({ person_id: 1 }), pets.first.prefix_options
176+
177+
assert_equal "Daisy", pets.second.name
178+
assert_equal ({ person_id: 1 }), pets.second.prefix_options
179+
end
180+
155181
def test_find_first_or_last_not_found
156182
ActiveResource::HttpMock.respond_to { |m| m.get "/people.json", {}, "", 404 }
157183

test/fixtures/pet.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class Pet < ActiveResource::Base
4+
self.site = "http://37s.sunrise.i:3000"
5+
self.prefix = "/people/:person_id/"
6+
end

0 commit comments

Comments
 (0)