Skip to content

Commit febe5ba

Browse files
author
Dave Morehouse
committed
Add support for grouped results
1 parent eae8844 commit febe5ba

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

lib/ajax-datatables-rails/base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,15 @@ def retrieve_records
137137
end
138138

139139
def records_total_count
140-
fetch_records.count(:all)
140+
numeric_count(fetch_records.count(:all))
141141
end
142142

143143
def records_filtered_count
144-
filter_records(fetch_records).count(:all)
144+
numeric_count(filter_records(fetch_records).count(:all))
145+
end
146+
147+
def numeric_count(count)
148+
count.is_a?(Hash) ? count.values.size : count
145149
end
146150

147151
def global_search_delimiter
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe AjaxDatatablesRails::ORM::ActiveRecord do
6+
7+
let(:datatable) { ComplexDatatable.new(sample_params) }
8+
let(:records) { User.all }
9+
10+
describe '#records_total_count' do
11+
context 'ungrouped results' do
12+
it 'returns the count' do
13+
expect(datatable.send(:records_total_count)).to eq records.count
14+
end
15+
end
16+
17+
context 'grouped results' do
18+
let(:datatable) { GroupedDatatable.new(sample_params) }
19+
20+
it 'returns the count' do
21+
expect(datatable.send(:records_total_count)).to eq records.count
22+
end
23+
end
24+
end
25+
26+
27+
describe '#records_filtered_count' do
28+
context 'ungrouped results' do
29+
it 'returns the count' do
30+
expect(datatable.send(:records_filtered_count)).to eq records.count
31+
end
32+
end
33+
34+
context 'grouped results' do
35+
let(:datatable) { GroupedDatatable.new(sample_params) }
36+
37+
it 'returns the count' do
38+
expect(datatable.send(:records_filtered_count)).to eq records.count
39+
end
40+
end
41+
end
42+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class GroupedDatatable < ComplexDatatable
4+
5+
def get_raw_records
6+
User.all.group(:id)
7+
end
8+
end

0 commit comments

Comments
 (0)