class ConventionsController < ApplicationController
  # You should always be able to get a list of conventions
  skip_authorization_check
  
  def index
    @upcoming_cons = []
    @past_cons = []

    @conventions = Convention.all

    @conventions.each do |con|
      if con.ended?
        @past_cons << con
      else
        @upcoming_cons << con
      end
    end
  end
  
  def new
    @convention = Convention.new
  end
  
  # Write the new convention to the database
  def create
    @convention = Convention.new(convention_params)
    if @convention.save
      redirect_to conventions_url
    else
      render 'new'
    end
  end
  
  def show
    @convention = Convention.find(params[:id])
    authorize_action_for @convention
  end  
  
  private
  def convention_params
    params.require(:convention).permit(:name, :domain)
  end
end

class ConventionsController < ApplicationController
  load_and_authorize_resource
  
  def index
    @upcoming_cons = []
    @past_cons = []

    @conventions.each do |con|
      if con.ended?
        @past_cons << con
      else
        @upcoming_cons << con
      end
    end
  end
  
  def new
  end
  
  # Write the new convention to the database
  def create
    if @convention.save
      redirect_to conventions_url
    else
      render 'new'
    end
  end
  
  def edit
  end
  
  def update
    if @convention.save
      redirect_to conventions_url
    else
      render 'edit'
    end
  end
  
  def destroy
    @convention.destroy!
    redirect_to conventions_url
  end
  
  private
  def convention_params
    params.require(:convention).permit(:name, :domain)
  end
end

Refactor ConventionsController to take advantage of CanCan's load_and_authorize_resource
Refactor ConventionsController to take advantage of CanCan's load_and_authorize_resource
