Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix belongs to association load when using active model serializers #213

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion her.gemspec
Expand Up @@ -24,6 +24,6 @@ Gem::Specification.new do |s|

s.add_runtime_dependency "activemodel", ">= 3.0.0"
s.add_runtime_dependency "activesupport", ">= 3.0.0"
s.add_runtime_dependency "faraday", "~> 0.8"
s.add_runtime_dependency "faraday", "~> 0.8.9"
s.add_runtime_dependency "multi_json", "~> 1.7"
end
2 changes: 1 addition & 1 deletion lib/her/model/associations/belongs_to_association.rb
Expand Up @@ -79,7 +79,7 @@ def fetch
if @parent.attributes[@name].blank? || @params.any?
path_params = @parent.attributes.merge(@params.merge(@klass.primary_key => foreign_key_value))
path = build_association_path lambda { @klass.build_request_path(path_params) }
@klass.get(path, @params)
@klass.get_resource(path, @params)
else
@parent.attributes[@name]
end
Expand Down
37 changes: 36 additions & 1 deletion spec/model/associations_spec.rb
Expand Up @@ -390,4 +390,39 @@ def present?
end
end
end
end

context "belongs to" do
before do
spawn_model "Foo::User" do
parse_root_in_json true , format: :active_model_serializers
end
spawn_model "Foo::Comment" do
belongs_to :user
parse_root_in_json true , format: :active_model_serializers
end
spawn_model "Foo::Post" do
has_many :comments
parse_root_in_json true , format: :active_model_serializers
end

Her::API.setup :url => "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/posts/1") { |env| [200, {}, { post: { id: 1, name: 'post1' } }.to_json] }
stub.get("/posts/1/comments") { |env| [200, {}, { comments: [{ id: 1, name: "comment1", user_id: 1 }] }.to_json] }
stub.get("/users/1") { |env| [200, {}, { user: { :id => 2, :name => "Lindsay", :company => nil } }.to_json] }
end
end
Foo::Post.use_api Her::API.default_api
Foo::Comment.use_api Her::API.default_api
Foo::User.use_api Her::API.default_api
end

it 'should pull down the single user object when it belongs to an association' do
foo = Foo::Post.find(1)
foo.comments[0].user.name.should eql 'Lindsay'
end

end
end