Skip to content

Commit

Permalink
Add support for grouped results
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Morehouse committed Apr 19, 2023
1 parent eae8844 commit febe5ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/ajax-datatables-rails/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,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
Expand Down
42 changes: 42 additions & 0 deletions spec/ajax-datatables-rails/orm/active_record_count_records_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/support/datatables/grouped_datatable_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class GroupedDatatable < ComplexDatatable

def get_raw_records
User.all.group(:id)
end
end

0 comments on commit febe5ba

Please sign in to comment.