implement pagination

This commit is contained in:
Ilion Beyst 2015-09-11 12:00:49 +02:00
parent d336e778f7
commit bf9a845780
2 changed files with 27 additions and 26 deletions

View file

@ -8,22 +8,38 @@ class DataTable
end end
def json def json
response_json(@params[:draw], data) {
draw: @params[:draw],
recordsTotal: @user.transactions.count,
recordsFiltered: count,
data: data
}
end end
private
def data def data
ActiveRecord::Base.connection.execute(self.query.to_sql) run_query(paginated_query.project(Arel.star))
end
def count
run_query(query.project(Arel.star.count)).first[0]
end
def paginated_query
query.skip(@params[:start]).take(@params[:length])
end end
def query def query
pred = predicates return @query if @query # Do not build query twice
q = @transactions.query @query = @transactions.query
q = q.where(pred) if pred @query.where(predicate) if predicate
q @query
end end
def predicates def predicate
[ *range_predicates(:amount), @predicate ||= [
*range_predicates(:amount),
*range_predicates(:time), *range_predicates(:time),
eq_predicate(:peer), eq_predicate(:peer),
eq_predicate(:issuer), eq_predicate(:issuer),
@ -49,7 +65,9 @@ class DataTable
@table[name].matches("%#{value}%") if value @table[name].matches("%#{value}%") if value
end end
private def run_query query
ActiveRecord::Base.connection.execute(query.to_sql)
end
def sanitize_params(params) def sanitize_params(params)
# Parsing according to https://datatables.net/manual/server-side # Parsing according to https://datatables.net/manual/server-side
@ -82,21 +100,5 @@ class DataTable
end end
return clean return clean
end end
def response_json(draw, selection)
{
draw: draw,
recordsTotal: @user.transactions.count,
recordsFiltered: selection.count,
#data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
#time: transaction.created_at,
#amount: transaction.signed_amount_for(user),
#peer: transaction.peer_of(user).try(:name),
#issuer: transaction.issuer.name,
#message: transaction.message,
#}}
data: selection
}
end
end end

View file

@ -12,7 +12,6 @@ class TransactionsQuery
def query def query
Arel::SelectManager.new(ActiveRecord::Base) Arel::SelectManager.new(ActiveRecord::Base)
.from(arel) .from(arel)
.project(Arel.star)
end end
def arel def arel