Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/pkg
/tmp
/coverage
.idea
2 changes: 1 addition & 1 deletion lib/her/model/associations/has_many_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.parse(association, klass, data)
# new_comment = user.comments.build(:body => "Hello!")
# new_comment # => #<Comment user_id=1 body="Hello!">
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
Expand Down
2 changes: 1 addition & 1 deletion lib/her/model/associations/has_one_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.parse(*args)
# new_role = user.role.build(:title => "moderator")
# new_role # => #<Role user_id=1 title="moderator">
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
Expand Down
3 changes: 3 additions & 0 deletions lib/her/model/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down