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
def json
response_json(@params[:draw], data)
{
draw: @params[:draw],
recordsTotal: @user.transactions.count,
recordsFiltered: count,
data: data
}
end
private
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
def query
pred = predicates
q = @transactions.query
q = q.where(pred) if pred
q
return @query if @query # Do not build query twice
@query = @transactions.query
@query.where(predicate) if predicate
@query
end
def predicates
[ *range_predicates(:amount),
def predicate
@predicate ||= [
*range_predicates(:amount),
*range_predicates(:time),
eq_predicate(:peer),
eq_predicate(:issuer),
@ -49,7 +65,9 @@ class DataTable
@table[name].matches("%#{value}%") if value
end
private
def run_query query
ActiveRecord::Base.connection.execute(query.to_sql)
end
def sanitize_params(params)
# Parsing according to https://datatables.net/manual/server-side
@ -82,21 +100,5 @@ class DataTable
end
return clean
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

View file

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