make datatable an object
This commit is contained in:
parent
fdf4e27a27
commit
dd3c27c374
2 changed files with 73 additions and 47 deletions
|
@ -1,57 +1,82 @@
|
|||
module DataTable
|
||||
extend ActiveSupport::Concern
|
||||
class DataTable
|
||||
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
|
||||
lower = params[:columns][:time][:lower]
|
||||
upper = params[:columns][:time][:upper]
|
||||
if lower and upper
|
||||
selection = selection.where(created_at: lower..upper)
|
||||
elsif lower
|
||||
selection = selection.where('created_at > :lower', lower: lower)
|
||||
elsif upper
|
||||
selection = selection.where('created_at < :upper', upper: upper)
|
||||
end
|
||||
#lower = params[:columns][:time][:lower]
|
||||
#upper = params[:columns][:time][:upper]
|
||||
#if lower and upper
|
||||
#selection = selection.where(created_at: lower..upper)
|
||||
#elsif lower
|
||||
#selection = selection.where('created_at > :lower', lower: lower)
|
||||
#elsif upper
|
||||
#selection = selection.where('created_at < :upper', upper: upper)
|
||||
#end
|
||||
|
||||
# filter amount TODO this filters on absolute value
|
||||
lower = params[:columns][:amount][:lower]
|
||||
upper = params[:columns][:amount][:upper]
|
||||
if lower and upper
|
||||
selection = selection.where(amount: lower..upper)
|
||||
elsif lower
|
||||
selection = selection.where('amount > :lower', lower: lower)
|
||||
elsif upper
|
||||
selection = selection.where('amount < :upper', upper: upper)
|
||||
end
|
||||
## filter amount TODO this filters on absolute value
|
||||
#lower = params[:columns][:amount][:lower]
|
||||
#upper = params[:columns][:amount][:upper]
|
||||
#if lower and upper
|
||||
#selection = selection.where(amount: lower..upper)
|
||||
#elsif lower
|
||||
#selection = selection.where('amount > :lower', lower: lower)
|
||||
#elsif upper
|
||||
#selection = selection.where('amount < :upper', upper: upper)
|
||||
#end
|
||||
|
||||
# filter peer # TODO peer.name
|
||||
peer = params[:columns][:peer][:value]
|
||||
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}%")
|
||||
end
|
||||
## filter peer # TODO peer.name
|
||||
#peer = params[:columns][:peer][:value]
|
||||
#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}%")
|
||||
#end
|
||||
|
||||
# filter issuer # TODO issuer.name
|
||||
issuer = params[:columns][:issuer][:value]
|
||||
if issuer
|
||||
selection = selection.where("issuer_id LIKE :re", re: "%#{issuer}%")
|
||||
end
|
||||
## filter issuer # TODO issuer.name
|
||||
#issuer = params[:columns][:issuer][:value]
|
||||
#if issuer
|
||||
#selection = selection.where("issuer_id LIKE :re", re: "%#{issuer}%")
|
||||
#end
|
||||
|
||||
# filter message
|
||||
message = params[:columns][:message][:value]
|
||||
if message
|
||||
selection = selection.where("message LIKE :re", re: "%#{message}%")
|
||||
end
|
||||
## filter message
|
||||
#message = params[:columns][:message][:value]
|
||||
#if message
|
||||
#selection = selection.where("message LIKE :re", re: "%#{message}%")
|
||||
#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
|
||||
response_json(params[:draw], selection)
|
||||
response_json(@params[:draw], selection)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -86,7 +111,7 @@ module DataTable
|
|||
def response_json(draw, selection)
|
||||
{
|
||||
draw: draw,
|
||||
recordsTotal: user.transactions.count,
|
||||
recordsTotal: @user.transactions.count,
|
||||
recordsFiltered: selection.count,
|
||||
#data: selection.offset(params[:start]).take(params[:length]).map { |transaction| {
|
||||
#time: transaction.created_at,
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
class UsersController < ApplicationController
|
||||
load_and_authorize_resource
|
||||
|
||||
include DataTable
|
||||
|
||||
def show
|
||||
@user = User.find(params[:id])
|
||||
respond_to do |format|
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue