tab/app/controllers/transactions_controller.rb

51 lines
1.3 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-09 19:52:16 +00:00
# This line MUST be placed after authentication
load_and_authorize_resource
2015-09-08 13:25:54 +00:00
def index
2015-09-09 23:31:47 +00:00
gridparams = params[:transactions_grid] || Hash.new
gridparams = gridparams.merge(
order: :created_at,
descending: true,
current_user: current_user
)
@grid = TransactionsGrid.new(gridparams) do |scope|
scope.page(params[:page])
end
2015-09-08 13:25:54 +00:00
end
def create
2015-09-10 19:46:05 +00:00
transaction = Transaction.new(transaction_params)
if transaction.save
head :created
else
render json: transaction.errors.full_messages, status: :unprocessable_entity
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)
2015-09-09 09:56:13 +00:00
{
2015-09-09 09:56:13 +00:00
debtor: User.find_by(name: t[:debtor]) || User.zeus,
2015-09-09 11:33:55 +00:00
creditor: User.find_by(name: t[:creditor]) || User.zeus,
issuer: current_client || current_user,
amount: (float(t[:euros]) * 100 + float(t[:cents])).to_i,
message: t[:message]
}
2015-09-08 19:07:00 +00:00
end
def float arg
if arg.is_a? String then arg.sub!(',', '.') end
arg.to_f
end
2015-09-08 13:25:54 +00:00
end