Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/active_resource/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def defines_has_one_finder_method(reflection)
elsif reflection.klass.respond_to?(:singleton_name)
instance_variable_set(ivar_name, reflection.klass.find(params: { "#{self.class.element_name}_id": self.id }))
else
instance_variable_set(ivar_name, reflection.klass.find(:one, from: "/#{self.class.collection_name}/#{self.id}/#{method_name}#{self.class.format_extension}"))
instance_variable_set(ivar_name, reflection.klass.find(:one, from: "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{ERB::Util.url_encode(id.to_s)}/#{method_name}#{self.class.format_extension}"))
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,33 @@ def test_parse_non_singleton_resource_with_has_one_makes_get_request_on_child_ro
assert post.author.name == ActiveSupport::JSON.decode(@matz)["person"]["name"]
end

def test_non_singleton_has_one_child_route_includes_site_path
accepts = { "Accept" => "application/json" }
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v1/posts/1.json", accepts, @post
mock.get "/api/v1/posts/1/author.json", accepts, @matz
end

post_class = Class.new(ActiveResource::Base)
post_class.site = "http://37s.sunrise.i:3000/api/v1"
post_class.element_name = "post"
post_class.send(:has_one, :author, class_name: "Person")
post = post_class.find(1)
assert_equal "Matz", post.author.name
end

def test_non_singleton_has_one_child_route_includes_prefix_options
accepts = { "Accept" => "application/json" }
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/posts/5/comments/1.json", accepts, { id: 1, post_id: 5 }.to_json
mock.get "/posts/5/comments/1/author.json", accepts, @matz
end

Comment.send(:has_one, :author, class_name: "Person")
comment = Comment.find(1, params: { post_id: 5 })
assert_equal "Matz", comment.author.name
end

def test_with_custom_formatter
addresses = [ { id: "1", street: "1 Infinite Loop", city: "Cupertino", state: "CA" } ].to_xml(root: :addresses)

Expand Down
Loading