Skip to content

Commit e8ec222

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 8b7c6e9 commit e8ec222

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
@@ -48,9 +48,13 @@ def fetch(opts = {})
4848
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
4949
return @opts[:default].try(:dup) if @parent.new?
5050

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

lib/her/model/relation.rb

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

spec/model/associations_spec.rb

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

445452
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
@@ -60,6 +60,13 @@
6060
expect(Foo::User.create(fullname: "George Michael Bluth").id).to eq(3)
6161
expect(Foo::User.all.size).to eql 3
6262
end
63+
64+
it "returns an empty collection without fetching when [] is given" do
65+
Foo::User.should_not_receive(:request)
66+
users = Foo::User.where(:fullname => [])
67+
users.should respond_to(:length)
68+
users.size.should eql 0
69+
end
6370
end
6471

6572
context "for parent class" do

0 commit comments

Comments
 (0)