allow euros and cents in transaction params

This commit is contained in:
Ilion Beyst 2015-09-09 16:26:06 +02:00
parent 661728772f
commit 44e83e2aba
2 changed files with 24 additions and 10 deletions

View file

@ -34,12 +34,14 @@ class TransactionsController < ApplicationController
def transaction_params def transaction_params
t = params.require(:transaction) t = params.require(:transaction)
.permit(:debtor, :creditor, :amount, :message) .permit(:debtor, :creditor, :message, :euros, :cents)
t.update({ {
debtor: User.find_by(name: t[:debtor]) || User.zeus, debtor: User.find_by(name: t[:debtor]) || User.zeus,
creditor: User.find_by(name: t[:creditor]) || User.zeus, creditor: User.find_by(name: t[:creditor]) || User.zeus,
issuer: current_client || current_user issuer: current_client || current_user,
}) amount: (t[:euros].to_f*100 + t[:cents].to_f).to_i,
message: t[:message]
}
end end
end end

View file

@ -14,7 +14,7 @@ RSpec.describe TransactionsController, type: :controller do
@attributes = { transaction: { @attributes = { transaction: {
debtor: @debtor.name, debtor: @debtor.name,
creditor: @creditor.name, creditor: @creditor.name,
amount: 20, cents: 70,
message: 'hoi' message: 'hoi'
}} }}
post :create, @attributes post :create, @attributes
@ -30,7 +30,7 @@ RSpec.describe TransactionsController, type: :controller do
end end
it "should set amount" do it "should set amount" do
expect(@transaction.amount).to eq(20) expect(@transaction.amount).to eq(70)
end end
it "should set creditor" do it "should set creditor" do
@ -42,10 +42,22 @@ RSpec.describe TransactionsController, type: :controller do
end end
end end
context "with float euros" do
it "should set correct amount" do
post :create, transaction: {
debtor: @debtor.name,
creditor: @creditor.name,
euros: 10.5,
message: "Omdat je een leuke jongen bent!"
}
expect(Transaction.last.amount).to eq(1050)
end
end
context "with negative amount" do context "with negative amount" do
it "should be refused" do it "should be refused" do
expect do expect do
post :create, transaction: attributes_for(:transaction, amount: -20) post :create, transaction: attributes_for(:transaction, cents: -20)
end.not_to change {Transaction.count} end.not_to change {Transaction.count}
end end
end end
@ -54,9 +66,9 @@ RSpec.describe TransactionsController, type: :controller do
it "should be refused" do it "should be refused" do
expect do expect do
post :create, transaction: { post :create, transaction: {
debtor: @creditor, debtor: @creditor.name,
creditor: @debtor, creditor: @debtor.name,
amount: 10000000000000, euros: 10000000000000,
message: 'DIT IS OVERVAL' message: 'DIT IS OVERVAL'
} }
end.not_to change {Transaction.count} end.not_to change {Transaction.count}