Skip to content

Commit c0fa680

Browse files
author
Nicolas Rodriguez
committed
Extract methods, add tests
1 parent 442d874 commit c0fa680

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/ajax-datatables-rails/base.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def get_raw_records
3434
fail(NotImplemented, raw_records_error_text)
3535
end
3636

37-
def as_json(options = {})
37+
def as_json(*)
3838
{
39-
recordsTotal: get_raw_records.count(:all),
40-
recordsFiltered: get_raw_records.model.from("(#{filter_records(get_raw_records).except(:limit, :offset, :order).to_sql}) AS foo").count,
39+
recordsTotal: records_total_count,
40+
recordsFiltered: records_filtered_count,
4141
data: sanitize(data)
4242
}
4343
end
@@ -87,6 +87,14 @@ def retrieve_records
8787
records
8888
end
8989

90+
def records_total_count
91+
get_raw_records.count(:all)
92+
end
93+
94+
def records_filtered_count
95+
get_raw_records.model.from("(#{filter_records(get_raw_records).except(:limit, :offset, :order).to_sql}) AS foo").count
96+
end
97+
9098
# Private helper methods
9199
def load_orm_extension
92100
case config.orm

spec/ajax-datatables-rails/base_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,21 @@
8888
end
8989
end
9090
end
91+
92+
describe '#as_json' do
93+
it 'should return a hash' do
94+
datatable = ComplexDatatableHash.new(view)
95+
create_list(:user, 5)
96+
data = datatable.as_json
97+
expect(data[:recordsTotal]).to eq 5
98+
expect(data[:recordsFiltered]).to eq 5
99+
expect(data[:data]).to be_a(Array)
100+
expect(data[:data].size).to eq 5
101+
end
102+
end
91103
end
92104

105+
93106
context 'Private API' do
94107

95108
let(:view) { double('view', params: sample_params) }

0 commit comments

Comments
 (0)