tab/app/controllers/transactions_controller.rb

60 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2015-09-08 15:25:54 +02:00
class TransactionsController < ApplicationController
2019-04-10 14:14:17 +02:00
load_and_authorize_resource :user, find_by: :name
2015-09-09 14:08:40 +02:00
2019-04-10 14:14:17 +02:00
def index
@transactions = @user.transactions
respond_to do |format|
2019-04-11 17:34:23 +02:00
format.json {
render json: @transactions.map {|t| {
:id => t.id,
:debtor => t.debtor.name,
:creditor => t.creditor.name,
:time => t.updated_at,
:amount => t.amount,
:message => t.message,
:issuer => t.issuer.name
}
}
}
2019-04-10 14:14:17 +02:00
end
end
2015-09-09 14:08:40 +02:00
2015-09-08 15:25:54 +02:00
def create
@transaction = Transaction.new(transaction_params)
@transaction.reverse if @transaction.amount < 0
2017-01-14 22:45:51 +01:00
unless can? :create, @transaction
@transaction = Request.new @transaction.info
authorize!(:create, @transaction)
end
if @transaction.save
respond_to do |format|
format.html { redirect_to root_path }
format.json { render json: @transaction, status: :created }
2017-01-09 15:46:43 +01:00
end
2015-09-10 21:46:05 +02:00
else
2017-01-14 22:45:51 +01:00
respond_to do |format|
format.html { redirect_to root_path }
format.json { render json: @transaction.errors.full_messages,
status: :unprocessable_entity }
2017-01-09 15:46:43 +01:00
end
2015-09-08 21:07:00 +02:00
end
2015-09-08 15:25:54 +02:00
end
2015-09-08 21:07:00 +02:00
private
2015-09-09 13:33:55 +02:00
def transaction_params
2015-09-09 11:56:13 +02:00
t = params.require(:transaction)
.permit(:debtor, :creditor, :message, :euros, :cents, :id_at_client)
2015-09-09 11:56:13 +02:00
{
2019-05-07 16:01:54 +02:00
debtor: t[:debtor] ? User.find_by(name: t[:debtor]) : User.zeus,
creditor: t[:creditor] ? User.find_by(name: t[:creditor]) : User.zeus,
2019-04-12 11:31:59 +02:00
issuer: authenticate_user_or_client!,
2015-09-14 10:32:58 +02:00
amount: (t[:euros].to_f * 100 + t[:cents].to_f).to_i,
message: t[:message],
}.merge(current_client ? { id_at_client: t[:id_at_client] } : {})
2015-09-08 21:07:00 +02:00
end
2015-09-08 15:25:54 +02:00
end