11class ValuationsController < ApplicationController
22 include EntryableResource , StreamExtensions
33
4+ def confirm_create
5+ @account = Current . family . accounts . find ( params . dig ( :entry , :account_id ) )
6+ @entry = @account . entries . build ( entry_params . merge ( currency : @account . currency ) )
7+
8+ render :confirm_create
9+ end
10+
11+ def confirm_update
12+ @entry = Current . family . entries . find ( params [ :id ] )
13+ @account = @entry . account
14+ @entry . assign_attributes ( entry_params . merge ( currency : @account . currency ) )
15+
16+ render :confirm_update
17+ end
18+
419 def create
520 account = Current . family . accounts . find ( params . dig ( :entry , :account_id ) )
6-
7- result = account . update_balance (
8- balance : entry_params [ :amount ] ,
9- date : entry_params [ :date ] ,
10- currency : entry_params [ :currency ] ,
11- notes : entry_params [ :notes ]
12- )
21+ result = perform_balance_update ( account , entry_params . merge ( currency : account . currency ) )
1322
1423 if result . success?
1524 @success_message = result . updated? ? "Balance updated" : "No changes made. Account is already up to date."
@@ -25,12 +34,7 @@ def create
2534 end
2635
2736 def update
28- result = @entry . account . update_balance (
29- date : @entry . date ,
30- balance : entry_params [ :amount ] ,
31- currency : entry_params [ :currency ] ,
32- notes : entry_params [ :notes ]
33- )
37+ result = perform_balance_update ( @entry . account , entry_params . merge ( currency : @entry . currency , existing_valuation_id : @entry . id ) )
3438
3539 if result . success?
3640 @entry . reload
@@ -59,4 +63,14 @@ def entry_params
5963 params . require ( :entry )
6064 . permit ( :date , :amount , :currency , :notes )
6165 end
66+
67+ def perform_balance_update ( account , params )
68+ account . update_balance (
69+ balance : params [ :amount ] ,
70+ date : params [ :date ] ,
71+ currency : params [ :currency ] ,
72+ notes : params [ :notes ] ,
73+ existing_valuation_id : params [ :existing_valuation_id ]
74+ )
75+ end
6276end
0 commit comments