From 8a7afe1c2da302c3845d84cd5f599b6a8956e0b5 Mon Sep 17 00:00:00 2001 From: Pontus Date: Mon, 5 May 2014 10:18:55 +0200 Subject: [PATCH] Proposed solution to issue #224, getting the expected request_path when creating a nested resource --- .gitignore | 1 + lib/her/model/associations/has_many_association.rb | 2 +- lib/her/model/associations/has_one_association.rb | 2 +- lib/her/model/paths.rb | 3 +++ spec/model/associations_spec.rb | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f72c9df9..7e94dd5b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /pkg /tmp /coverage +.idea \ No newline at end of file diff --git a/lib/her/model/associations/has_many_association.rb b/lib/her/model/associations/has_many_association.rb index b64bbbb9..2dd40a75 100644 --- a/lib/her/model/associations/has_many_association.rb +++ b/lib/her/model/associations/has_many_association.rb @@ -50,7 +50,7 @@ def self.parse(association, klass, data) # new_comment = user.comments.build(:body => "Hello!") # new_comment # => # def build(attributes = {}) - @klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id)) + @klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id, :_parent_request_path => @parent.request_path)) end # Create a new object, save it and add it to the associated collection diff --git a/lib/her/model/associations/has_one_association.rb b/lib/her/model/associations/has_one_association.rb index 80cb3cd3..2ff28b46 100644 --- a/lib/her/model/associations/has_one_association.rb +++ b/lib/her/model/associations/has_one_association.rb @@ -45,7 +45,7 @@ def self.parse(*args) # new_role = user.role.build(:title => "moderator") # new_role # => # def build(attributes = {}) - @klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id)) + @klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id, :_parent_request_path => @parent.request_path)) end # Create a new object, save it and associate it to the parent diff --git a/lib/her/model/paths.rb b/lib/her/model/paths.rb index 6bf9dd3a..4db83d28 100644 --- a/lib/her/model/paths.rb +++ b/lib/her/model/paths.rb @@ -89,6 +89,7 @@ def resource_path(path = nil) # # @private def build_request_path(path=nil, parameters={}) + parent_request_path = path.delete('_parent_request_path') unless path.nil? parameters = parameters.try(:with_indifferent_access) unless path.is_a?(String) @@ -100,6 +101,8 @@ def build_request_path(path=nil, parameters={}) collection_path.dup end + path = "#{parent_request_path}/#{path}" unless parent_request_path.to_s.empty? + # Replace :id with our actual primary key path.gsub!(/(\A|\/):id(\Z|\/)/, "\\1:#{primary_key}\\2") end diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index 9cd08df7..9db37417 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -359,7 +359,7 @@ def present? builder.use Faraday::Request::UrlEncoded builder.adapter :test do |stub| stub.get("/users/10") { |env| [200, {}, { :id => 10 }.to_json] } - 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] } + 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] } end end