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 get when used for has_one relationships with active model serializers #394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions lib/her/model/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def #{method}(path, params={})
path = build_request_path_from_string_or_symbol(path, params)
params = to_params(params) unless #{method.to_sym.inspect} == :get
send(:'#{method}_raw', path, params) do |parsed_data, response|
if parsed_data[:data].is_a?(Array) || active_model_serializers_format? || json_api_format?

if parsed_data[:data].is_a?(Array) || json_api_format? || (active_model_serializers_format? && parsed_data[:data].has_key?(pluralized_parsed_root_element))
new_collection(parsed_data)
else
new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:metadata], :_errors => parsed_data[:errors])
new_from_parsed_data(parsed_data)
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :user => { :id => 1, :name => "Tobias Fünke", :comments => [{ :id => 2, :body => "Tobias, you blow hard!", :user_id => 1 }, { :id => 3, :body => "I wouldn't mind kissing that man between the cheeks, so to speak", :user_id => 1 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 1, :name => "Bluth Company" }, :organization_id => 1 } }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :user => { :id => 2, :name => "Lindsay Fünke", :organization_id => 1 } }.to_json] }
stub.get("/users/2/role") { |env| [200, {}, { :role => { :id => 3, :body => "User" } }.to_json] }
stub.get("/users/1/comments") { |env| [200, {}, { :comments => [{ :id => 4, :body => "They're having a FIRESALE?" }] }.to_json] }
stub.get("/users/2/comments") { |env| [200, {}, { :comments => [{ :id => 4, :body => "They're having a FIRESALE?" }, { :id => 5, :body => "Is this the tiny town from Footloose?" }] }.to_json] }
stub.get("/users/2/comments/5") { |env| [200, {}, { :comment => { :id => 5, :body => "Is this the tiny town from Footloose?" } }.to_json] }
Expand All @@ -288,6 +289,7 @@
spawn_model "Foo::User" do
parse_root_in_json true, :format => :active_model_serializers
has_many :comments, class_name: "Foo::Comment"
has_one :role, class_name: "Foo::Role"
belongs_to :organization
end
spawn_model "Foo::Comment" do
Expand All @@ -298,6 +300,10 @@
parse_root_in_json true, :format => :active_model_serializers
end

spawn_model "Foo::Role" do
parse_root_in_json true, :format => :active_model_serializers
end

@user_with_included_data = Foo::User.find(1)
@user_without_included_data = Foo::User.find(2)
end
Expand All @@ -309,6 +315,12 @@
@user_with_included_data.comments.first.body.should == "Tobias, you blow hard!"
end

it "maps a hash of included data through has_one" do
@user_with_included_data.role.should be_a(Foo::Role)
@user_with_included_data.role.id.should == 1
@user_with_included_data.role.body.should == "Admin"
end

it "does not refetch the parents models data if they have been fetched before" do
@user_with_included_data.comments.first.user.object_id.should == @user_with_included_data.object_id
end
Expand All @@ -320,6 +332,12 @@
@user_without_included_data.comments.first.body.should == "They're having a FIRESALE?"
end

it "fetches data that was not included through has_one" do
@user_without_included_data.role.should be_a(Foo::Role)
@user_without_included_data.role.id.should == 3
@user_without_included_data.role.body.should == "User"
end

it "fetches has_many data even if it was included, only if called with parameters" do
@user_with_included_data.comments.where(:foo_id => 1).length.should == 1
end
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
@spawned_models = []
end

config.filter_run :focus
config.run_all_when_everything_filtered = true

config.after :each do
@spawned_models.each do |model|
Object.instance_eval { remove_const model } if Object.const_defined?(model)
Expand Down