Skip to content

Commit f404a9c

Browse files
committed
Use nested path parameters from the parent when using build
1 parent 282aecd commit f404a9c

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/her/model/associations/has_many_association.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ def self.parse(association, klass, data)
4949
# user = User.find(1)
5050
# new_comment = user.comments.build(:body => "Hello!")
5151
# new_comment # => #<Comment user_id=1 body="Hello!">
52-
# TODO: This only merges the id of the parents, handle the case
53-
# where this is more deeply nested
5452
def build(attributes = {})
55-
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id))
53+
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id)).tap do |resource|
54+
begin
55+
resource.request_path
56+
rescue Her::Errors::PathError => e
57+
e.missing_parameters.each do |m|
58+
if id = @parent.get_attribute(m) || @parent.get_attribute("_#{m}")
59+
resource.send("_#{m}=", id)
60+
end
61+
end
62+
end
63+
end
5664
end
5765

5866
# Create a new object, save it and add it to the associated collection

lib/her/model/associations/has_one_association.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ def self.parse(*args)
4545
# new_role = user.role.build(:title => "moderator")
4646
# new_role # => #<Role user_id=1 title="moderator">
4747
def build(attributes = {})
48-
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id))
48+
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id)).tap do |resource|
49+
begin
50+
resource.request_path
51+
rescue Her::Errors::PathError => e
52+
e.missing_parameters.each do |m|
53+
if id = @parent.get_attribute(m) || @parent.get_attribute("_#{m}")
54+
resource.send("_#{m}=", id)
55+
end
56+
end
57+
end
58+
end
4959
end
5060

5161
# Create a new object, save it and associate it to the parent

spec/model/associations_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,13 @@ def present?
449449

450450
context "building and creating association data" do
451451
before do
452-
spawn_model "Foo::Comment"
452+
spawn_model "Foo::Like" do
453+
collection_path "/users/:user_id/comments/:comment_id/likes"
454+
end
455+
spawn_model "Foo::Comment" do
456+
collection_path "/users/:user_id/comments"
457+
has_many :likes
458+
end
453459
spawn_model "Foo::User" do
454460
has_many :comments
455461
end
@@ -461,6 +467,10 @@ def present?
461467
@comment.body.should == "Hello!"
462468
@comment.user_id.should == 10
463469
end
470+
it "uses nested path parameters from the parent" do
471+
@like = Foo::User.new(:id => 10).comments.build(:id => 20).likes.build
472+
@like.request_path.should == "/users/10/comments/20/likes"
473+
end
464474
end
465475

466476
context "with #create" do
@@ -470,7 +480,7 @@ def present?
470480
builder.use Faraday::Request::UrlEncoded
471481
builder.adapter :test do |stub|
472482
stub.get("/users/10") { |env| [200, {}, { :id => 10 }.to_json] }
473-
stub.post("/comments") { |env| [200, {}, { :id => 1, :body => Faraday::Utils.parse_query(env[:body])['body'], :user_id => Faraday::Utils.parse_query(env[:body])['user_id'].to_i }.to_json] }
483+
stub.post("/users/10/comments") { |env| [200, {}, { :id => 1, :body => Faraday::Utils.parse_query(env[:body])['body'], :user_id => Faraday::Utils.parse_query(env[:body])['user_id'].to_i }.to_json] }
474484
end
475485
end
476486

0 commit comments

Comments
 (0)