make datatable an object

This commit is contained in:
Ilion Beyst 2015-09-10 20:12:12 +02:00
parent fdf4e27a27
commit dd3c27c374
2 changed files with 73 additions and 47 deletions

View file

@ -1,57 +1,82 @@
module DataTable class DataTable
extend ActiveSupport::Concern def initialize user, params
@user = user
@params = sanitize_params(params)
@transactions = TransactionsQuery.new(@user)
@table = @transactions.arel_table
end
def query
@transactions.query
end
def predicates
end
def range_predicates name
col = @params[:columns][name]
[
(@table[:name].gteq(col[:lower]) if col[:lower]),
(@table[:name].lteq(col[:upper]) if col[:upper])
]
end
def json
#if columns[:time][:lower]
#query = query.where(table[:time].gteq(columns[:time][:lower]))
#end
#if columns[:time][:upper]
#query = query.where(table
def apply_filter(user, params)
params = sanitize_params(params)
selection = user.transactions
# filter time # filter time
lower = params[:columns][:time][:lower] #lower = params[:columns][:time][:lower]
upper = params[:columns][:time][:upper] #upper = params[:columns][:time][:upper]
if lower and upper #if lower and upper
selection = selection.where(created_at: lower..upper) #selection = selection.where(created_at: lower..upper)
elsif lower #elsif lower
selection = selection.where('created_at > :lower', lower: lower) #selection = selection.where('created_at > :lower', lower: lower)
elsif upper #elsif upper
selection = selection.where('created_at < :upper', upper: upper) #selection = selection.where('created_at < :upper', upper: upper)
end #end
# filter amount TODO this filters on absolute value ## filter amount TODO this filters on absolute value
lower = params[:columns][:amount][:lower] #lower = params[:columns][:amount][:lower]
upper = params[:columns][:amount][:upper] #upper = params[:columns][:amount][:upper]
if lower and upper #if lower and upper
selection = selection.where(amount: lower..upper) #selection = selection.where(amount: lower..upper)
elsif lower #elsif lower
selection = selection.where('amount > :lower', lower: lower) #selection = selection.where('amount > :lower', lower: lower)
elsif upper #elsif upper
selection = selection.where('amount < :upper', upper: upper) #selection = selection.where('amount < :upper', upper: upper)
end #end
# filter peer # TODO peer.name ## filter peer # TODO peer.name
peer = params[:columns][:peer][:value] #peer = params[:columns][:peer][:value]
if peer #if peer
selection = selection.where("(debtor_id = :id AND creditor_id LIKE :peer) OR (creditor_id = :id AND debtor_id LIKE :peer)", id: user.id, peer: "%#{peer}%") #selection = selection.where("(debtor_id = :id AND creditor_id LIKE :peer) OR (creditor_id = :id AND debtor_id LIKE :peer)", id: user.id, peer: "%#{peer}%")
end #end
# filter issuer # TODO issuer.name ## filter issuer # TODO issuer.name
issuer = params[:columns][:issuer][:value] #issuer = params[:columns][:issuer][:value]
if issuer #if issuer
selection = selection.where("issuer_id LIKE :re", re: "%#{issuer}%") #selection = selection.where("issuer_id LIKE :re", re: "%#{issuer}%")
end #end
# filter message ## filter message
message = params[:columns][:message][:value] #message = params[:columns][:message][:value]
if message #if message
selection = selection.where("message LIKE :re", re: "%#{message}%") #selection = selection.where("message LIKE :re", re: "%#{message}%")
end #end
selection_to_json(user, params[:draw], selection) ##selection_to_json(user, params[:draw], selection)
transactions = TransactionsQuery.new(@user) #query = transactions.query
query = transactions.query
selection = ActiveRecord::Base.connection.execute(query.to_sql).to_a selection = ActiveRecord::Base.connection.execute(query.to_sql).to_a
response_json(params[:draw], selection) response_json(@params[:draw], selection)
end end
private private
@ -86,7 +111,7 @@ module DataTable
def response_json(draw, selection) def response_json(draw, selection)
{ {
draw: draw, draw: draw,
recordsTotal: user.transactions.count, recordsTotal: @user.transactions.count,
recordsFiltered: selection.count, recordsFiltered: selection.count,
#data: selection.offset(params[:start]).take(params[:length]).map { |transaction| { #data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
#time: transaction.created_at, #time: transaction.created_at,

View file

@ -1,13 +1,14 @@
class UsersController < ApplicationController class UsersController < ApplicationController
load_and_authorize_resource load_and_authorize_resource
include DataTable
def show def show
@user = User.find(params[:id]) @user = User.find(params[:id])
respond_to do |format| respond_to do |format|
format.html format.html
format.json { render json: apply_filter(@user, params) } format.json do
datatable = DataTable.new(@user, params)
render json: datatable.json
end
end end
end end