Skip to content

Commit

Permalink
Merge pull request #160 from rubyforgood/jonny5/142-fixes
Browse files Browse the repository at this point in the history
Fixes for #142
  • Loading branch information
jonny5 authored Jun 1, 2024
2 parents 25363da + cb649ea commit 863ae06
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/controllers/admin/students_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/students/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ and renders all form fields for a resource's editable attributes.
</div>
<% end %>
</fieldset>
<% if action_name == 'edit' %>
<% if action_name == 'edit' || action_name == 'update' %>
<div class="field-unit">
<div class="field-unit__label"><label>Cash balance</label></div>
<div class="field-unit__field">
<%= sprintf('%.2f', page.resource&.portfolio&.cash_balance || 0)%>
<%= number_to_currency(page.resource&.portfolio&.cash_balance || 0)%>
</div>
</div>
<div class="field-unit">
Expand Down
19 changes: 19 additions & 0 deletions test/controllers/admin/students_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@ 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' } }
end

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

0 comments on commit 863ae06

Please sign in to comment.