From b917b0be0ff989634db6ab65bf09a7da00fa6715 Mon Sep 17 00:00:00 2001 From: Ivan Filho Date: Wed, 11 Nov 2020 11:47:34 -0300 Subject: [PATCH] Fix find_every instantiating records from symbol and string without prefix_options --- lib/active_resource/base.rb | 27 ++++++++++++++++----------- test/abstract_unit.rb | 3 +++ test/cases/finder_test.rb | 26 ++++++++++++++++++++++++++ test/fixtures/pet.rb | 6 ++++++ 4 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/pet.rb diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb index 11c266bbb5..3f05fbca15 100644 --- a/lib/active_resource/base.rb +++ b/lib/active_resource/base.rb @@ -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. diff --git a/test/abstract_unit.rb b/test/abstract_unit.rb index 7d6a9c277f..c9760cbbd4 100644 --- a/test/abstract_unit.rb +++ b/test/abstract_unit.rb @@ -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) @@ -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 diff --git a/test/cases/finder_test.rb b/test/cases/finder_test.rb index 82e081de78..50da1e15b7 100644 --- a/test/cases/finder_test.rb +++ b/test/cases/finder_test.rb @@ -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 @@ -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 @@ -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 } @@ -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 } diff --git a/test/fixtures/pet.rb b/test/fixtures/pet.rb new file mode 100644 index 0000000000..b19d2f734c --- /dev/null +++ b/test/fixtures/pet.rb @@ -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