diff --git a/app/controllers/admin/students_controller.rb b/app/controllers/admin/students_controller.rb index 42c3d28..b9fa355 100644 --- a/app/controllers/admin/students_controller.rb +++ b/app/controllers/admin/students_controller.rb @@ -47,9 +47,13 @@ def update private def create_portfolio_transaction! - return unless fund_amount + return unless fund_amount.present? - PortfolioTransaction.create!(portfolio: requested_resource.portfolio, amount: fund_amount) if fund_amount + PortfolioTransaction.create!( + portfolio: requested_resource.portfolio, + amount: fund_amount, + transaction_type: 'deposit' + ) end def fund_amount diff --git a/app/views/admin/students/_form.html.erb b/app/views/admin/students/_form.html.erb index 25fb1d1..66204cd 100644 --- a/app/views/admin/students/_form.html.erb +++ b/app/views/admin/students/_form.html.erb @@ -52,11 +52,11 @@ and renders all form fields for a resource's editable attributes. <% end %> - <% if action_name == 'edit' %> + <% if action_name == 'edit' || action_name == 'update' %>
- <%= sprintf('%.2f', page.resource&.portfolio&.cash_balance || 0)%> + <%= number_to_currency(page.resource&.portfolio&.cash_balance || 0)%>
diff --git a/test/controllers/admin/students_controller_test.rb b/test/controllers/admin/students_controller_test.rb index 759283f..9d85b4e 100644 --- a/test/controllers/admin/students_controller_test.rb +++ b/test/controllers/admin/students_controller_test.rb @@ -14,6 +14,18 @@ class Admin::StudentsControllerTest < ActionDispatch::IntegrationTest assert_redirected_to admin_student_url(@student) end + test 'should not update with an error' do + @student.update_attribute(:username, 'testingusername') + + patch admin_student_url(@student), params: { student: { username: '' } } + + @student.reload + + assert_equal 'testingusername', @student.username + assert_response :unprocessable_entity + assert_select 'div#error_explanation' + end + test 'given a add_fund_amount, creates a transaction' do assert_difference('PortfolioTransaction.count', 1) do patch admin_student_url(@student), params: { student: { add_fund_amount: '10.50' } } @@ -21,9 +33,16 @@ class Admin::StudentsControllerTest < ActionDispatch::IntegrationTest transaction = PortfolioTransaction.last + assert_equal 'deposit', transaction.transaction_type assert_equal 10.50, transaction.amount assert_equal @student.reload.portfolio.portfolio_transactions.last, transaction assert_redirected_to admin_student_url(@student) end + + test 'given an empty add_fund_amount, does not create a transaction' do + assert_difference('PortfolioTransaction.count', 0) do + patch admin_student_url(@student), params: { student: { add_fund_amount: '' } } + end + end end