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: c9e0c1d6257f60b5b07b2e17d6dcbc1ca2fdc39c
4
- data.tar.gz: 0b559fd04b9cc9fce90cf40fda777e63f6c942bc
3
+ metadata.gz: 1946685a7d2ed739b245b8c11ed085f976db2a6a
4
+ data.tar.gz: 3ae44f3298539852f0b441b7141bee76b3c0c74c
5
5
  SHA512:
6
- metadata.gz: 7eb090ae3ef6f7968594f31b28161cbd5e527078ed53da634358eb20b1a5a9e3434a4cbe69b53bf9193a2a227bb7045f350f23b2183d717249e96dc67fbfe68c
7
- data.tar.gz: 81ab0c4502f9b29b793b5ab514056622580862fe036483d8b65773734c9eff9c61e79ce5a87e4e5e93529c7273d2b2805441fb0124f1a263aae2ea48565cba14
6
+ metadata.gz: c8c7f010af76b62d868ab3ff270495d3a010c8aa220008ed92122de9f227cb60317b9a5ed94ab8f448abbff16e1d878a5d131e17735b0245c5a71319a96462aa
7
+ data.tar.gz: e3228c8be84958dec33da231ec4496e0e316b10f1d0c08710c366100d027c10fc8e8a814063fa547e7b42c32a1bdea3dc48a73d7dbaae140cd058fd9c4301613
@@ -12,7 +12,7 @@ module DataTables
12
12
  def as_h
13
13
  {
14
14
  recordsTotal: collection&.total_entries&.to_i,
15
- recordsFiltered: @collection&.unscope(:limit, :offset)&.count_estimate&.to_i
15
+ recordsFiltered: collection&.unscope(:limit, :offset)&.count_estimate&.to_i
16
16
  }
17
17
  end
18
18
 
@@ -5,10 +5,11 @@ module DataTables
5
5
  class Pagination
6
6
  FIRST_PAGE = 1
7
7
 
8
- attr_reader :collection, :context
8
+ attr_reader :original_scope, :filtered_scope, :context
9
9
 
10
- def initialize(collection, request_parameters)
11
- @collection = collection
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
- @collection = @collection.paginate(page: page, per_page: length, total_entries: records_total)
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: @collection&.total_entries&.to_i,
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
- @collection&.model&.all.count_estimate
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
- @collection&.unscope(:limit, :offset)&.count_estimate
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(resource, params)
13
- model = resource.try(:model) || resource
12
+ def self.respond(original_scope, params)
13
+ model = original_scope.try(:model) || original_scope
14
14
 
15
- results = resource
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
- results = results.joins(k)
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
- results = search(results, params)
30
+ filtered_results = search(filtered_results, params)
31
31
 
32
32
  # Rails.logger.warn "SEARCH BY: #{search_by}"
33
- results = order_by.inject(results) { |r, o| r.order(o) }
34
- results = paginate(results, params)
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(collection, params)
47
- Modules::Pagination.new(collection, params).paginate
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(collection, params)
51
- Modules::Search.new(collection, params).search
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)
@@ -1,5 +1,5 @@
1
1
  module DataTables
2
2
  module Responder
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_tables-responder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens