diff --git a/app/controllers/admin/students_controller.rb b/app/controllers/admin/students_controller.rb
index 6d6caa9..42c3d28 100644
--- a/app/controllers/admin/students_controller.rb
+++ b/app/controllers/admin/students_controller.rb
@@ -2,11 +2,12 @@ module Admin
class StudentsController < Admin::ApplicationController
# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
- #
- # def update
- # super
- # send_foo_updated_email(requested_resource)
- # end
+
+ def update
+ super
+
+ create_portfolio_transaction!
+ end
# Override this method to specify custom lookup behavior.
# This will be used to set the resource for the `show`, `edit`, and `update`
@@ -42,5 +43,17 @@ class StudentsController < Admin::ApplicationController
# See https://administrate-demo.herokuapp.com/customizing_controller_actions
# for more information
+ #
+ private
+
+ def create_portfolio_transaction!
+ return unless fund_amount
+
+ PortfolioTransaction.create!(portfolio: requested_resource.portfolio, amount: fund_amount) if fund_amount
+ end
+
+ def fund_amount
+ @fund_amount ||= params['student']['add_fund_amount']
+ end
end
end
diff --git a/app/dashboards/portfolio_transaction_dashboard.rb b/app/dashboards/portfolio_transaction_dashboard.rb
index 0f943da..8d67f86 100644
--- a/app/dashboards/portfolio_transaction_dashboard.rb
+++ b/app/dashboards/portfolio_transaction_dashboard.rb
@@ -1,4 +1,4 @@
-require "administrate/base_dashboard"
+require 'administrate/base_dashboard'
class PortfolioTransactionDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
@@ -9,11 +9,13 @@ class PortfolioTransactionDashboard < Administrate::BaseDashboard
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
id: Field::Number,
- actor: Field::BelongsTo,
portfolio: Field::BelongsTo,
- transaction_type: Field::Select.with_options(searchable: false, collection: ->(field) { field.resource.class.send(field.attribute.to_s.pluralize).keys }),
+ transaction_type: Field::Select.with_options(searchable: false, collection: lambda { |field|
+ field.resource.class.send(field.attribute.to_s.pluralize).keys
+ }),
+ amount: Field::Number.with_options(decimals: 2),
created_at: Field::DateTime,
- updated_at: Field::DateTime,
+ updated_at: Field::DateTime
}.freeze
# COLLECTION_ATTRIBUTES
@@ -23,7 +25,6 @@ class PortfolioTransactionDashboard < Administrate::BaseDashboard
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
id
- actor
portfolio
transaction_type
].freeze
@@ -32,7 +33,6 @@ class PortfolioTransactionDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
id
- actor
portfolio
transaction_type
created_at
@@ -43,9 +43,9 @@ class PortfolioTransactionDashboard < Administrate::BaseDashboard
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
- actor
portfolio
transaction_type
+ amount
].freeze
# COLLECTION_FILTERS
diff --git a/app/models/portfolio.rb b/app/models/portfolio.rb
index da52130..198a859 100644
--- a/app/models/portfolio.rb
+++ b/app/models/portfolio.rb
@@ -3,6 +3,10 @@ class Portfolio < ApplicationRecord
has_many :portfolio_transactions
has_many :portfolio_stocks
+ def cash_balance
+ portfolio_transactions.sum(:amount)
+ end
+
# a User (specifically student) can buy a certain amount of stock
def buy_stock(ticker, shares)
stock = Stock.find_by(ticker:)
diff --git a/app/models/student.rb b/app/models/student.rb
index 71dfb88..43e6df0 100644
--- a/app/models/student.rb
+++ b/app/models/student.rb
@@ -1,4 +1,2 @@
class Student < User
- belongs_to :classroom
- has_one :portfolio, foreign_key: 'user_id'
-end
\ No newline at end of file
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index 16a0cc1..0afdf52 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,6 +1,7 @@
class User < ApplicationRecord
has_one :portfolio
has_many :orders
+ belongs_to :classroom
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
diff --git a/app/views/admin/students/_form.html.erb b/app/views/admin/students/_form.html.erb
new file mode 100644
index 0000000..25fb1d1
--- /dev/null
+++ b/app/views/admin/students/_form.html.erb
@@ -0,0 +1,74 @@
+<%#
+# Form Partial
+
+This partial is rendered on a resource's `new` and `edit` pages,
+and renders all form fields for a resource's editable attributes.
+
+## Local variables:
+
+- `page`:
+ An instance of [Administrate::Page::Form][1].
+ Contains helper methods to display a form,
+ and knows which attributes should be displayed in the resource's form.
+
+[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
+%>
+
+<%= form_for([namespace, page.resource], html: { class: "form" }) do |f| %>
+ <% if page.resource.errors.any? %>
+