Created
January 26, 2010 22:24
-
-
Save andrewle/287312 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A rough and dirty baseline Rails app template | |
# I just discovered this feature and love it | |
remove_files = [ | |
"public/index.html", | |
"public/images/rails.png", | |
"public/javascripts/controls.js", | |
"public/javascripts/dragdrop.js", | |
"public/javascripts/effects.js", | |
"public/javascripts/prototype.js" | |
] | |
run "rm #{remove_files.join(' ')}" | |
# Plugins | |
plugin 'rails-authorization-plugin', | |
:git => 'git://github.com/DocSavage/rails-authorization-plugin.git' | |
# Gems | |
gem 'binarylogic-searchlogic', :lib => 'searchlogic' | |
gem "authlogic" | |
gem 'chriseppstein-compass', :lib => 'compass', :source => 'http://gems.github.com/' | |
gem 'haml', :lib => 'haml', :version => '>=2.2.0' | |
gem "openrain-action_mailer_tls", :lib => "smtp_tls.rb", :source => "http://gems.github.com" | |
gem 'will_paginate' | |
gem 'redgreen' unless ENV['TM_MODE'] | |
# Generators | |
generate(:rspec_model, "role", "name:string", "authorizable_type:string", "authorizable_id:integer") | |
generate(:rspec_model, "roles_user", "user_id:integer", "role_id:integer") | |
generate(:rspec_controller, "user_session", "new", "create", "destroy") | |
generate(:rspec_scaffold, "user", | |
"email:string", | |
"crypted_password:string", | |
"password_salt:string", | |
"persistence_token:string", | |
"single_access_token:string", | |
"perishable_token:string", | |
"login_count:integer", | |
"failed_login_count:integer", | |
"last_request_at:datetime", | |
"current_login_at:datetime", | |
"current_login_ip:string", | |
"last_login_ip:string" | |
) | |
generate("rspec") | |
# Routes | |
route <<-ROUTE | |
map.resource :account, :controller => "users" | |
map.resources :users | |
map.with_options :controller => 'user_session' do |u| | |
u.login '/login', :action => :new, :conditions => { :method => :get } | |
u.login '/login', :action => :create, :conditions => { :method => :post } | |
u.logout '/logout', :action => :destroy | |
end | |
ROUTE | |
rake("db:migrate") | |
append_file 'config/environment.rb', <<-END | |
END | |
path = destination_path('config/environment.rb') | |
content = File.read(path).sub('Rails::Initializer.run do |config|', <<-END | |
# Authorization plugin for role based access control | |
# You can override default authorization system constants here. | |
# Can be 'object roles' or 'hardwired' | |
AUTHORIZATION_MIXIN = "object roles" | |
# NOTE : If you use modular controllers like '/admin/products' be sure | |
# to redirect to something like '/sessions' controller (with a leading slash) | |
# as shown in the example below or you will not get redirected properly | |
# | |
# This can be set to a hash or to an explicit path like '/login' | |
# | |
LOGIN_REQUIRED_REDIRECTION = { :controller => '/user_session', :action => 'new' } | |
PERMISSION_DENIED_REDIRECTION = { :controller => '/home', :action => 'index' } | |
# The method your auth scheme uses to store the location to redirect back to | |
STORE_LOCATION_METHOD = :store_location | |
Rails::Initializer.run do |config| | |
config.action_controller.session_store = :active_record_store | |
END | |
) | |
File.open(path, 'wb') { |file| file.write(content) } | |
file "app/controllers/user_session_controller.rb", <<-END | |
class UserSessionController < ApplicationController | |
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 login_url | |
end | |
end | |
END | |
file "app/controllers/users_controller.rb", <<-END | |
class UsersController < ApplicationController | |
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 | |
@user.has_role 'admin' | |
permit "admin" do | |
@special_message = "Only an admin should see this." | |
end | |
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 | |
END | |
file 'app/controllers/application_controller.rb', <<-END | |
# 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 | |
helper :all # include all helpers, all the time | |
protect_from_forgery # See ActionController::RequestForgeryProtection for details | |
helper_method :current_user_session, :current_user | |
# Scrub sensitive parameters from your log | |
filter_parameter_logging :password, :password_confirmation | |
protected | |
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 login_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 | |
END | |
file 'app/models/user.rb', <<-END | |
class User < ActiveRecord::Base | |
acts_as_authentic | |
acts_as_authorized_user | |
end | |
END | |
file 'app/models/user_session.rb', <<-END | |
class UserSession < Authlogic::Session::Base | |
end | |
END | |
file 'app/models/roles_user.rb', <<-END | |
class RolesUser < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :role | |
end | |
END | |
file 'app/models/role.rb', <<-END | |
# Defines named roles for users that may be applied to | |
# objects in a polymorphic fashion. For example, you could create a role | |
# "moderator" for an instance of a model (i.e., an object), a model class, | |
# or without any specification at all. | |
class Role < ActiveRecord::Base | |
has_many :roles_users, :dependent => :delete_all | |
has_many :users, :through => :roles_users | |
belongs_to :authorizable, :polymorphic => true | |
end | |
END | |
# Make it a git repo | |
file ".gitignore", <<-END | |
log/*.log | |
tmp/**/* | |
config/database.yml | |
db/*.sqlite3 | |
END | |
git :init | |
git :add => "." | |
git :commit => "-a -m 'Initial commit'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment