Muck Users
Installation
The muck users engine is part of the muck framework and relies upon the muck_engine as well as authlogic. Both gems should be installed automatically when you install the muck_users engine.
Then
sudo gem install muck-users
Use search logic for searching users. Add this to environment.rb:
config.gem "authlogic"
config.gem "searchlogic"
In addition, you will need to install the ssl_requirement plugin (github.com/rails/ssl_requirement/tree/master) into your Rails project:
ruby script/plugin install ssl_requirement
If you used the muck template to create your rails application you will have a global_config.yml file. If not then you will need to create a global_config.yml file and then add the following to environment.rb right above Rails::Initializer.run do |config|
require 'ostruct'
require 'yaml'
::GlobalConfig = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/global_config.yml")[RAILS_ENV])
Inside of global_config.yml add the following changing the emails to match your application:
default: &DEFAULT
# Sent in emails to users
application_name: 'Name of my application'
from_email: '[email protected]'
support_email: '[email protected]'
admin_email: '[email protected]'
require_access_code: false # Determines whether or not a access code is required to sign up.
Users migration
The users table is frequently one of the most significant of any application. A migration to create users is not included in this gem so that the users tables can be customized appropriately. Here’s an example migration:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users, :force => true do |t|
t.string :login
t.string :email
t.string :first_name
t.string :last_name
t.string :crypted_password
t.string :password_salt
t.string :persistence_token
t.string :single_access_token
t.string :perishable_token
t.integer :login_count, :null => false, :default => 0
t.integer :failed_login_count, :null => false, :default => 0
t.datetime :last_request_at
t.datetime :last_login_at
t.datetime :current_login_at
t.string :current_login_ip
t.string :last_login_ip
t.boolean :terms_of_service, :default => false, :null => false
t.string :time_zone, :default => "UTC"
t.datetime :disabled_at
t.datetime :activated_at
t.
end
add_index :users, :login
add_index :users, :email
add_index :users, :persistence_token
add_index :users, :perishable_token
add_index :users, :single_access_token
add_index :users, :last_request_at
end
def self.down
drop_table :users
end
end
Usage
There are a couple of routes that muck-users will look for:
Route to the site home page:
map.root '', :controller => 'default', :action => 'index'
Route to a public user page (this could also go to home etc. if needed)
map.public_user '/profiles/:id', :controller => 'profiles', :action => 'show'
This is the path that a user will be redirected to if they attempt to access another user’s dashboard page.
By default when a user logs out they are sent to the login page. You can add a new route and change the behavior:
map.logout_complete '/login', :controller => 'user_session', :action => 'new'
muck-users sends out emails that need to be able to generate links. Be sure to set a value for application_url in global_config.yml
Helpers
Muck users provides an autocomplete login search. To enable this functionality create a text box with the class ‘login-search’ and add the following code to include the needed javascript and css:
<% content_for :head do -%>
<%= javascript_include_tag 'jquery/jquery.autocomplete.min' %>
<%= javascript_include_tag 'muck-users' %>
<%= stylesheet_link_tag 'jquery/jquery.autocomplete' %>
<% end -%>
If you override the users controller you will also need to include login_search in your routes:
map.resources :users, :collection => { :login_search => :get }
General information
This engine implements authlogic. Some of the code contained was taken from here: railsforum.com/viewtopic.php?id=14216 and here github.com/activefx/restful_authentication_tutorial/tree/master
Inspiration also came from: github.com/tsechingho/authlogic_bundle/tree/master
Example
After installing the engine just create a user model thus:
class User < ActiveRecord::Base
acts_as_authentic
acts_as_muck_user
end
Then you will be able to go to: http//:localhost:3000/login http//:localhost:3000/signup
Copyright © 2009 Tatemae, released under the MIT license