diff --git a/lib/data_tables/active_model_serializers/adapter/pagination.rb b/lib/data_tables/active_model_serializers/adapter/pagination.rb index f4a5fa4..ad2d8f9 100644 --- a/lib/data_tables/active_model_serializers/adapter/pagination.rb +++ b/lib/data_tables/active_model_serializers/adapter/pagination.rb @@ -12,7 +12,7 @@ def initialize(serializer) def as_h { recordsTotal: collection&.total_entries&.to_i, - recordsFiltered: @collection&.unscope(:limit, :offset)&.count_estimate&.to_i + recordsFiltered: collection&.unscope(:limit, :offset)&.count_estimate&.to_i } end diff --git a/lib/data_tables/modules/pagination.rb b/lib/data_tables/modules/pagination.rb index 5a65fb6..57e1eb4 100644 --- a/lib/data_tables/modules/pagination.rb +++ b/lib/data_tables/modules/pagination.rb @@ -5,10 +5,11 @@ module Modules class Pagination FIRST_PAGE = 1 - attr_reader :collection, :context + attr_reader :original_scope, :filtered_scope, :context - def initialize(collection, request_parameters) - @collection = collection + def initialize(original_scope, filtered_scope, request_parameters) + @original_scope = original_scope + @filtered_scope = filtered_scope @request_parameters = request_parameters end @@ -16,12 +17,12 @@ def paginate start = (@request_parameters[:start] || '0').to_i length = (@request_parameters[:length] || '10').to_i page = (start / length) + 1 - @collection = @collection.paginate(page: page, per_page: length, total_entries: records_total) + @filtered_scope = @filtered_scope.paginate(page: page, per_page: length, total_entries: records_total) end def as_json { - recordsTotal: @collection&.total_entries&.to_i, + recordsTotal: @filtered_scope&.total_entries&.to_i, recordsFiltered: records_filtered&.to_i } end @@ -29,11 +30,17 @@ def as_json protected def records_total - @collection&.model&.all.count_estimate + # TODO: Check threshold + count_estimate = @original_scope&.model&.all&.count_estimate.to_i + if count_estimate < 1_000_000 + count_estimate = @original_scope&.count + end + + count_estimate end def records_filtered - @collection&.unscope(:limit, :offset)&.count_estimate + @filtered_scope&.unscope(:limit, :offset)&.count_estimate end attr_reader :adapter_options diff --git a/lib/data_tables/responder.rb b/lib/data_tables/responder.rb index a79fc6d..d6f1f9d 100644 --- a/lib/data_tables/responder.rb +++ b/lib/data_tables/responder.rb @@ -9,17 +9,17 @@ module DataTables module Responder - def self.respond(resource, params) - model = resource.try(:model) || resource + def self.respond(original_scope, params) + model = original_scope.try(:model) || original_scope - results = resource + filtered_results = original_scope&.dup || model.none hashed_orders = transmute_datatable_order(params[:order], params[:columns]) orders = flat_keys_to_nested hashed_orders order_by = orders.collect do |k, order| if order.is_a? Hash if (klass = model.reflect_on_association(k).try(:klass)) - results = results.joins(k) + filtered_results = filtered_results.joins(k) klass.arel_table[order.first.first].send(order.first.last) end else @@ -27,11 +27,11 @@ def self.respond(resource, params) end end - results = search(results, params) + filtered_results = search(filtered_results, params) # Rails.logger.warn "SEARCH BY: #{search_by}" - results = order_by.inject(results) { |r, o| r.order(o) } - results = paginate(results, params) + filtered_results = order_by.inject(filtered_results) { |r, o| r.order(o) } + filtered_results = paginate(original_scope, filtered_results, params) end def self.flat_keys_to_nested(hash) @@ -43,12 +43,12 @@ def self.flat_keys_to_nested(hash) end - def self.paginate(collection, params) - Modules::Pagination.new(collection, params).paginate + def self.paginate(original_scope, filtered_results, params) + Modules::Pagination.new(original_scope, filtered_results, params).paginate end - def self.search(collection, params) - Modules::Search.new(collection, params).search + def self.search(filtered_results, params) + Modules::Search.new(filtered_results, params).search end def self.transmute_datatable_order(orders, columns) diff --git a/lib/data_tables/responder/version.rb b/lib/data_tables/responder/version.rb index 9485152..48ee92c 100644 --- a/lib/data_tables/responder/version.rb +++ b/lib/data_tables/responder/version.rb @@ -1,5 +1,5 @@ module DataTables module Responder - VERSION = '0.3.0' + VERSION = '0.3.1' end end