Skip to content

Commit 9555bbd

Browse files
Merge pull request hyperstack-org#219 from hyperstack-org/issue-215
Fixes hyperstack-org#215
2 parents ecba60a + 4ff430a commit 9555bbd

2 files changed

Lines changed: 67 additions & 14 deletions

File tree

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/collection.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,22 @@ def empty?
618618
count.zero?
619619
end
620620

621-
def any?
622-
!count.zero?
621+
def any?(*args, &block)
622+
# If there are any args passed in, then the collection is being used in the condition
623+
# and we must load it all into memory.
624+
return all.any?(*args, &block) if args&.length&.positive? || block.present?
625+
626+
# Otherwise we can just check the count for efficiency
627+
!empty?
628+
end
629+
630+
def none?(*args, &block)
631+
# If there are any args passed in, then the collection is being used in the condition
632+
# and we must load it all into memory.
633+
return all.none?(*args, &block) if args&.length&.positive? || block.present?
634+
635+
# Otherwise we can just check the count for efficiency
636+
empty?
623637
end
624638

625639
def method_missing(method, *args, &block)

ruby/hyper-model/spec/batch2/collection_aggregate_methods_spec.rb

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,61 @@
3232
size_window(:small, :portrait)
3333
end
3434

35-
[:count, :empty?, :any?].each do |method|
35+
%i[count empty? any? none?].each do |method|
3636
it "will not retrieve the entire collection when using #{method}" do
3737
FactoryBot.create(:test_model)
3838

39-
expect_promise(<<-RUBY
40-
Hyperstack::Model
41-
.load { TestModel.#{method} }
42-
.then do |val|
43-
if TestModel.all.instance_variable_get('@collection')
44-
"unnecessary fetch of all"
45-
else
46-
val
39+
expect_promise(
40+
<<~RUBY
41+
Hyperstack::Model
42+
.load { TestModel.#{method} }
43+
.then do |val|
44+
if TestModel.all.instance_variable_get('@collection')
45+
"unnecessary fetch of all"
46+
else
47+
val
48+
end
4749
end
48-
end
49-
RUBY
50-
).to eq(TestModel.all.send(method))
50+
RUBY
51+
).to eq(TestModel.all.send(method))
52+
end
53+
end
54+
55+
%i[any? none?].each do |method|
56+
it 'will retrieve the entire collection when using any? if an arg is passed in' do
57+
FactoryBot.create(:test_model)
58+
59+
expect_promise(
60+
<<~RUBY
61+
Hyperstack::Model.load do
62+
TestModel.#{method}(TestModel)
63+
end.then do |val|
64+
if TestModel.all.instance_variable_get('@collection')
65+
'necessary fetch of all'
66+
else
67+
val
68+
end
69+
end
70+
RUBY
71+
).to eq('necessary fetch of all')
72+
end
73+
74+
it 'will retrieve the entire collection when using any? if a block is passed in' do
75+
FactoryBot.create(:test_model)
76+
77+
expect_promise(
78+
<<~RUBY
79+
Hyperstack::Model.load do
80+
TestModel.#{method} { |test_model| test_model }
81+
end.then do |val|
82+
if TestModel.all.instance_variable_get('@collection')
83+
'necessary fetch of all'
84+
else
85+
val
86+
end
87+
end
88+
RUBY
89+
).to eq('necessary fetch of all')
5190
end
5291
end
5392
end

0 commit comments

Comments
 (0)