Skip to content

Commit

Permalink
Merge pull request #419 from dmorehouse/grouped_counts
Browse files Browse the repository at this point in the history
Add support for grouped results
  • Loading branch information
n-rodriguez authored Apr 19, 2023
2 parents b541799 + febe5ba commit c20adb0
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 @@ -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
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 c20adb0

Please sign in to comment.