# class PortfoliosController

### Public Instance Methods

create() click to toggle source

implemented and partially tested

    # File app/controllers/portfolios_controller.rb, line 7
    def create
      # Add logic to check whether the league is full first
      if Portfolio.where(:user_id => current_user, :league_id => params[:league_id]).first.nil?
        @league = League.find(params[:league_id])
        @portfolio = @league.portfolios.build(:capital => @league.capital, :user_id => current_user.id,
                                              :margin => @league.margin, :manager => false,
                                              :league_id => @league.id)
        if @portfolio.save
          flash[:success] = "Welcome to the league!"
          redirect_to league_url(params[:league_id])
        else
          flash[:fail] = "Sorry, you were unable to join this league."
          redirect_to league_url(params[:league_id])
        end
      else
        flash[:fail] = "You're already in the league!"
        redirect_to :back
      end
    end

destroy() click to toggle source

    # File app/controllers/portfolios_controller.rb, line 32
    def destroy
      #Get the portfolio we want to remove
      port = Portfolio.find_by_id(params[:id])

      #Find out if it has already been removed (If someone presses it twice)
      if port != nil
        #If not, delete it
        port.delete
      end

      #Go back to the league page
      redirect_to league_url(params[:league_id])
    end

show() click to toggle source

Implemented but not tested

    # File app/controllers/portfolios_controller.rb, line 28
    def show
      @portfolio = Portfolio.find(params[:id])
    end

### Protected Instance Methods

check_league_not_full() click to toggle source

    # File app/controllers/portfolios_controller.rb, line 48
    def check_league_not_full
      @league = League.find(params[:league_id])
      if @league.member_limit < Portfolio.where("league_id = ?", params[:league_id]).length
         flash[:fail] = "Sorry, that league is full!"
          redirect_to :back
      end
    end