Created
February 9, 2012 14:26
-
-
Save tallgreentree/1780315 to your computer and use it in GitHub Desktop.
Rails template - has_secure_password-based login system (thanks ryanb!)
This file contains hidden or 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
# use like so: | |
# rails new <projectname> -Tm https://raw.github.com/gist/1780315/509575d448ed95784c90abcb1ef82eac0a682714/template.rb | |
log "Running rvm" | |
run "rvm use --create --rvmrc 1.9.2@#{app_name}" | |
log "Removing index.html" | |
run "rm public/index.html" | |
generate "controller welcome index" | |
route 'root :to => "welcome#index"' | |
log "Adding bcrypt-ruby" | |
gem "bcrypt-ruby" | |
log "Installing rspec" | |
gem 'rspec-rails', :group => [:development, :test] | |
run "bundle install" | |
generate "rspec:install" | |
log "Creating basic auth system" | |
create_file "db/migrate/20120208162831_create_users.rb", %Q{class CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :name | |
t.string :email | |
t.string :password_digest | |
t.timestamps | |
end | |
end | |
end | |
} | |
route "resources :users, :only => ['new', 'create']" | |
route "resources :sessions, :only => ['new', 'create', 'destroy']" | |
create_file "app/models/user.rb", %Q{class User < ActiveRecord::Base | |
has_secure_password | |
attr_accessible :name, :email, :password, :password_confirmation | |
validates_presence_of :password, :on => :create | |
validates_presence_of :name | |
validates_presence_of :email | |
end | |
} | |
create_file "app/controllers/users_controller.rb", %Q{class UsersController < ApplicationController | |
force_ssl | |
def new | |
@user = User.new | |
end | |
def create | |
@user = User.new(params[:user]) | |
if @user.save | |
redirect_to root_url, :notice => 'Thanks for signing up!' | |
else | |
render :action => :new | |
end | |
end | |
end | |
} | |
create_file "app/views/users/new.html.erb", %Q{ | |
<h1>Register</h1> | |
<%= render :partial => "form" %> | |
} | |
create_file "app/views/users/_form.html.erb", %Q{ | |
<%= form_for @user do |f| %> | |
<% if @user.errors.any? %> | |
<div class="error_messages"> | |
<h2>There was a problem with your registration</h2> | |
<ul> | |
<% for message in @user.errors.full_messages %> | |
<li><%= message %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :name %> | |
<%= f.text_field :name %> | |
</div> | |
<div class="field"> | |
<%= f.label :email %> | |
<%= f.text_field :email %> | |
</div> | |
<div class="field"> | |
<%= f.label :password %> | |
<%= f.password_field :password %> | |
</div> | |
<div class="field"> | |
<%= f.label :password_confirmation %> | |
<%= f.password_field :password_confirmation %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
} | |
create_file "app/controllers/sessions_controller.rb", %Q{ | |
class SessionsController < ApplicationController | |
force_ssl | |
def new | |
end | |
def create | |
user = User.find_by_email(params[:email]) | |
if user && user.authenticate(params[:password]) | |
session[:user_id] = user.id | |
redirect_to root_url, :notice => "Logged in!" | |
else | |
flash.now.alert = "Invalid email or password" | |
render "new" | |
end | |
end | |
def destroy | |
session[:user_id] = nil | |
redirect_to root_url, :notice => "Logged out!" | |
end | |
end | |
} | |
create_file "app/views/sessions/new.html.erb", %Q{ | |
<h1>Please Log In</h1> | |
<%= render :partial => "form" %> | |
} | |
create_file "app/views/sessions/_form.html.erb", %Q{ | |
<%= form_tag sessions_path do %> | |
<div class="field"> | |
<%= label_tag :email %> | |
<%= text_field_tag :email %> | |
</div> | |
<div class="field"> | |
<%= label_tag :password %> | |
<%= password_field_tag :password %> | |
</div> | |
<div class="actions"> | |
<%= submit_tag "Log In" %> | |
</div> | |
<% end %> | |
} | |
create_file "app/controllers/application_controller.rb", %Q{ | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
private | |
def current_user | |
@current_user ||= User.find(session[:user_id]) if session[:user_id] | |
end | |
helper_method :current_user | |
end | |
} | |
log "Creating git repo and adding all files." | |
git :init | |
git :add => "." | |
git :commit => "-m Initial project setup" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment