tab/app/controllers/transactions_controller.rb

47 lines
1.4 KiB
Ruby
Raw Normal View History

2015-09-08 13:25:54 +00:00
class TransactionsController < ApplicationController
2015-09-09 12:08:40 +00: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-14 08:32:58 +00:00
2015-09-11 11:16:53 +00:00
respond_to :js, only: :create
2015-09-09 12:08:40 +00:00
2015-09-08 13:25:54 +00:00
def create
@transaction = Transaction.new(transaction_params)
@transaction.reverse if @transaction.amount < 0
2017-01-14 21:45:51 +00: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 14:46:43 +00:00
end
2015-09-10 19:46:05 +00:00
else
2017-01-14 21:45:51 +00: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 14:46:43 +00:00
end
2015-09-08 19:07:00 +00:00
end
2015-09-08 13:25:54 +00:00
end
2015-09-08 19:07:00 +00:00
private
2015-09-09 11:33:55 +00:00
def transaction_params
2015-09-09 09:56:13 +00:00
t = params.require(:transaction)
.permit(:debtor, :creditor, :message, :euros, :cents, :id_at_client)
2015-09-09 09:56:13 +00:00
{
debtor: t[:debtor] ? User.find_or_create_by(name: t[:debtor]) : User.zeus,
creditor: t[:creditor] ? User.find_or_create_by(name: t[:creditor]) : User.zeus,
issuer: current_client || current_user,
2015-09-14 08:32:58 +00: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 19:07:00 +00:00
end
2015-09-08 13:25:54 +00:00
end