Skip to content

Commit 1877e23

Browse files
authored
Remove Current position from Portfolio (#983)
* Remove Current position from Portfolio * Linting
1 parent 58b012a commit 1877e23

File tree

8 files changed

+19
-10
lines changed

8 files changed

+19
-10
lines changed

app/controllers/classrooms_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def calculate_classroom_stats
100100
{
101101
total_students: students.count,
102102
active_students: students.joins(:orders).distinct.count,
103-
total_portfolio_value: students.joins(:portfolio).sum("portfolios.current_position"),
103+
total_portfolio_value: students.includes(:portfolio).sum do |student|
104+
student.portfolio&.calculate_total_value || 0
105+
end,
104106
recent_orders_count: Order.joins(:user).where(users: { classroom: @classroom }).where("orders.created_at > ?",
105107
1.week.ago).count
106108
}

app/dashboards/portfolio_dashboard.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PortfolioDashboard < Administrate::BaseDashboard
1212
ATTRIBUTE_TYPES = {
1313
id: Field::Number,
1414
cash_balance: Field::Number.with_options(decimals: 2),
15-
current_position: Field::Number.with_options(decimals: 2),
15+
total_portfolio_worth: Field::Number.with_options(decimals: 2),
1616
portfolio_stocks: Field::HasMany,
1717
portfolio_transactions: Field::HasMany,
1818
user: Field::BelongsTo,
@@ -28,7 +28,7 @@ class PortfolioDashboard < Administrate::BaseDashboard
2828
COLLECTION_ATTRIBUTES = %i[
2929
id
3030
cash_balance
31-
current_position
31+
total_portfolio_worth
3232
portfolio_stocks
3333
].freeze
3434

@@ -37,7 +37,7 @@ class PortfolioDashboard < Administrate::BaseDashboard
3737
SHOW_PAGE_ATTRIBUTES = %i[
3838
id
3939
cash_balance
40-
current_position
40+
total_portfolio_worth
4141
portfolio_stocks
4242
portfolio_transactions
4343
].freeze
@@ -46,7 +46,6 @@ class PortfolioDashboard < Administrate::BaseDashboard
4646
# an array of attributes that will be displayed
4747
# on the model's form (`new` and `edit`) pages.
4848
FORM_ATTRIBUTES = %i[
49-
current_position
5049
portfolio_stocks
5150
portfolio_transactions
5251
].freeze

app/models/portfolio.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def calculate_total_value
3737
calculate_total_value_cents / 100.0
3838
end
3939

40+
def total_portfolio_worth
41+
calculate_total_value
42+
end
43+
4044
def holdings_value
4145
holdings_value_cents / 100.0
4246
end

app/models/student.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def set_default_email
7676
end
7777

7878
def ensure_portfolio
79-
create_portfolio!(current_position: 0) if portfolio.blank?
79+
create_portfolio! if portfolio.blank?
8080
end
8181

8282
def create_initial_enrollment

app/views/classrooms/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</td>
6868
<td class="table-body-cell table-body-cell-right">
6969
<span class="table-cell-content">
70-
$<%= number_with_precision(classroom.students.sum { |s| s.portfolio&.current_position || 0 }, precision: 2) %>
70+
$<%= number_with_precision(classroom.students.sum { |s| s.portfolio&.calculate_total_value || 0 }, precision: 2) %>
7171
</span>
7272
</td>
7373
<td class="table-body-cell table-body-cell-center">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveCurrentPositionFromPortfolios < ActiveRecord::Migration[8.1]
2+
def change
3+
safety_assured { remove_column :portfolios, :current_position, :float }
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/controllers/students_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class StudentsControllerTest < ActionDispatch::IntegrationTest
6464

6565
student = Student.last
6666
assert_not_nil student.portfolio
67-
assert_equal 0, student.portfolio.current_position
67+
assert_equal 0, student.portfolio.calculate_total_value
6868
end
6969

7070
test "student creation generates memorable password" do

0 commit comments

Comments
 (0)