Setup User Admin with ActiveScaffold
Context
- Rails 2.0.2
- Installed Restful Authentication plugin
- Users table has a
time_zonecolumn - Installed Configuration Settings plugin
- Installed TzTime and TzInfo::Timezone
References
Active Scaffold - This is the project page.
How to Approach Active Scaffold - Use cases and benefits
Install ActiveScaffold Plugin
Installation - Follow the Getting Started section to install the version of the plugin (1.0.1 at the time of this writing) at http://activescaffold.googlecode.com/svn/tags/active_scaffold
/vendor/plugins/active_scaffold/lib/extensions/action_controller.rb:26:in `alias_method': undefined method `render_action' for class ActionController::Base' (NameError)
./script/plugin install http://activescaffold.googlecode.com/svn/tags/1.1.0_rc1
Once the install is complete, go to vendor/plugins and rename the folder 1.1.0_rc1 to active_scaffold.
Set up admin page
1. Create an Admin::UserController which will be located at /app/controllers/admin/user_controller.rb
class Admin::UserController < ApplicationController layout "admin" active_scaffold :user end
2. Create the layout /app/views/layouts/admin.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Generic ActiveScaffold Layout</title>
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>
</head>
<body>
<%= yield %>
</body>
</html>
3. Set up the columns that appear when listing, creating, and updating records by replacing the line active_scaffold :user at Step 1 with the following:
active_scaffold :user do |config|
config.columns = [:login,
:email,
:password,
:password_confirmation,
:activated?,
:activated_at,
:time_zone,
:_configurable_settings ]
config.columns[:time_zone].form_ui = [:time_zone]
config.list.columns.exclude [
:password,
:password_confirmation ]
config.create.columns.exclude [
:activated?,
:activated_at ]
config.update.columns.exclude [
:login,
:activated?,
:activated_at ]
end
4. Exclude certain columns by adding the following to the active_scaffold :user do ... end block.
excluded = [:created_at,
:updated_at, :remember_token, :remember_token_expires_at,
:salt, :activation_code, :crypted_password]
config.list.columns.exclude excluded
config.create.columns.exclude excluded
config.update.columns.exclude excluded
Add Activate links to activate users
1. Add the following in the active_scaffold :user do |config| ... end block
config.action_links.add('activate',
:label => 'Activate',
:type => :record,
:method => :post,
:inline => true,
:crud_type => :update)
2. Add the method in Admin::UsersController
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [:activate],
:redirect_to => { :action => "activate_error" }
def activate
@user = User.find(params[:id])
if @user.activated?
flash.now[:notice] = "#{@user.login} is already activated."
elsif @user.activate!
flash.now[:notice] = "Activated #{@user.login}."
else
flash.now[:notice] = "Cannot activate #{@user.login}."
end
end
def activate_error
render :text => "Failed to activate. Invalid method #{request.env['REQUEST_METHOD']}"
end
3. Add the layout for activate in /app/views/admin/users/activate.html.erb
/app/views/admin/users/activate.html.erb
<p class="message"><%= flash[:notice] %></p>
Extend ActiveScaffold
1. Add active_scaffold_extensions.rb in /lib. This will add a feature to make fields to select time zones with.
/lib/active_scaffold_extentions.rb
module ActiveScaffold
module Helpers
# Helpers that assist with the rendering of a Form Column
module FormColumns
def active_scaffold_input_time_zone(column, options)
time_zone_select :record, column.name, TZInfo::Timezone.us_zones, :model => TZInfo::Timezone
end
end
end
end
2. Make sure to activate the extension by adding the following line at the end of /config/environment.rb
require 'active_scaffold_extensions'
Making ConfigurableSettings Editable
1. Create /app/controllers/admin/configurable_settings_controller.rb
/app/controllers/admin/configurable_settings_controller.rb
class Admin::ConfigurableSettingsController < ApplicationController before_filter :login_required, :admin_required active_scaffold :configurable_settings end