Hobo's Miscellaneous Controller Extensions
{.document-title}

This chapter of the Hobo Manual describes Hobo's controller extensions.

Contents
{.contents-heading}

- contents
{:toc}

   doctest: prepare testapp environment
   doctest_require: '../prepare_testapp'
{.hidden}

# Authentication support

## authorized?

This method checks if the user is authorized. It returns true by default. Override this method in your controllers if you want to restrict access to only a few actions or if you want to check if the user has the correct rights.

Example:

      # only allow nonbobs
      def authorized?
        current_user.login != "bob"
      end

## login_required

This is used as a filter to control authentication.

To require logins for all actions, use this in your controllers:

    before_filter :login_required

To require logins for specific actions, use this in your controllers:

    before_filter :login_required, :only => [ :edit, :update ]

To skip this in a subclassed controller:

    skip_before_filter :login_required



## store_location

Store the URI of the current request in the session. We can return to this location by calling #redirect_back_or_default.



## redirect_back_or_default(default)

Redirect to the URI stored by the most recent store_location call or to the passed default.




## login_from_cookie and authenticated_user_from_cookie

When called with before_filter, `login_from_cookie` will call `authenticated_user_from_cookie` to check for
an `auth_token` cookie, and if the result is appropiate, it will log the user in.




## create_auth_cookie

Populates `cookies[:auth_token]` with `current_user` data.



# Cache

## expire_swept_caches_for(obj, attr=nil)

This method is to be used in the controller as a partner to the `<swept-cache>` tag. 
Refer to the `<swept-cache>` taglib for more information.



# Model

## Introduction to the Hobo Model Controller Module

This module uses `def included(base)` to run some code in the context of the current controller. Some interesting stuff:
* Defines @auto_actions as an empty hash if it's not already defined.
* Runs `extend ClassMethods` to add the ClassMethods module to the current controller.
* Adds a couple of helpers: `:model, :current_user`
* Rescues from some errors: `ActiveRecord::RecordNotFound, ActiveRecord::RecordNotFound, Hobo::Model::Lifecycles::LifecycleKeyError`
* Loads some subsite magic

The rest of this section will talk about the ClassMethods added to the controller.


## model_name and model

`model_name` returns `model.name.underscore`

`model` returns `@model` or `controller_name.camelcase.singularize.constantize`


## autocomplete

This is the action called by the `<name-one>` tag to autocomplete results. See Autocompleters section in the controller manual.


## web_method

See `Web methods` sections in the Controller manual.



## auto_actions

See `Selecting the automatic actions` in the Controller manual.



## def_lifecycle_actions

See Lifecycles section in the Controller manual.