From 44e83e2aba5db24c9f61f5d5ab7ceda206acc073 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 9 Sep 2015 16:26:06 +0200 Subject: [PATCH] allow euros and cents in transaction params --- app/controllers/transactions_controller.rb | 10 ++++---- .../transactions_controller_spec.rb | 24 ++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 7c1152f..394b608 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -34,12 +34,14 @@ class TransactionsController < ApplicationController def transaction_params 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, 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 diff --git a/spec/controllers/transactions_controller_spec.rb b/spec/controllers/transactions_controller_spec.rb index de39b7c..19e3e6f 100644 --- a/spec/controllers/transactions_controller_spec.rb +++ b/spec/controllers/transactions_controller_spec.rb @@ -14,7 +14,7 @@ RSpec.describe TransactionsController, type: :controller do @attributes = { transaction: { debtor: @debtor.name, creditor: @creditor.name, - amount: 20, + cents: 70, message: 'hoi' }} post :create, @attributes @@ -30,7 +30,7 @@ RSpec.describe TransactionsController, type: :controller do end it "should set amount" do - expect(@transaction.amount).to eq(20) + expect(@transaction.amount).to eq(70) end it "should set creditor" do @@ -42,10 +42,22 @@ RSpec.describe TransactionsController, type: :controller do 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 it "should be refused" 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 end @@ -54,9 +66,9 @@ RSpec.describe TransactionsController, type: :controller do it "should be refused" do expect do post :create, transaction: { - debtor: @creditor, - creditor: @debtor, - amount: 10000000000000, + debtor: @creditor.name, + creditor: @debtor.name, + euros: 10000000000000, message: 'DIT IS OVERVAL' } end.not_to change {Transaction.count}