2015-09-08 15:25:54 +02:00
|
|
|
class TransactionsController < ApplicationController
|
2015-09-09 14:08:40 +02:00
|
|
|
skip_before_filter :verify_authenticity_token, only: :create
|
|
|
|
|
|
|
|
before_action :authenticate_user!, except: :create
|
|
|
|
before_action :authenticate_user_or_client!, only: :create
|
|
|
|
|
2015-09-09 21:52:16 +02:00
|
|
|
# This line MUST be placed after authentication
|
|
|
|
load_and_authorize_resource
|
|
|
|
|
2015-09-08 15:25:54 +02:00
|
|
|
def index
|
2015-09-08 20:45:32 +02:00
|
|
|
@transactions = Transaction.all
|
2015-09-08 15:25:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-09-09 13:33:55 +02:00
|
|
|
@transaction = Transaction.new(transaction_params)
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2015-09-10 15:26:15 +02:00
|
|
|
@user = current_user
|
2015-09-09 13:33:55 +02:00
|
|
|
if @transaction.save
|
|
|
|
flash[:success] = "Transaction created"
|
2015-09-10 15:26:15 +02:00
|
|
|
redirect_to current_user
|
2015-09-09 13:33:55 +02:00
|
|
|
else
|
2015-09-10 15:26:15 +02:00
|
|
|
render "users/show"
|
2015-09-09 13:33:55 +02:00
|
|
|
end
|
|
|
|
end
|
2015-09-08 21:07:00 +02:00
|
|
|
|
2015-09-09 13:33:55 +02:00
|
|
|
format.json do
|
|
|
|
head(@transaction.save ? :created : :unprocessable_entity)
|
2015-09-09 12:58:44 +02: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)
|
2015-09-09 16:26:06 +02:00
|
|
|
.permit(:debtor, :creditor, :message, :euros, :cents)
|
2015-09-09 11:56:13 +02:00
|
|
|
|
2015-09-09 16:26:06 +02:00
|
|
|
{
|
2015-09-09 11:56:13 +02:00
|
|
|
debtor: User.find_by(name: t[:debtor]) || User.zeus,
|
2015-09-09 13:33:55 +02:00
|
|
|
creditor: User.find_by(name: t[:creditor]) || User.zeus,
|
2015-09-09 16:26:06 +02:00
|
|
|
issuer: current_client || current_user,
|
2015-09-10 16:15:17 +02:00
|
|
|
amount: (float(t[:euros]) * 100 + float(t[:cents])).to_i,
|
2015-09-09 16:26:06 +02:00
|
|
|
message: t[:message]
|
|
|
|
}
|
2015-09-08 21:07:00 +02:00
|
|
|
end
|
2015-09-10 16:15:17 +02:00
|
|
|
|
|
|
|
def float arg
|
|
|
|
if arg.is_a? String then arg.sub!(',', '.') end
|
|
|
|
arg.to_f
|
|
|
|
end
|
2015-09-08 15:25:54 +02:00
|
|
|
end
|