class ApplicationController < ActionController::Base
  before_filter :set_user, :count_visits
  protect_from_forgery

  private
  def count_visits
    value = (cookies[:visits] || '0').to_i
    cookies[:visits] = (value + 1).to_s
    @visits = cookies[:visits]
  end

  protected
  def set_user
    @user = User.find(session[:id]) if @user.nil? && session[:id]
  end

  def login_required
    return true if @user
    access_denied
    return false
  end

  def access_denied
    session[:return_to] = request.fullpath
    flash[:error] = 'Oops.  You need to login before you can view that page.'
    redirect_to :controller => 'user', :action => 'login'
  end
end