Hobo’s Miscellaneous Controller Extensions

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

Contents

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.

See the controller section in Users and Authentication for more information

create_auth_cookie

Populates cookies[:auth_token] with current_user data.

See also the Hobo User Controller section in the Users and Authentication manual.

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

Adding hobo_model_controller to one of your controllers will include this 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

Class Methods

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 in the Controller manual.

auto_actions

See Selecting the automatic actions in the Controller manual.

def_lifecycle_actions

See Lifecycles in the Controller manual.

Instance Methods

find_instance

If your model is Foo, this method is equivalent to

Foo.find(params[:id])

in the simple case. However, it also understands owned routes and sets acting_user.

@foo = find_instance

is quite frequently the first line a custom Hobo controller action.

re_render_form

If you have errors on your page, you can call this helper to re-render the view that contains the source form, with errors highlighted.

destination_after_submit

Contains the logic for Automatic Redirects.

parse_sort_param

Converts params[:sort] from the form used by <table-plus> into a form suitable to be passed to the order_by scope.

Simple usage is to pass a list of field names, however the last parameter can be a hash which maps from field name to database column name.

parse_sort_param(:title, :isbn, :author => "authors.last_name")

parse_sort_param accepts dotted field names, automatically pluralizing to determine the database column.

Story.include(:tasks).order_by(parse_sort_param("task.title"))

will sort on task.title as long as :tasks is included or joined to the scope


Edit this page