2015-09-10 20:12:12 +02:00
|
|
|
class DataTable
|
2016-03-21 11:54:32 +01:00
|
|
|
include ActionView::Helpers::JavaScriptHelper
|
|
|
|
include ERB::Util
|
2015-09-10 20:12:12 +02:00
|
|
|
def initialize user, params
|
|
|
|
@user = user
|
|
|
|
@params = sanitize_params(params)
|
|
|
|
@transactions = TransactionsQuery.new(@user)
|
|
|
|
@table = @transactions.arel_table
|
|
|
|
end
|
|
|
|
|
2015-09-10 21:09:57 +02:00
|
|
|
def json
|
2015-09-11 12:00:49 +02:00
|
|
|
{
|
|
|
|
draw: @params[:draw],
|
|
|
|
recordsTotal: @user.transactions.count,
|
|
|
|
recordsFiltered: count,
|
2016-03-21 11:54:32 +01:00
|
|
|
data: data.map { |d| (d["message"] = json_escape(d["message"])) && d }
|
2015-09-11 12:00:49 +02:00
|
|
|
}
|
2015-09-10 21:09:57 +02:00
|
|
|
end
|
2015-09-11 12:00:49 +02:00
|
|
|
private
|
2015-09-10 21:09:57 +02:00
|
|
|
def data
|
2015-09-11 20:03:04 +02:00
|
|
|
run_query(paginated_query.project(Arel.star))
|
2015-09-11 12:00:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def count
|
2015-09-11 20:03:04 +02:00
|
|
|
run_query(query.project(Arel.star.count)).first["COUNT(*)"]
|
2015-09-11 12:00:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def paginated_query
|
|
|
|
query.skip(@params[:start]).take(@params[:length])
|
2015-09-10 21:09:57 +02:00
|
|
|
end
|
|
|
|
|
2015-09-10 20:12:12 +02:00
|
|
|
def query
|
2015-09-11 15:50:55 +02:00
|
|
|
q = @transactions.query.order(@table[:time].desc)
|
2015-09-11 12:07:39 +02:00
|
|
|
q.where(predicate) if predicate
|
|
|
|
q
|
2015-09-10 20:12:12 +02:00
|
|
|
end
|
|
|
|
|
2015-09-11 12:00:49 +02:00
|
|
|
def predicate
|
|
|
|
@predicate ||= [
|
|
|
|
*range_predicates(:amount),
|
2015-09-10 21:09:57 +02:00
|
|
|
*range_predicates(:time),
|
|
|
|
eq_predicate(:peer),
|
|
|
|
eq_predicate(:issuer),
|
|
|
|
like_predicate(:message)
|
|
|
|
].compact.inject { |l, r| l.and(r) }
|
2015-09-10 20:12:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def range_predicates name
|
|
|
|
col = @params[:columns][name]
|
|
|
|
[
|
2015-09-11 10:46:03 +02:00
|
|
|
(@table[name].gteq(col[:lower]) if col[:lower]),
|
|
|
|
(@table[name].lteq(col[:upper]) if col[:upper])
|
2015-09-10 20:12:12 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2015-09-10 21:09:57 +02:00
|
|
|
def eq_predicate name
|
2015-09-11 10:46:03 +02:00
|
|
|
value = @params[:columns][name][:value]
|
|
|
|
@table[name].eq(value) if value
|
2015-09-10 21:09:57 +02:00
|
|
|
end
|
2015-09-10 19:41:47 +02:00
|
|
|
|
2015-09-10 21:09:57 +02:00
|
|
|
def like_predicate name
|
2015-09-11 10:46:03 +02:00
|
|
|
value = @params[:columns][name][:value]
|
|
|
|
@table[name].matches("%#{value}%") if value
|
2015-09-09 14:46:06 +02:00
|
|
|
end
|
|
|
|
|
2015-09-11 12:00:49 +02:00
|
|
|
def run_query query
|
2015-09-11 20:03:04 +02:00
|
|
|
ActiveRecord::Base.connection.exec_query(query.to_sql)
|
2015-09-11 12:00:49 +02:00
|
|
|
end
|
2015-09-09 14:46:06 +02:00
|
|
|
|
2015-09-09 21:09:07 +02:00
|
|
|
def sanitize_params(params)
|
|
|
|
# Parsing according to https://datatables.net/manual/server-side
|
|
|
|
clean = {
|
|
|
|
draw: params.require(:draw).to_i,
|
|
|
|
start: params.require(:start).to_i,
|
|
|
|
length: params.require(:length).to_i,
|
|
|
|
columns: Hash.new
|
|
|
|
}
|
|
|
|
params.require(:columns).each do |i, column|
|
2015-09-11 11:55:26 +02:00
|
|
|
type, *values = column.require(:search)[:value].split(':')
|
|
|
|
value = values.join(':') unless values.empty?
|
2015-09-09 21:09:07 +02:00
|
|
|
h = clean[:columns][column.require(:data).to_sym] = {
|
|
|
|
name: column[:name],
|
|
|
|
searchable: column[:searchable] == 'true',
|
|
|
|
orderable: column[:orderable] == 'true',
|
|
|
|
type: type
|
|
|
|
}
|
|
|
|
if type == 'number-range'
|
2015-09-11 11:55:26 +02:00
|
|
|
h[:lower], h[:upper] = value.split('~').map do |euros|
|
2015-09-11 14:24:20 +02:00
|
|
|
(Float(euros) * 100).to_i rescue nil
|
2015-09-11 11:55:26 +02:00
|
|
|
end
|
2015-09-09 21:09:07 +02:00
|
|
|
elsif type == 'date-range'
|
2015-09-11 11:55:26 +02:00
|
|
|
h[:lower], h[:upper] = value.split('~').map do |string|
|
|
|
|
string.to_datetime rescue nil
|
|
|
|
end
|
2015-09-09 21:09:07 +02:00
|
|
|
else
|
|
|
|
h[:value] = value
|
2015-09-09 17:14:06 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-09 21:09:07 +02:00
|
|
|
return clean
|
2015-09-09 17:14:06 +02:00
|
|
|
end
|
2015-09-09 14:46:06 +02:00
|
|
|
end
|
|
|
|
|