Merge branch 'filter-datatables' of https://github.com/ZeusWPI/Tab into filter-datatables
This commit is contained in:
commit
8d472c079b
2 changed files with 34 additions and 29 deletions
|
@ -8,22 +8,37 @@ class DataTable
|
||||||
end
|
end
|
||||||
|
|
||||||
def json
|
def json
|
||||||
response_json(@params[:draw], data)
|
{
|
||||||
|
draw: @params[:draw],
|
||||||
|
recordsTotal: @user.transactions.count,
|
||||||
|
recordsFiltered: count,
|
||||||
|
data: data
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def data
|
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
|
end
|
||||||
|
|
||||||
def query
|
def query
|
||||||
pred = predicates
|
|
||||||
q = @transactions.query
|
q = @transactions.query
|
||||||
q = q.where(pred) if pred
|
q.where(predicate) if predicate
|
||||||
q
|
q
|
||||||
end
|
end
|
||||||
|
|
||||||
def predicates
|
def predicate
|
||||||
[ *range_predicates(:amount),
|
@predicate ||= [
|
||||||
|
*range_predicates(:amount),
|
||||||
*range_predicates(:time),
|
*range_predicates(:time),
|
||||||
eq_predicate(:peer),
|
eq_predicate(:peer),
|
||||||
eq_predicate(:issuer),
|
eq_predicate(:issuer),
|
||||||
|
@ -49,7 +64,9 @@ class DataTable
|
||||||
@table[name].matches("%#{value}%") if value
|
@table[name].matches("%#{value}%") if value
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def run_query query
|
||||||
|
ActiveRecord::Base.connection.execute(query.to_sql)
|
||||||
|
end
|
||||||
|
|
||||||
def sanitize_params(params)
|
def sanitize_params(params)
|
||||||
# Parsing according to https://datatables.net/manual/server-side
|
# Parsing according to https://datatables.net/manual/server-side
|
||||||
|
@ -60,7 +77,8 @@ class DataTable
|
||||||
columns: Hash.new
|
columns: Hash.new
|
||||||
}
|
}
|
||||||
params.require(:columns).each do |i, column|
|
params.require(:columns).each do |i, column|
|
||||||
type, value = column.require(:search)[:value].split(':')
|
type, *values = column.require(:search)[:value].split(':')
|
||||||
|
value = values.join(':') unless values.empty?
|
||||||
h = clean[:columns][column.require(:data).to_sym] = {
|
h = clean[:columns][column.require(:data).to_sym] = {
|
||||||
name: column[:name],
|
name: column[:name],
|
||||||
searchable: column[:searchable] == 'true',
|
searchable: column[:searchable] == 'true',
|
||||||
|
@ -68,30 +86,18 @@ class DataTable
|
||||||
type: type
|
type: type
|
||||||
}
|
}
|
||||||
if type == 'number-range'
|
if type == 'number-range'
|
||||||
h[:lower], h[:upper] = value.split('~').map &:to_i
|
h[:lower], h[:upper] = value.split('~').map do |euros|
|
||||||
|
(euros.to_f * 100).to_i rescue nil
|
||||||
|
end
|
||||||
elsif type == 'date-range'
|
elsif type == 'date-range'
|
||||||
h[:lower], h[:upper] = value.split('~').map &:to_datetime
|
h[:lower], h[:upper] = value.split('~').map do |string|
|
||||||
|
string.to_datetime rescue nil
|
||||||
|
end
|
||||||
else
|
else
|
||||||
h[:value] = value
|
h[:value] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return clean
|
return clean
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ class TransactionsQuery
|
||||||
def query
|
def query
|
||||||
Arel::SelectManager.new(ActiveRecord::Base)
|
Arel::SelectManager.new(ActiveRecord::Base)
|
||||||
.from(arel)
|
.from(arel)
|
||||||
.project(Arel.star)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def arel
|
def arel
|
||||||
|
@ -61,10 +60,10 @@ class TransactionsQuery
|
||||||
def incoming
|
def incoming
|
||||||
@user.incoming_transactions.arel
|
@user.incoming_transactions.arel
|
||||||
@transactions
|
@transactions
|
||||||
.where(@transactions[:debtor_id].eq(@user.id))
|
.where(@transactions[:creditor_id].eq(@user.id))
|
||||||
.project(
|
.project(
|
||||||
@transactions[:amount],
|
@transactions[:amount],
|
||||||
@transactions[:creditor_id].as('peer_id'),
|
@transactions[:debtor_id].as('peer_id'),
|
||||||
@transactions[:created_at].as('time'),
|
@transactions[:created_at].as('time'),
|
||||||
@transactions[:issuer_id],
|
@transactions[:issuer_id],
|
||||||
@transactions[:issuer_type],
|
@transactions[:issuer_type],
|
||||||
|
|
Loading…
Reference in a new issue