data_tables-responder 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1946685a7d2ed739b245b8c11ed085f976db2a6a
|
4
|
+
data.tar.gz: 3ae44f3298539852f0b441b7141bee76b3c0c74c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8c7f010af76b62d868ab3ff270495d3a010c8aa220008ed92122de9f227cb60317b9a5ed94ab8f448abbff16e1d878a5d131e17735b0245c5a71319a96462aa
|
7
|
+
data.tar.gz: e3228c8be84958dec33da231ec4496e0e316b10f1d0c08710c366100d027c10fc8e8a814063fa547e7b42c32a1bdea3dc48a73d7dbaae140cd058fd9c4301613
|
@@ -5,10 +5,11 @@ module DataTables
|
|
5
5
|
class Pagination
|
6
6
|
FIRST_PAGE = 1
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :original_scope, :filtered_scope, :context
|
9
9
|
|
10
|
-
def initialize(
|
11
|
-
@
|
10
|
+
def initialize(original_scope, filtered_scope, request_parameters)
|
11
|
+
@original_scope = original_scope
|
12
|
+
@filtered_scope = filtered_scope
|
12
13
|
@request_parameters = request_parameters
|
13
14
|
end
|
14
15
|
|
@@ -16,12 +17,12 @@ module DataTables
|
|
16
17
|
start = (@request_parameters[:start] || '0').to_i
|
17
18
|
length = (@request_parameters[:length] || '10').to_i
|
18
19
|
page = (start / length) + 1
|
19
|
-
@
|
20
|
+
@filtered_scope = @filtered_scope.paginate(page: page, per_page: length, total_entries: records_total)
|
20
21
|
end
|
21
22
|
|
22
23
|
def as_json
|
23
24
|
{
|
24
|
-
recordsTotal: @
|
25
|
+
recordsTotal: @filtered_scope&.total_entries&.to_i,
|
25
26
|
recordsFiltered: records_filtered&.to_i
|
26
27
|
}
|
27
28
|
end
|
@@ -29,11 +30,17 @@ module DataTables
|
|
29
30
|
protected
|
30
31
|
|
31
32
|
def records_total
|
32
|
-
|
33
|
+
# TODO: Check threshold
|
34
|
+
count_estimate = @original_scope&.model&.all&.count_estimate.to_i
|
35
|
+
if count_estimate < 1_000_000
|
36
|
+
count_estimate = @original_scope&.count
|
37
|
+
end
|
38
|
+
|
39
|
+
count_estimate
|
33
40
|
end
|
34
41
|
|
35
42
|
def records_filtered
|
36
|
-
@
|
43
|
+
@filtered_scope&.unscope(:limit, :offset)&.count_estimate
|
37
44
|
end
|
38
45
|
|
39
46
|
attr_reader :adapter_options
|
@@ -9,17 +9,17 @@ require 'data_tables/responder/railtie' if defined? ::Rails::Railtie
|
|
9
9
|
module DataTables
|
10
10
|
module Responder
|
11
11
|
|
12
|
-
def self.respond(
|
13
|
-
model =
|
12
|
+
def self.respond(original_scope, params)
|
13
|
+
model = original_scope.try(:model) || original_scope
|
14
14
|
|
15
|
-
|
15
|
+
filtered_results = original_scope&.dup || model.none
|
16
16
|
hashed_orders = transmute_datatable_order(params[:order], params[:columns])
|
17
17
|
orders = flat_keys_to_nested hashed_orders
|
18
18
|
|
19
19
|
order_by = orders.collect do |k, order|
|
20
20
|
if order.is_a? Hash
|
21
21
|
if (klass = model.reflect_on_association(k).try(:klass))
|
22
|
-
|
22
|
+
filtered_results = filtered_results.joins(k)
|
23
23
|
klass.arel_table[order.first.first].send(order.first.last)
|
24
24
|
end
|
25
25
|
else
|
@@ -27,11 +27,11 @@ module DataTables
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
filtered_results = search(filtered_results, params)
|
31
31
|
|
32
32
|
# Rails.logger.warn "SEARCH BY: #{search_by}"
|
33
|
-
|
34
|
-
|
33
|
+
filtered_results = order_by.inject(filtered_results) { |r, o| r.order(o) }
|
34
|
+
filtered_results = paginate(original_scope, filtered_results, params)
|
35
35
|
end
|
36
36
|
|
37
37
|
def self.flat_keys_to_nested(hash)
|
@@ -43,12 +43,12 @@ module DataTables
|
|
43
43
|
end
|
44
44
|
|
45
45
|
|
46
|
-
def self.paginate(
|
47
|
-
Modules::Pagination.new(
|
46
|
+
def self.paginate(original_scope, filtered_results, params)
|
47
|
+
Modules::Pagination.new(original_scope, filtered_results, params).paginate
|
48
48
|
end
|
49
49
|
|
50
|
-
def self.search(
|
51
|
-
Modules::Search.new(
|
50
|
+
def self.search(filtered_results, params)
|
51
|
+
Modules::Search.new(filtered_results, params).search
|
52
52
|
end
|
53
53
|
|
54
54
|
def self.transmute_datatable_order(orders, columns)
|