Files
project-work/worker-toolkit-stocks-in-the-future/repo/app/policies/stock_policy.rb
Eric Bell 392781f7aa chore: init commit
in worker.../repo/GITFOLDER.zip is the .git folder.
2026-08-11 14:44:09 -04:00

58 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class StockPolicy < ApplicationPolicy
# Show trading-related links (buy/sell/trade) when the user is a student
# and has a portfolio (safeguard for nil portfolio) and the stock is not archived
# (or if archived, user is holding it) and trading is enabled for their classroom.
def show_trading_link?
show_holdings? && (!record.archived? || user.holding?(record)) && user.classroom&.trading_enabled?
end
# Show holdings column / counts for students with a portfolio
def show_holdings?
user.present? && user.student? && portfolio_present?
end
def index?
user.present?
end
def show?
user.present?
end
def new?
create?
end
def create?
admin_required?
end
def edit?
update?
end
def update?
admin_required?
end
def destroy?
admin_required?
end
private
def portfolio_present?
# Student must actually have a persisted portfolio. Nil-safe, avoids relying on cached association state.
user&.portfolio&.persisted?
end
# Scope to control which stocks are visible in listings
class Scope < ApplicationPolicy::Scope
def resolve
scope.all
end
end
end