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

Has many fetch collection check #264

Open
wants to merge 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/her/model/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}")}
Expand Down