8000 Provide friendlier error when async_count is not supported · activeadmin/activeadmin@32dfecf · GitHub
[go: up one dir, main page]

Skip to content

Commit 32dfecf

Browse files
jdlubranojavierjulio
authored andcommitted
Provide friendlier error when async_count is not supported
1 parent 7fd4bed commit 32dfecf

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/active_admin/async_count.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22
module ActiveAdmin
33
class AsyncCount
4+
class NotSupportedError < RuntimeError; end
5+
46
def initialize(collection)
7+
raise NotSupportedError, "#{collection.inspect} does not support :async_count" unless collection.respond_to?(:async_count)
8+
59
@collection = collection.except(:select, :order)
610
@promise = @collection.async_count
711
end

spec/unit/async_count_spec.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require "rails_helper"
33

4-
RSpec.describe ActiveAdmin::AsyncCount, if: Post.respond_to?(:async_count) do
4+
RSpec.describe ActiveAdmin::AsyncCount do
55
include ActiveAdmin::IndexHelper
66

77
def seed_posts
@@ -10,22 +10,29 @@ def seed_posts
1010
end
1111
end
1212

13-
it "can be passed to the collection_size helper" do
13+
it "can be passed to the collection_size helper", if: Post.respond_to?(:async_count) do
1414
seed_posts
1515

1616
expect(collection_size(described_class.new(Post.all))).to eq(Post.count)
1717
expect(collection_size(described_class.new(Post.group(:author_id)))).to eq(Post.distinct.pluck(:author_id).size)
1818
end
1919

2020
describe "#initialize" do
21-
it "initiates an async_count query" do
22-
collection = Post.all
21+
let(:collection) { Post.all }
22+
23+
it "initiates an async_count query", if: Post.respond_to?(:async_count) do
2324
expect(collection).to receive(:async_count)
2425
described_class.new(collection)
2526
end
27+
28+
it "raises an error when ActiveRecord async_count is unavailable", unless: Post.respond_to?(:async_count) do
29+
expect do
30+
described_class.new(collection)
31+
end.to raise_error(ActiveAdmin::AsyncCount::NotSupportedError, %r{does not support :async_count})
32+
end
2633
end
2734

28-
describe "#count" do
35+
describe "#count", if: Post.respond_to?(:async_count) do
2936
before { seed_posts }
3037

3138
it "returns the result of a count query" do
@@ -49,7 +56,7 @@ def seed_posts
4956
end
5057
end
5158

52-
describe "delegation" do
59+
describe "delegation", if: Post.respond_to?(:async_count) do
5360
let(:collection) { Post.all }
5461

5562
%i[

spec/unit/views/components/scopes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
end
7070

7171
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})
72+
expect { scopes }.to raise_error(ActiveAdmin::AsyncCount::NotSupportedError, %r{does not support :async_count})
7373
end
7474

7575
context "when async_count is available in Rails", if: Post.respond_to?(:async_count) do

0 commit comments

Comments
 (0)
0