# frozen_string_literal: true module Admin class StocksController < BaseController before_action :set_stock, only: %i[show edit update destroy] def index @stocks = apply_sorting(Stock.all, default: "ticker") @breadcrumbs = [ { label: "Stocks" } ] end def show @breadcrumbs = [ { label: "Stocks", path: admin_stocks_path }, { label: @stock.ticker } ] end def new @stock = Stock.new @breadcrumbs = [ { label: "Stocks", path: admin_stocks_path }, { label: "New Stock" } ] end def edit @breadcrumbs = [ { label: "Stocks", path: admin_stocks_path }, { label: @stock.ticker, path: admin_stock_path(@stock) }, { label: "Edit" } ] end def create @stock = Stock.new(stock_params) if @stock.save redirect_to admin_stock_path(@stock), notice: t(".notice") else @breadcrumbs = [ { label: "Stocks", path: admin_stocks_path }, { label: "New Stock" } ] render :new, status: :unprocessable_content end end def update if @stock.update(stock_params) redirect_to admin_stock_path(@stock), notice: t(".notice") else @breadcrumbs = [ { label: "Stocks", path: admin_stocks_path }, { label: @stock.ticker, path: admin_stock_path(@stock) }, { label: "Edit" } ] render :edit, status: :unprocessable_content end end def destroy @stock.destroy redirect_to admin_stocks_path, notice: t(".notice") rescue ActiveRecord::DeleteRestrictionError => e redirect_to admin_stocks_path, alert: "Cannot delete stock: #{e.message}" end private def set_stock @stock = Stock.find(params.expect(:id)) end # rubocop:disable Metrics/MethodLength def stock_params params.expect( stock: %i[ ticker company_name company_website stock_exchange description price_cents yesterday_price_cents employees industry cash_flow debt debt_to_equity profit_margin sales_growth industry_avg_debt_to_equity industry_avg_profit_margin industry_avg_sales_growth management competitor_names archived ] ) end # rubocop:enable Metrics/MethodLength end end