parse ranges and return some data about the filtering
This commit is contained in:
parent
4fa9a01c73
commit
1957e3854a
1 changed files with 33 additions and 21 deletions
|
@ -3,40 +3,52 @@ module DataTable
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
def apply_filter(user, params)
|
def apply_filter(user, params)
|
||||||
p AjaxRequest.new(params)
|
params = sanitize_params(params)
|
||||||
selection_to_json(user, user.transactions)
|
selection_to_json(user, params[:draw], user.transactions)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
class AjaxRequest
|
def sanitize_params(params)
|
||||||
|
|
||||||
def initialize(params)
|
|
||||||
# Parsing according to https://datatables.net/manual/server-side
|
# Parsing according to https://datatables.net/manual/server-side
|
||||||
@draw = params.require(:draw).to_i
|
clean = {
|
||||||
@start = params.require(:start).to_i
|
draw: params.require(:draw).to_i,
|
||||||
@length = params.require(:length).to_i
|
start: params.require(:start).to_i,
|
||||||
@columns = Hash.new
|
length: params.require(:length).to_i,
|
||||||
|
columns: Hash.new
|
||||||
|
}
|
||||||
params.require(:columns).each do |i, column|
|
params.require(:columns).each do |i, column|
|
||||||
@columns[column.require(:data).to_sym] = {
|
type, value = column.require(:search)[:value].split(':')
|
||||||
|
h = clean[:columns][column.require(:data).to_sym] = {
|
||||||
name: column[:name],
|
name: column[:name],
|
||||||
searchable: column[:searchable] == 'true',
|
searchable: column[:searchable] == 'true',
|
||||||
orderable: column[:orderable] == 'true',
|
orderable: column[:orderable] == 'true',
|
||||||
search_value: column.require(:search)[:value]
|
type: type
|
||||||
}
|
}
|
||||||
|
if type == 'number-range'
|
||||||
|
h[:lower], h[:upper] = value.split('~').map &:to_i
|
||||||
|
elsif type == 'date-range'
|
||||||
|
h[:lower], h[:upper] = value.split('~').map &:to_datetime
|
||||||
|
else
|
||||||
|
h[:value] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return clean
|
||||||
|
end
|
||||||
|
|
||||||
end
|
def selection_to_json(user, draw, selection)
|
||||||
|
selection = selection.to_a
|
||||||
def selection_to_json(user, selection)
|
{
|
||||||
{ data: selection.map { |transaction| {
|
draw: draw,
|
||||||
|
recordsTotal: user.transactions.count,
|
||||||
|
recordsFiltered: selection.count,
|
||||||
|
data: selection.map { |transaction| {
|
||||||
time: transaction.created_at,
|
time: transaction.created_at,
|
||||||
amount: transaction.signed_amount_for(user),
|
amount: transaction.signed_amount_for(user),
|
||||||
peer: transaction.peer_of(user).try(:name),
|
peer: transaction.peer_of(user).try(:name),
|
||||||
issuer: transaction.issuer.name,
|
issuer: transaction.issuer.name,
|
||||||
message: transaction.message,
|
message: transaction.message,
|
||||||
} }
|
}}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue