From aa458e1a000a1b3c8a11e3265487d1e9072cb923 Mon Sep 17 00:00:00 2001 From: "M.Shibuya" Date: Tue, 3 Nov 2015 20:56:53 +0900 Subject: [PATCH] #count breaks when default-scoped with select. Closes #2129, Fixes #2447 --- lib/rails_admin/adapters/active_record.rb | 2 +- .../adapters/active_record_spec.rb | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/rails_admin/adapters/active_record.rb b/lib/rails_admin/adapters/active_record.rb index c4e50824d7..099f682f1c 100644 --- a/lib/rails_admin/adapters/active_record.rb +++ b/lib/rails_admin/adapters/active_record.rb @@ -40,7 +40,7 @@ def all(options = {}, scope = nil) end def count(options = {}, scope = nil) - all(options.merge(limit: false, page: false), scope).count + all(options.merge(limit: false, page: false), scope).count(:all) end def destroy(objects) diff --git a/spec/rails_admin/adapters/active_record_spec.rb b/spec/rails_admin/adapters/active_record_spec.rb index 328d489273..4bd46ff6ec 100644 --- a/spec/rails_admin/adapters/active_record_spec.rb +++ b/spec/rails_admin/adapters/active_record_spec.rb @@ -47,8 +47,24 @@ expect(@players).to include abstract_model.first end - it '#count returns count of items' do - expect(abstract_model.count).to eq(@players.count) + describe '#count' do + it 'returns count of items' do + expect(abstract_model.count).to eq(@players.count) + end + + context 'when default-scoped with select' do + before do + class PlayerWithDefaultScope < Player + self.table_name = 'players' + default_scope { select(:id, :name) } + end + end + let(:abstract_model) { RailsAdmin::AbstractModel.new('PlayerWithDefaultScope') } + + it 'does not break' do + expect(abstract_model.count).to eq(@players.count) + end + end end it '#destroy destroys multiple items' do