diff --git a/lib/ajax-datatables-rails/base.rb b/lib/ajax-datatables-rails/base.rb index 83dc206..58fbd6e 100644 --- a/lib/ajax-datatables-rails/base.rb +++ b/lib/ajax-datatables-rails/base.rb @@ -139,11 +139,15 @@ def retrieve_records end def records_total_count - fetch_records.count(:all) + numeric_count(fetch_records.count(:all)) end def records_filtered_count - filter_records(fetch_records).count(:all) + numeric_count(filter_records(fetch_records).count(:all)) + end + + def numeric_count(count) + count.is_a?(Hash) ? count.values.size : count end def global_search_delimiter diff --git a/spec/ajax-datatables-rails/orm/active_record_count_records_spec.rb b/spec/ajax-datatables-rails/orm/active_record_count_records_spec.rb new file mode 100644 index 0000000..4c5c2be --- /dev/null +++ b/spec/ajax-datatables-rails/orm/active_record_count_records_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe AjaxDatatablesRails::ORM::ActiveRecord do + + let(:datatable) { ComplexDatatable.new(sample_params) } + let(:records) { User.all } + + describe '#records_total_count' do + context 'ungrouped results' do + it 'returns the count' do + expect(datatable.send(:records_total_count)).to eq records.count + end + end + + context 'grouped results' do + let(:datatable) { GroupedDatatable.new(sample_params) } + + it 'returns the count' do + expect(datatable.send(:records_total_count)).to eq records.count + end + end + end + + + describe '#records_filtered_count' do + context 'ungrouped results' do + it 'returns the count' do + expect(datatable.send(:records_filtered_count)).to eq records.count + end + end + + context 'grouped results' do + let(:datatable) { GroupedDatatable.new(sample_params) } + + it 'returns the count' do + expect(datatable.send(:records_filtered_count)).to eq records.count + end + end + end +end diff --git a/spec/support/datatables/grouped_datatable_array.rb b/spec/support/datatables/grouped_datatable_array.rb new file mode 100644 index 0000000..e4547ad --- /dev/null +++ b/spec/support/datatables/grouped_datatable_array.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class GroupedDatatable < ComplexDatatable + + def get_raw_records + User.all.group(:id) + end +end