Move transaction table to user/show
This commit is contained in:
parent
272923d08d
commit
eb717e8cbd
6 changed files with 37 additions and 40 deletions
|
@ -1,11 +1,7 @@
|
|||
class TransactionsController < ApplicationController
|
||||
|
||||
def index
|
||||
p params
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json { render json: TransactionDatatable.new(view_context) }
|
||||
end
|
||||
@transactions = Transaction.all
|
||||
end
|
||||
|
||||
def new
|
||||
|
|
|
@ -3,6 +3,12 @@ class UsersController < ApplicationController
|
|||
|
||||
def show
|
||||
@user = User.find(params[:id])
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json { render json: TransactionDatatable.new(
|
||||
view_context, {user: @user})
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def index
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
class TransactionDatatable < AjaxDatatablesRails::Base
|
||||
include TransactionsHelper
|
||||
|
||||
def sortable_columns
|
||||
@sortable_columns ||= ['Transaction.id']
|
||||
@sortable_columns ||= ['Transaction.amount']
|
||||
end
|
||||
|
||||
def searchable_columns
|
||||
|
@ -9,14 +10,18 @@ class TransactionDatatable < AjaxDatatablesRails::Base
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def data
|
||||
records.map do |record|
|
||||
[ record.id, record.debtor.name, record.creditor.name, record.amount ]
|
||||
[ amount_in_perspective(record, options[:user]),
|
||||
record.origin,
|
||||
record.message,
|
||||
get_transaction_peer(record, options[:user]).name,
|
||||
record.created_at.strftime('%d/%m/%y %H:%M')
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def get_raw_records
|
||||
Transaction.all.eager_load(:debtor, :creditor)
|
||||
options[:user].transactions.eager_load(:debtor, :creditor)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,11 @@
|
|||
module TransactionsHelper
|
||||
def get_transaction_peer(transaction, user)
|
||||
return transaction.creditor if user == transaction.debtor
|
||||
return transaction.debtor if user == transaction.creditor
|
||||
end
|
||||
|
||||
def amount_in_perspective(transaction, user)
|
||||
return -transaction.amount if user == transaction.debtor
|
||||
return transaction.amount if user == transaction.creditor
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
%table#transactions.display{data: { source: transactions_path }}
|
||||
%thead
|
||||
%tr
|
||||
%th id
|
||||
%th debtor
|
||||
%th creditor
|
||||
%th amount
|
||||
%tbody
|
||||
|
||||
:javascript
|
||||
$(document).ready(function() {
|
||||
$('#transactions').DataTable({
|
||||
'processing': true,
|
||||
'serverSide': true,
|
||||
'ajax': $('#transactions').data('source'),
|
||||
'pagingType': 'full_numbers'
|
||||
});
|
||||
});
|
|
@ -1,21 +1,20 @@
|
|||
%h2= @user.name
|
||||
%table
|
||||
%table#transactions.display{data: { source: user_path(@user) }}
|
||||
%thead
|
||||
%tr
|
||||
%th ID
|
||||
%th Debtor
|
||||
%th Creditor
|
||||
%th Amount
|
||||
%th Origin
|
||||
%th Message
|
||||
%th Peer
|
||||
%th Time
|
||||
%tbody
|
||||
- @user.transactions.each do |t|
|
||||
%tr
|
||||
%td= t.id
|
||||
%td= t.debtor.name
|
||||
%td= t.creditor.name
|
||||
%td= t.amount
|
||||
%td= t.origin
|
||||
%td= t.message
|
||||
%td= t.created_at
|
||||
|
||||
:javascript
|
||||
$(document).ready(function() {
|
||||
$('#transactions').DataTable({
|
||||
'processing': true,
|
||||
'serverSide': true,
|
||||
'ajax': $('#transactions').data('source'),
|
||||
'pagingType': 'full_numbers'
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue