Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 912b32bea5
Fetching contributors…

Cannot retrieve contributors at this time

399 lines (325 sloc) 11.361 kB
#
# base rails application template
# last updated:
#
#============================================================
# clean out a default rails app
#============================================================
run "rm README"
run "rm public/index.html"
run "rm public/favicon.ico"
run "rm public/robots.txt"
run "rm public/images/rails.png"
#============================================================
# copy off database.yml
#============================================================
run 'cp config/database.yml config/database.yml.example'
#============================================================
# enable git
#============================================================
git :init
#============================================================
# GEMS
#============================================================
gem 'haml', :lib => 'haml', :version => '>= 2.2.0'
gem 'binarylogic-authlogic', :lib => "authlogic", :source => "http://gems.github.com"
#============================================================
# gitignore
#============================================================
run "touch tmp/.gitignore log/.gitignore vendor/.gitignore"
run %{find . -type d -empty | grep -v "vendor" | grep -v ".git" | grep -v "tmp" | xargs -I xxx touch xxx/.gitignore}
file '.gitignore', <<-ENDEND
.idea
.DS_Store
**/.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
doc/plugins/*
doc/*
public/stylesheets/compiled/*
**/*.swp
*.swp
config/*.sphinx.conf
db/sphinx/
ENDEND
#============================================================
# Authlogic Code
#============================================================
generate(:session, "user_session")
generate(:controller, "user_sessions")
# default session routes
route "map.resource :account, :controller => 'users'"
route "map.resource :user_session"
route "map.root :controller => 'user_sessions', :action => 'new'"
route "map.login '/login', :controller => 'user_sessions', :action => 'destroy'"
# user table migration
file "db/migrate/20090621150348_create_users_and_roles.rb", <<-FILE
class CreateUsersAndRoles < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :login, :null => false # optional, you can use email instead, or both
t.string :email, :null => false # optional, you can use login instead, or both
t.string :crypted_password, :default => nil, :null => true
t.string :password_salt, :default => nil, :null => true # optional, but highly recommended
t.string :persistence_token, :null => false # required
t.string :single_access_token, :null => false # optional, see Authlogic::Session::Params
t.string :perishable_token, :null => false # optional, see Authlogic::Session::Perishability
t.boolean :active, :null => false, :default => false
# Magic columns, just like ActiveRecord's created_at and updated_at. These are automatically maintained by Authlogic if they are present.
t.integer :login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
t.integer :failed_login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
t.datetime :last_request_at # optional, see Authlogic::Session::MagicColumns
t.datetime :current_login_at # optional, see Authlogic::Session::MagicColumns
t.datetime :last_login_at # optional, see Authlogic::Session::MagicColumns
t.string :current_login_ip # optional, see Authlogic::Session::MagicColumns
t.string :last_login_ip # optional, see Authlogic::Session::MagicColumns
t.timestamps
end
add_index :users, :login
add_index :users, :persistence_token
add_index :users, :last_request_at
create_table :roles do |t|
t.string :name
t.timestamps
end
create_table :user_roles do |t|
t.column :user_id, :integer
t.column :role_id, :integer
t.column :created_at, :datetime
end
add_index :user_roles, [:user_id, :role_id], :unique => true
add_index :roles, :name
end
def self.down
remove_index :roles, :name
remove_index :user_roles, :column => [:user_id, :role_id]
drop_table "users"
drop_table "roles"
end
end
FILE
# role migration goes here
# setup UsesSessionsController
file "app/controllers/user_sessions_controller.rb", <<-FILE
class UserSessionsController < ApplicationController
skip_before_filter :require_user # Override application wide filter
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end
FILE
rake "db:sessions:create"
rake "db:migrate"
# make user act as authentic
file "app/models/user.rb", <<-FILE
class User < ActiveRecord::Base
acts_as_authentic
end
FILE
file "app/controllers/users_controller.rb", <<-FILE
class UsersController < ApplicationController
# Comment the 3 following lines to disable new user registration
skip_before_filter :require_user # Override application wide filter
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def show
@user = @current_user
end
def edit
@user = @current_user
end
def update
@user = @current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
end
FILE
file "app/controllers/application_controller.rb", <<-FILE
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
before_filter :require_user # Protect the whole app by requiring a logged in user always
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
filter_parameter_logging :password, :password_confirmation
helper_method :current_user_session, :current_user
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to account_url
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end
FILE
file "app/views/users/_form.erb", <<-FILE
<%= form.label :login %><br />
<%= form.text_field :login %><br />
<br />
<%= form.label :email %><br />
<%= form.text_field :email %><br />
<br />
<%= form.label :password, form.object.new_record? ? nil : "Change password" %><br />
<%= form.password_field :password %><br />
<br />
<%= form.label :password_confirmation %><br />
<%= form.password_field :password_confirmation %><br />
FILE
file "app/views/users/edit.html.erb", <<-FILE
<h1>Edit My Account</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Update" %>
<% end %>
<br /><%= link_to "My Profile", account_path %>
FILE
file "app/views/users/new.html.erb", <<-FILE
<h1>Register</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Register" %>
<% end %>
FILE
file "app/views/users/show.html.erb", <<-FILE
<p>
<b>Login:</b>
<%=h @user.login %>
</p>
<p>
<b>Email:</b>
<%=h @user.email %>
</p>
<p>
<b>Login count:</b>
<%=h @user.login_count %>
</p>
<p>
<b>Last request at:</b>
<%=h @user.last_request_at %>
</p>
<p>
<b>Last login at:</b>
<%=h @user.last_login_at %>
</p>
<p>
<b>Current login at:</b>
<%=h @user.current_login_at %>
</p>
<p>
<b>Last login ip:</b>
<%=h @user.last_login_ip %>
</p>
<p>
<b>Current login ip:</b>
<%=h @user.current_login_ip %>
</p>
<%= link_to 'Edit', edit_account_path %>
FILE
file "app/views/user_sessions/new.html.erb", <<-FILE
<h1>Login</h1>
<% form_for @user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<%= f.label :login %><br />
<%= f.text_field :login %><br />
<br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<%= f.submit "Login" %>
<% end %>
FILE
file "app/views/layouts/application.html.erb", <<-FILE
<!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><%= controller.controller_name %>: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag :defaults %>
</head>
<body>
<h1>Authlogic Basic App</h1>
<%= pluralize User.logged_in.count, "user" %> currently logged in<br /> <!-- This based on last_request_at, if they were active < 10 minutes they are logged in -->
<br />
<br />
<% if !current_user %>
<%= link_to "Register", new_account_path %> |
<%= link_to "Log In", new_user_session_path %> |
<% else %>
<%= link_to "My Account", account_path %> |
<%= link_to "Logout", user_session_path, :method => :delete, :confirm => "Are you sure you want to logout?" %>
<% end %>
<p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>
FILE
# Set up git repository
git :add => '.'
Jump to Line
Something went wrong with that request. Please try again.