Skip to content

Commit 0901e9c

Browse files
committed
Return an empty collection when where(:foo => []) is given
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.
1 parent c319c63 commit 0901e9c

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

lib/her/model/associations/association.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ def fetch(opts = {})
4747
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
4848
return @opts[:default].try(:dup) if @parent.new?
4949

50-
path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" }
51-
@klass.get(path, @params).tap do |result|
52-
@cached_result = result unless @params.any?
50+
if @params.values.include?([]) and !self.is_a?(HasOneAssociation)
51+
Her::Collection.new
52+
else
53+
path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" }
54+
@klass.get(path, @params).tap do |result|
55+
@cached_result = result unless @params.any?
56+
end
5357
end
5458
end
5559

lib/her/model/relation.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ def kind_of?(thing)
6666
# @private
6767
def fetch
6868
@_fetch ||= begin
69-
path = @parent.build_request_path(@parent.collection_path, @params)
70-
method = @parent.method_for(:find)
71-
@parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
72-
@parent.new_collection(parsed_data)
69+
if @params.values.include?([])
70+
Her::Collection.new
71+
else
72+
path = @parent.build_request_path(@parent.collection_path, @params)
73+
method = @parent.method_for(:find)
74+
@parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
75+
@parent.new_collection(parsed_data)
76+
end
7377
end
7478
end
7579
end

spec/model/associations_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@
439439
expect(user.organization.respond_to?(:empty?)).to be_truthy
440440
expect(user.organization).not_to be_empty
441441
end
442+
443+
it "returns an empty collection without fetching when [] is a parameter" do
444+
Foo::Comment.should_not_receive(:request)
445+
comments = user.comments.where(:foo_id => [])
446+
comments.should respond_to(:length)
447+
comments.size.should eql 0
448+
end
442449
end
443450

444451
context "without included parent data" do

spec/model/relation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
expect(Foo::User.create(fullname: "George Michael Bluth").id).to eq(3)
6060
expect(Foo::User.all.size).to eql 3
6161
end
62+
63+
it "returns an empty collection without fetching when [] is given" do
64+
Foo::User.should_not_receive(:request)
65+
users = Foo::User.where(:fullname => [])
66+
users.should respond_to(:length)
67+
users.size.should eql 0
68+
end
6269
end
6370

6471
context "for parent class" do

0 commit comments

Comments
 (0)