8000 Make count query optimisation work when using decorate_with · activeadmin/activeadmin@8e24320 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e24320

Browse files
irmeladeivid-rodriguez
irmela
authored andcommitted
Make count query optimisation work when using decorate_with
1 parent 9736856 commit 8e24320

File tree

10 files changed

+64
-3
lines changed

10 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
* Make optimization to not use expensive COUNT queries also work for decorated actions. [#5811](https://github.com/activeadmin/activeadmin/pull/5811) by [@irmela]
8+
59
## 2.3.1 [](https://github.com/activeadmin/activeadmin/compare/v2.3.0..v2.3.1)
610

711
### Bug Fixes
@@ -499,6 +503,7 @@ Please check [0-6-stable] for previous changes.
499503
[#5800]: https://github.com/activeadmin/activeadmin/pull/5800
500504
[#5801]: https://github.com/activeadmin/activeadmin/pull/5801
501505
[#5802]: https://github.com/activeadmin/activeadmin/pull/5802
506+
[#5811]: https://github.com/activeadmin/activeadmin/pull/5811
502507
[#5816]: https://github.com/activeadmin/activeadmin/pull/5816
503508
[#5822]: https://github.com/activeadmin/activeadmin/pull/5822
504509
[#5826]: https://github.com/activeadmin/activeadmin/pull/5826
@@ -577,3 +582,4 @@ Please check [0-6-stable] for previous changes.
577582
[@ndbroadbent]: https://github.com/ndbroadbent
578583
[@Kris-LIBIS]: https://github.com/Kris-LIBIS
5 8000 79584
[@sgara]: https://github.com/sgara
585+
[@irmela]: https://github.com/irmela

Gemfile.common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ gem "devise", "~> 4.7"
1414
group :test do
1515
gem 'apparition'
1616
gem 'capybara', '~> 3.14'
17+
gem 'db-query-matchers', '0.9.0'
1718

1819
gem 'simplecov', require: false # Test coverage generator. Go to /coverage/ after running tests
1920
gem 'cucumber-rails', '~> 1.5', require: false

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ GEM
136136
cucumber-tag_expressions (1.1.1)
137137
cucumber-wire (0.0.1)
138138
database_cleaner (1.7.0)
139+
db-query-matchers (0.9.0)
140+
activesupport (>= 4.0, <= 6.0)
141+
rspec (~> 3.0)
139142
devise (4.7.1)
140143
bcrypt (~> 3.0)
141144
orm_adapter (~> 0.1)
@@ -323,6 +326,10 @@ GEM
323326
responders (3.0.0)
324327
actionpack (>= 5.0)
325328
railties (>= 5.0)
329+
rspec (3.8.0)
330+
rspec-core (~> 3.8.0)
331+
rspec-expectations (~> 3.8.0)
332+
rspec-mocks (~> 3.8.0)
326333
rspec-core (3.8.2)
327334
rspec-support (~> 3.8.0)
328335
rspec-expectations (3.8.4)
@@ -417,6 +424,7 @@ DEPENDENCIES
417424
cucumber
418425
cucumber-rails (~> 1.5)
419426
database_cleaner
427+
db-query-matchers (= 0.9.0)
420428
devise (~> 4.7)
421429
draper (~> 3.1)
422430
i18n-spec

gemfiles/rails_50.gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ GEM
115115
cucumber-tag_expressions (1.1.1)
116116
cucumber-wire (0.0.1)
117117
database_cleaner (1.7.0)
118+
db-query-matchers (0.9.0)
119+
activesupport (>= 4.0, <= 6.0)
120+
rspec (~> 3.0)
118121
devise (4.7.1)
119122
bcrypt (~> 3.0)
120123
orm_adapter (~> 0.1)
@@ -261,6 +264,10 @@ GEM
261264
responders (3.0.0)
262265
actionpack (>= 5.0)
263266
railties (>= 5.0)
267+
rspec (3.8.0)
268+
rspec-core (~> 3.8.0)
269+
rspec-expectations (~> 3.8.0)
270+
rspec-mocks (~> 3.8.0)
264271
rspec-core (3.8.2)
265272
rspec-support (~> 3.8.0)
266273
rspec-expectations (3.8.4)
@@ -334,6 +341,7 @@ DEPENDENCIES
334341
cucumber
335342
cucumber-rails (~> 1.5)
336343
database_cleaner
344+
db-query-matchers (= 0.9.0)
337345
devise (~> 4.7)
338346
draper (~> 3.1)
339347
jasmine

gemfiles/rails_51.gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ GEM
115115
cucumber-tag_expressions (1.1.1)
116116
cucumber-wire (0.0.1)
117117
database_cleaner (1.7.0)
118+
db-query-matchers (0.9.0)
119+
activesupport (>= 4.0, <= 6.0)
120+
rspec (~> 3.0)
118121
devise (4.7.1)
119122
bcrypt (~> 3.0)
120123
orm_adapter (~> 0.1)
@@ -261,6 +264,10 @@ GEM
261264
responders (3.0.0)
262265
actionpack (>= 5.0)
263266
railties (>= 5.0)
267+
rspec (3.8.0)
268+
rspec-core (~> 3.8.0)
269+
rspec-expectations (~> 3.8.0)
270+
rspec-mocks (~> 3.8.0)
264271
rspec-core (3.8.2)
265272
rspec-support (~> 3.8.0)
266273
rspec-expectations (3.8.4)
@@ -334,6 +341,7 @@ DEPENDENCIES
334341
cucumber
335342
cucumber-rails (~> 1.5)
336343
database_cleaner
344+
db-query-matchers (= 0.9.0)
337345
devise (~> 4.7)
338346
draper (~> 3.1)
339347
jasmine

gemfiles/rails_52.gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ GEM
119119
cucumber-tag_expressions (1.1.1)
120120
cucumber-wire (0.0.1)
121121
database_cleaner (1.7.0)
122+
db-query-matchers (0.9.0)
123+
activesupport (>= 4.0, <= 6.0)
124+
rspec (~> 3.0)
122125
devise (4.7.1)
123126
bcrypt (~> 3.0)
124127
orm_adapter (~> 0.1)
@@ -269,6 +272,10 @@ GEM
269272
responders (3.0.0)
270273
actionpack (>= 5.0)
271274
railties (>= 5.0)
275+
rspec (3.8.0)
276+
rspec-core (~> 3.8.0)
277< 10000 code class="diff-text syntax-highlighted-line addition">+
rspec-expectations (~> 3.8.0)
278+
rspec-mocks (~> 3.8.0)
272279
rspec-core (3.8.2)
273280
rspec-support (~> 3.8.0)
274281
rspec-expectations (3.8.4)
@@ -342,6 +349,7 @@ DEPENDENCIES
342349
cucumber
343350
cucumber-rails (~> 1.5)
344351
database_cleaner
352+
db-query-matchers (= 0.9.0)
345353
devise (~> 4.7)
346354
draper (~> 3.1)
347355
jasmine

gemfiles/rails_60_turbolinks.gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ GEM
132132
cucumber-tag_expressions (1.1.1)
133133
cucumber-wire (0.0.1)
134134
database_cleaner (1.7.0)
135+
db-query-matchers (0.9.0)
136+
activesupport (>= 4.0, <= 6.0)
137+
rspec (~> 3.0)
135138
devise (4.7.1)
136139
bcrypt (~> 3.0)
137140
orm_adapter (~> 0.1)
@@ -284,6 +287,10 @@ GEM
284287
responders (3.0.0)
285288
actionpack (>= 5.0)
286289
railties (>= 5.0)
290+
rspec (3.8.0)
291+
rspec-core (~> 3.8.0)
292+
rspec-expectations (~> 3.8.0)
293+
rspec-mocks (~> 3.8.0)
287294
rspec-core (3.8.2)
288295
rspec-support (~> 3.8.0)
289296
rspec-expectations (3.8.4)
@@ -361,6 +368,7 @@ DEPENDENCIES
361368
cucumber
362369
cucumber-rails (~> 1.5)
363370
database_cleaner
371+
db-query-matchers (= 0.9.0)
364372
devise (~> 4.7)
365373
draper (~> 3.1)
366374
jasmine

lib/active_admin/resource_controller/decorators.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def self.wrap(decorator)
6767
def self.wrap!(parent, name)
6868
::Class.new parent do
6969
delegate :reorder, :page, :current_page, :total_pages, :limit_value,
70-
:total_count, :total_pages, :to_key, :group_values, :except,
71-
:find_each, :ransack
70+
:total_count, :total_pages, :offset, :to_key, :group_values,
71+
:except, :find_each, :ransack
7272

7373
define_singleton_method(:name) { name }
7474
end

lib/active_admin/views/components/paginated_collection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def build_pagination
9696
options[:params] = @params if @params
9797
options[:param_name] = @param_name if @param_name
9898

99-
if !@display_total && collection.respond_to?(:offset)
99+
if !@display_total
100100
# The #paginate method in kaminari will query the resource with a
101101
# count(*) to determine how many pages there should be unless
102102
# you pass in the :total_pages option. We issue a query to determine

spec/unit/views/components/paginated_collection_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ def paginated_collection(*args)
227227
end
228228
end
229229

230+
it "makes no expensive COUNT queries when pagination_total is false" do
231+
require 'db-query-matchers'
232+
233+
undecorated_collection = Post.all.page(1).per(30)
< 1241 /code>
234+
235+
expect { paginated_collection(undecorated_collection, pagination_total: false) }
236+
.not_to make_database_queries(matching: "SELECT COUNT(*) FROM \"posts\"")
237+
238+
decorated_collection = controller_with_decorator("index", PostDecorator).apply_collection_decorator(undecorated_collection)
239+
240+
expect { paginated_collection(decorated_collection, pagination_total: false) }
241+
.not_to make_database_queries(matching: "SELECT COUNT(*) FROM \"posts\"")
242+
end
243+
230244
context "when specifying per_page: array option" do
231245
let(:collection) do
232246
posts = 10.times.map { Post.new }

0 commit comments

Comments
 (0)
0