Skip to content

Commit 917f16d

Browse files
sauloperezenricostano
authored andcommitted
Do not allow transfers to same account
It adds a validation on the Transfer model and shows a flash error message when that happens, without making the actual movement.
1 parent 39ce959 commit 917f16d

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

app/controllers/transfers_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
class TransfersController < ApplicationController
22
def create
33
@source = find_source
4-
Transfer.create(transfer_params.merge source: @source)
54
@account = Account.find(transfer_params[:destination])
5+
transfer = Transfer.new(
6+
transfer_params.merge(source: @source, destination: @account)
7+
)
8+
9+
if transfer.valid?
10+
transfer.make_movements
11+
else
12+
flash[:error] = transfer.errors.full_messages.to_sentence
13+
end
614
redirect_to redirect_target
715
end
816

app/models/transfer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Transfer < ActiveRecord::Base
1717
belongs_to :operator, class_name: "User"
1818
has_many :movements
1919

20+
validate :different_source_and_destination
21+
2022
after_create :make_movements
2123

2224
def make_movements
@@ -39,4 +41,9 @@ def source_id
3941
def destination_id
4042
destination.respond_to?(:id) ? destination.id : destination
4143
end
44+
45+
def different_source_and_destination
46+
return unless source == destination
47+
errors.add(:base, :same_account)
48+
end
4249
end

config/locales/ca.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ ca:
102102
attributes:
103103
web:
104104
url_format_invalid: Format de URL no vàlid
105+
transfer:
106+
attributes:
107+
base:
108+
same_account: No es pot fer una transacció al mateix compte
105109

106110
# ETIQUETAS VARIADAS EN PLANTILLAS
107111

config/locales/en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ en:
102102
attributes:
103103
web:
104104
url_format_invalid: invalid URL format
105+
transfer:
106+
attributes:
107+
base:
108+
same_account: A transfer cannot be made to the same account
105109

106110
# ETIQUETAS VARIADAS EN PLANTILLAS
107111

config/locales/es.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ es:
102102
attributes:
103103
web:
104104
url_format_invalid: Formato de URL no válido
105+
transfer:
106+
attributes:
107+
base:
108+
same_account: No se puede hacer una transacción a la misma cuenta
105109

106110
# ETIQUETAS VARIADAS EN PLANTILLAS
107111

0 commit comments

Comments
 (0)