implement filters for datatable

This commit is contained in:
Ilion Beyst 2015-09-10 21:09:57 +02:00
parent dd3c27c374
commit 25c3bdd0ca

View file

@ -1,4 +1,5 @@
class DataTable class DataTable
def initialize user, params def initialize user, params
@user = user @user = user
@params = sanitize_params(params) @params = sanitize_params(params)
@ -6,11 +7,28 @@ class DataTable
@table = @transactions.arel_table @table = @transactions.arel_table
end end
def json
response_json(@params[:draw], data)
end
def data
ActiveRecord::Base.connection.execute(self.query.to_sql)
end
def query def query
@transactions.query pred = predicates
q = @transactions.query
q = q.where(pred) if pred
q
end end
def predicates def predicates
[ *range_predicates(:amount),
*range_predicates(:time),
eq_predicate(:peer),
eq_predicate(:issuer),
like_predicate(:message)
].compact.inject { |l, r| l.and(r) }
end end
def range_predicates name def range_predicates name
@ -21,62 +39,14 @@ class DataTable
] ]
end end
def json def eq_predicate name
#if columns[:time][:lower] value = @params[:columns][:name][:value]
#query = query.where(table[:time].gteq(columns[:time][:lower])) @table[:name].eq(value) if value
#end end
#if columns[:time][:upper] def like_predicate name
#query = query.where(table value = @params[:columns][:name][:value]
@table[:name].matches("%#{value}%") if value
# 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
## 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 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
##selection_to_json(user, params[:draw], selection)
#query = transactions.query
selection = ActiveRecord::Base.connection.execute(query.to_sql).to_a
response_json(@params[:draw], selection)
end end
private private