Skip to content

Commit

Permalink
Return an empty collection when where(:foo => []) is given
Browse files Browse the repository at this point in the history
The previous behaviour was to return everything! This is now
consistent with ActiveRecord and avoids a needless request.

The new behaviour is not applied to has_one associations as they
currently seem to ignore the where method entirely.
  • Loading branch information
chewi committed Aug 12, 2016
1 parent 8fcb92e commit 032d88f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/her/model/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ def fetch(opts = {})
return @cached_result unless @params.any? || @cached_result.nil?
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?
if @params.values.include?([]) and !self.is_a?(HasOneAssociation)
Her::Collection.new
else
path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" }
@klass.get(path, @params).tap do |result|
@cached_result = result unless @params.any?
end
end
end

Expand Down
12 changes: 8 additions & 4 deletions lib/her/model/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ def kind_of?(thing)
# @private
def fetch
@_fetch ||= begin
path = @parent.build_request_path(@params)
method = @parent.method_for(:find)
@parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
@parent.new_collection(parsed_data)
if @params.values.include?([])
Her::Collection.new
else
path = @parent.build_request_path(@params)
method = @parent.method_for(:find)
@parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
@parent.new_collection(parsed_data)
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@
@user_without_included_data.comments.first.object_id.should_not == @user_without_included_data.comments.where(:foo_id => 1).first.object_id
end

it "returns an empty collection without fetching when [] is a parameter" do
Foo::Comment.should_not_receive(:request)
comments = @user_without_included_data.comments.where(:foo_id => [])
comments.should respond_to(:length)
comments.size.should eql 0
end

it "maps an array of included data through has_one" do
@user_with_included_data.role.should be_a(Foo::Role)
@user_with_included_data.role.object_id.should == @user_with_included_data.role.object_id
Expand Down
7 changes: 7 additions & 0 deletions spec/model/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
Foo::User.create(:fullname => 'George Michael Bluth').id.should == 3
Foo::User.all.size.should eql 3
end

it "returns an empty collection without fetching when [] is given" do
Foo::User.should_not_receive(:request)
users = Foo::User.where(:fullname => [])
users.should respond_to(:length)
users.size.should eql 0
end
end

context "for parent class" do
Expand Down

0 comments on commit 032d88f

Please sign in to comment.