Skip to content

Commit

Permalink
Pass the build arguments through to new_element_path.
Browse files Browse the repository at this point in the history
Failing to do this leads to bad requests for nested element paths
so an api with self.site set to be

'foo.com/customers/:customer_id/items/:item_id'

will generate the url

/customers//items/new.json

when Item.build is called and the API returns an error code.

If we pass the attributes from build to new_element_path, this
behaviour is corrected such that build calls the url:

/customers/1/items/new.json
  • Loading branch information
Sean Handley authored and seanhandley committed Oct 25, 2012
1 parent 6639bb4 commit fd3616a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_resource/base.rb
Expand Up @@ -766,7 +766,7 @@ def collection_path(prefix_options = {}, query_options = nil)
# Returns the new resource instance.
#
def build(attributes = {})
attrs = self.format.decode(connection.get("#{new_element_path}", headers).body).merge(attributes)
attrs = self.format.decode(connection.get("#{new_element_path(attributes)}", headers).body)
self.new(attrs)
end

Expand Down
14 changes: 14 additions & 0 deletions test/cases/base_test.rb
Expand Up @@ -656,6 +656,20 @@ def test_build_with_custom_header
Person.headers.delete('key')
end

def test_build_without_attributes_for_prefix_call
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1/addresses/new.json", {}, StreetAddress.new.to_json
end
assert_raise(ActiveResource::InvalidRequestError) { StreetAddress.build }
end

def test_build_with_attributes_for_prefix_call
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1/addresses/new.json", {}, StreetAddress.new.to_json
end
assert_nothing_raised { StreetAddress.build(person_id: 1) }
end

def test_save
rick = Person.new
assert rick.save
Expand Down

0 comments on commit fd3616a

Please sign in to comment.