diff --git a/lib/her/model/associations/association.rb b/lib/her/model/associations/association.rb index 81fdeb96..99775f29 100644 --- a/lib/her/model/associations/association.rb +++ b/lib/her/model/associations/association.rb @@ -51,9 +51,12 @@ def fetch(opts = {}) return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank? path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" } - @klass.get(path, @params).tap do |result| - @cached_result = result unless @params.any? + result = if opts[:as] == :collection + @klass.get_collection(path, @params) + else + @klass.get(path, @params) end + result.tap { |r| @cached_result = r unless @params.any? } end # @private diff --git a/lib/her/model/associations/has_many_association.rb b/lib/her/model/associations/has_many_association.rb index 63cef796..f846ee5a 100644 --- a/lib/her/model/associations/has_many_association.rb +++ b/lib/her/model/associations/has_many_association.rb @@ -83,7 +83,7 @@ def create(attributes = {}) # @private def fetch - super.tap do |o| + super(as: :collection).tap do |o| inverse_of = @opts[:inverse_of] || @parent.singularized_resource_name o.each { |entry| entry.send("#{inverse_of}=", @parent) } end diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index a3bf3f68..cdc68aaa 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -89,6 +89,7 @@ builder.adapter :test do |stub| stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :comments => [{ :comment => { :id => 2, :body => "Tobias, you blow hard!", :user_id => 1 } }, { :comment => { :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, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 2 }.to_json] } + stub.get("/users/3/comments") { |env| [200, {}, nil]} stub.get("/users/1/comments") { |env| [200, {}, [{ :comment => { :id => 4, :body => "They're having a FIRESALE?" } }].to_json] } stub.get("/users/2/comments") { |env| [200, {}, [{ :comment => { :id => 4, :body => "They're having a FIRESALE?" } }, { :comment => { :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] } @@ -137,6 +138,7 @@ let(:user_with_included_data_after_create) { Foo::User.create } let(:user_with_included_data_after_save_existing) { Foo::User.save_existing(5, :name => "Clancy Brown") } let(:user_with_included_data_after_destroy) { Foo::User.new(:id => 5).destroy } + let(:user_with_no_comments) { Foo::User.new(id: 3)} let(:comment_without_included_parent_data) { Foo::Comment.new(:id => 7, :user_id => 1) } it "maps an array of included data through has_many" do @@ -245,6 +247,12 @@ params[:comments].length.should eq(2) end + it 'responds properly to blank?' do + @user_with_included_data.comments.should_not be_blank + user_with_no_comments.comments.should be_blank + end + + [:create, :save_existing, :destroy].each do |type| context "after #{type}" do let(:subject) { self.send("user_with_included_data_after_#{type}")}