Skip to content

Commit

Permalink
Improve variable naming and counting
Browse files Browse the repository at this point in the history
  • Loading branch information
voltechs committed Jan 31, 2017
1 parent b563c9c commit 0c24d32
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 14 additions & 7 deletions lib/data_tables/modules/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,42 @@ 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

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

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
Expand Down
22 changes: 11 additions & 11 deletions lib/data_tables/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
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
{ k => order }
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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/data_tables/responder/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module DataTables
module Responder
VERSION = '0.3.0'
VERSION = '0.3.1'
end
end

0 comments on commit 0c24d32

Please sign in to comment.