tab/app/controllers/transactions_controller.rb

41 lines
1 KiB
Ruby
Raw Normal View History

2015-09-08 15:25:54 +02:00
class TransactionsController < ApplicationController
load_and_authorize_resource
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-11 13:16:53 +02:00
respond_to :js, only: :create
2015-09-09 14:08:40 +02:00
2015-09-08 15:25:54 +02:00
def index
@transactions = Transaction.all
end
def new
@transaction = Transaction.new
2015-09-08 15:25:54 +02:00
end
def create
2015-09-10 21:46:05 +02:00
transaction = Transaction.new(transaction_params)
if transaction.save
head :created
else
render json: transaction.errors.full_messages, status: :unprocessable_entity
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)
2015-09-09 11:56:13 +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,
issuer: current_client || current_user,
amount: (t[:euros].to_f*100 + t[:cents].to_f).to_i,
message: t[:message]
}
2015-09-08 21:07:00 +02:00
end
2015-09-08 15:25:54 +02:00
end