8000 Only use AsyncCounts for scopes configured with show_count: :async · activeadmin/activeadmin@58c1c00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58c1c00

Browse files
committed
Only use AsyncCounts for scopes configured with show_count: :async
1 parent d214305 commit 58c1c00

File tree

2 files changed

+68
-45
lines changed

2 files changed

+68
-45
lines changed

lib/active_admin/views/components/scopes.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ def display_scope?(scope)
7373
call_method_or_exec_proc(scope.display_if_block)
7474
end
7575

76-
def async_counts?
77-
collection_before_scope.respond_to?(:async_count)
78-
end
79-
8076
def prepare_async_counts(scopes, options)
81-
@async_counts = if options[:scope_count] && async_counts?
77+
@async_counts = if options[:scope_count]
8278
scopes
83-
.select(&:show_count)
79+
.select(&:async_count?)
8480
.select { |scope| display_scope?(scope) }
8581
.index_with { |scope| AsyncCount.new(scope_chain(scope, collection_before_scope)) }
8682
else

spec/unit/views/components/scopes_spec.rb

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,56 +60,83 @@
6060
end
6161
end
6262

63< 10000 /td>-
it "uses AsyncCounts when available", if: Post.respond_to?(:async_count) do
64-
scopes
65-
66-
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.all)
67-
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.where.not(published_date: nil))
68-
end
69-
70-
it "avoids AsyncCounts when unavailable", unless: Post.respond_to?(:async_count) do
71-
scopes
72-
73-
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
74-
end
75-
76-
context "when an individual scope is configured to hide its count" do
63+
context "when scopes are configured to query their counts asynchronously" do
7764
let(:configured_scopes) do
7865
[
79-
ActiveAdmin::Scope.new(:all, nil, show_count: false)
66+
ActiveAdmin::Scope.new(:all, nil, show_count: :async),
67+
ActiveAdmin::Scope.new(:published, nil, show_count: :async) { |posts| posts.where.not(published_date: nil) }
8068
]
8169
end
8270

83-
it "avoids AsyncCounts" do
84-
scopes
85-
86-
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
87-
end
88-
end
89-
90-
context "when a scope is not to be displayed" do
91-
let(:configured_scopes) do
92-
[
93-
ActiveAdmin::Scope.new(:all, nil, if: -> { false })
94-
]
71+
it "raises an error when ActiveRecord async_count is unavailable", unless: Post.respond_to?(:async_count) do
72+
expect { scopes }.to raise_error(NoMethodError, %r{async_count})
9573
end
9674

97-
it "avoids AsyncCounts" do
98-
scopes
99-
100-
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
75+
context "when async_count is available in Rails", if: Post.respond_to?(:async_count) do
76+
it "uses AsyncCounts" do
77+
scopes
78+
79+
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.all)
80+
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.where.not(published_date: nil))
81+
end
82+
83+
context "when an individual scope is configured to show its count async" do
84+
let(:configured_scopes) do
85+
[
86+
ActiveAdmin::Scope.new(:all),
87+
ActiveAdmin::Scope.new(:published, nil, show_count: :async) { |posts| posts.where.not(published_date: nil) }
88+
]
89+
end
90+
91+
it "only uses AsyncCounts for the configured scopes" do
92+
scopes
93+
94+
expect(ActiveAdmin::AsyncCount).not_to have_received(:new).with(Post.all)
95+
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.where.not(published_date: nil))
96+
end
97+
end
98+
99+
context "when an individual scope is configured to hide its count" do
100+
let(:configured_scopes) do
101+
[
102+
ActiveAdmin::Scope.new(:all, nil, show_count: false),
103+
ActiveAdmin::Scope.new(:published, nil, show_count: :async) { |posts| posts.where.not(published_date: nil) }
104+
]
105+
end
106+
107+
it "only uses AsyncCounts for the configured scopes" do
108+
scopes
109+
110+
expect(ActiveAdmin::AsyncCount).not_to have_received(:new).with(Post.all)
111+
expect(ActiveAdmin::AsyncCount).to have_received(:new).with(Post.where.not(published_date: nil))
112+
end
113+
end
114+
115+
context "when a scope is not to be displayed" do
116+
let(:configured_scopes) do
117+
[
118+
ActiveAdmin::Scope.new(:all, nil, show_count: :async, if: -> { false })
119+
]
120+
end
121+
122+
it "avoids AsyncCounts" do
123+
scopes
124+
125+
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
126+
end
127+
end
101128
end
102-
end
103129

104-
context "when :show_count is configured as false" do
105-
let(:scope_options) do
106-
{ scope_count: false }
107-
end
130+
context "when :show_count is configured as false" do
131+
let(:scope_options) do
132+
{ scope_count: false }
133+
end
108134

109-
it "avoids AsyncCounts" do
110-
scopes
135+
it "avoids AsyncCounts" do
136+
scopes
111137

112-
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
138+
expect(ActiveAdmin::AsyncCount).not_to have_received(:new)
139+
end
113140
end
114141
end
115142
end

0 commit comments

Comments
 (0)
0