# These assume an application.rb method roughly equal to
#
#  def current_user
#    User.find(session[:user_id])
#  end
#

# most basic example with the feature enabled
class UserController< ActionController::Base
  load User

  def show
    redirect_to "/" unless user.is_active?
  end
end

# much more complex with feature enabled
class MessagesController < ActionController::Base
  load User, Message

  def edit
    redirect_to "/" if user != current_user
  end

  def update
    if update_message
      redirect_to message
    end
      message.errors.collect { BLAH }
      render "edit"
    end
  end
end

# second complex example
class UserPreferencesController < ActionController::Base
  load :current_user, Preferences

  def create
    preference = build_preference
    if preference.save
      # whateves
    else
      # whateves
    end
  end
end

# EQUIVALENT without the feature
class MessagesController < ActionController::Base

  def edit
    redirect_to "/" if user != current_user
  end

  def update
    if update_message
      redirect_to message
    end
      message.errors.collect { BLAH }
      render "edit"
    end
  end

 protected

  def user
    @user ||= User.find(params[:user_id])
  end
  helper_method :user

  def message
    @message ||= messages.find(params[:id])
  end
  helper_method :message

  def messages
    @messages ||= user.messages
  end
  helper_method :messages

  def update_message
    message.update_attribtues(params[:message])
  end

  def build_message
    messages.build(params[:message])
  end
end

class UserPreferencesController < ActionController::Base

  def create
    preference = build_preference
    if preference.save
      # whateves
    else
      # whateves
    end
  end

 protected

  def preference
    @preference ||= current_user.preferences.find(params[:id])
  end
  helper_method :preference

  def preferences
    @preferences ||= current_user.preferences
  end
  helper_method :preferences

  def update_preference
    preference.update_attributes(params[:preference])
  end

  def build_preference
    preferences.build(params[:preference])
  end
end