class BankAccountsController < ApplicationController before_action :set_owner before_action :set_bank_account, only: [:edit, :update, :destroy] def new @bank_account = BankAccount.new end def create @bank_account = @owner.bank_accounts.build(bank_account_params) if @bank_account.save redirect_to owner_path(@owner), notice: 'Bank account was successfully created.' else render :new end end def index @bank_accounts = @owner.bank_accounts.order(start_date: :desc) end def edit end def update @bank_account = BankAccount.find(params[:id]) if @bank_account.update(bank_account_params) redirect_to owner_path(@owner), notice: 'Bank account was successfully updated.' else render :edit end end def destroy @bank_account.destroy redirect_to polymorphic_path([@owner]), notice: 'Bank account was successfully deleted.' end private def set_owner @owner = if params[:participant_id] Participant.find(params[:participant_id]) elsif params[:worker_id] Worker.find(params[:worker_id]) elsif params[:vendor_id] Vendor.find(params[:vendor_id]) end end def find_owner params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end def set_bank_account @bank_account = BankAccount.find(params[:id]) end def bank_account_params params.require(:bank_account).permit(:institution_name, :account_type, :routing_number, :account_number, :start_date, :end_date) end def owner_path(owner) case owner when Participant participant_path(owner) when Worker worker_path(owner) when Vendor vendor_path(owner) else root_path # Default path if owner type is unknown end end end