Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jschoolcraft/79908 to your computer and use it in GitHub Desktop.
Save jschoolcraft/79908 to your computer and use it in GitHub Desktop.
# rails application template for generating customized rails apps
#
# == requires ==
#
# * rails 2.3+, rspec, cucumber, machinist
#
# == a newly generated app using this template comes with ==
#
# * working user registration/login via authlogic, cucumber features to verify that it works
# * rspec/cucumber for testing
# * capistrano deployment script
# * jquery and blueprint css set up
# * a blueprints.rb for machinist
#
# == how to use ==
#
# * install the required gems
# * generate a new app: rails my_new_app -m http://gist.github.com/raw/79908/3b1623869f74dcf449840f855a53e723e88062bc/upstream_rails_application_template.rb
# * run the features to verify everything is working: rake features
#
# == TODO ==
# * add forgot password method
app_name = `pwd`.split('/').last.strip
run "rm README"
run "rm -rf test"
run "rm public/index.html"
run "rm public/favicon.ico"
run "rm public/robots.txt"
run "rm public/images/rails.png"
run "rm -f public/javascripts/*"
# get jquery and plugins
run "wget -q http://jqueryjs.googlecode.com/files/jquery-1.3.2.js -O public/javascripts/jquery.js"
run "wget -q http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js -O public/javascripts/jquery.form.js"
run "wget -q http://jqueryjs.googlecode.com/svn/trunk/plugins/methods/date.js -O public/javascripts/date.js"
run "wget -q http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/scripts/jquery.datePicker.js -O public/javascripts/jquery.datePicker.js"
run "wget -q http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/styles/datePicker.css -O public/stylesheets/datePicker.css"
# blueprint/css
run "wget -q http://github.com/joshuaclayton/blueprint-css/tarball/master -O public/stylesheets/blueprint.tar && tar xf public/stylesheets/blueprint.tar"
run 'rm public/stylesheets/blueprint.tar'
blueprint_dir = Dir.entries('.').grep(/blueprint/).first
run "mv #{blueprint_dir}/blueprint/*.css public/stylesheets"
run "rm -rf #{blueprint_dir}"
# environment
file 'config/environment.rb', <<-FILE
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
# config.load_paths += %W( \#{RAILS_ROOT}/extras )
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# config.time_zone = 'UTC'
end
FILE
# application layout
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>#{app_name}</title>
<%= stylesheet_link_tag 'screen', 'datePicker', :media => 'screen, projection' %>
<%= stylesheet_link_tag 'print', :media => 'print' %>
<!--[if IE]>
<%= stylesheet_link_tag 'ie', :media => 'all' %>
<![endif]-->
<%= javascript_include_tag 'jquery', 'jquery.form.js', 'date', 'jquery.datePicker.js', :cache => true %>
<%= yield(:head) %>
<script type="text/javascript">
$(function() {
<%= yield(:jquery) %>
});
</script>
</head>
<body>
<div id="navigation">
<ul>
<%- if current_user -%>
<li><%= link_to 'Home', account_path %></li>
<li><%= link_to 'Log out', user_session_path, :method => :delete %></li>
<%- else -%>
<li><%= link_to 'Home', root_path %></li>
<li><%= link_to 'Sign up', new_user_path %></li>
<li><%= link_to 'Log in', new_user_session_path %></li>
<%- end -%>
</ul>
</div>
<div id="content">
<%- if flash[:notice] -%>
<div id="flash"><%= flash[:notice] %></div>
<%- end -%>
<%= yield %>
</div>
</body>
</html>
FILE
# Copy database.yml for distribution use
run "rm config/database.yml"
file "config/database.yml", <<-FILE
development:
username: root
password:
adapter: mysql
database: #{app_name}_development
encoding: utf8
socket: <%= [
'/var/lib/mysql/mysql.sock',
'/var/run/mysqld/mysqld.sock',
'/tmp/mysqld.sock',
'/tmp/mysql.sock',
].detect { |socket| File.exist?(socket) } %>
test:
username: root
password:
adapter: mysql
database: #{app_name}_test
encoding: utf8
socket: <%= [
'/var/lib/mysql/mysql.sock',
'/var/run/mysqld/mysqld.sock',
'/tmp/mysqld.sock',
'/tmp/mysql.sock',
].detect { |socket| File.exist?(socket) } %>
production:
username: root
password:
adapter: mysql
database: #{app_name}_production
encoding: utf8
socket: <%= [
'/var/lib/mysql/mysql.sock',
'/var/run/mysqld/mysqld.sock',
'/tmp/mysqld.sock',
'/tmp/mysql.sock',
].detect { |socket| File.exist?(socket) } %>
FILE
run "cp config/database.yml config/database.yml.example"
rake 'db:create'
# routes
file 'config/routes.rb', <<-FILE
ActionController::Routing::Routes.draw do |map|
end
FILE
# Set up .gitignore files
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', <<-END
.DS_Store
log/*.log
log/*.pid
tmp/**/*
config/database.yml
db/schema.rb
END
# gems
gem 'rspec', :version => '>= 1.2.0', :lib => false
gem 'rspec-rails', :version => '>= 1.2.0', :lib => false
gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
gem 'authlogic'
rake 'gems:install', :sudo => true
# plugins
# plugin 'exceptional', :git => 'git://github.com/contrast/exceptional.git'
# run 'cp vendor/plugins/exceptional/exceptional.yml config/exceptional.yml'
# generators
generate("rspec")
generate("rspec-rails")
generate("cucumber")
# machinist
file 'spec/blueprints.rb', <<-FILE
User.blueprint do
login "joe"
password "testtest"
password_confirmation "testtest"
end
FILE
# skip cucumber/rspec load errors on production server
[:cucumber, :rspec].each do |service|
file "lib/tasks/#{service}.rake", <<-FILE
begin
#{File.read("lib/tasks/#{service}.rake")}
rescue LoadError => e
STDERR.puts "could not load #{service}."
end
FILE
end
# authlogic login/signup
generate("session user_session")
generate 'rspec_model user login:string crypted_password:string password_salt:string persistence_token:string login_count:integer last_request_at:datetime last_login_at:datetime current_login_at:datetime last_login_ip:string current_login_ip:string'
route "map.root :controller => 'users', :action => 'new'"
route 'map.resource :user_session'
route "map.resource :account, :controller => 'users'"
route 'map.resources :users'
file "app/controllers/user_sessions_controller.rb", <<-FILE
class UserSessionsController < 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 new_user_session_url
end
end
FILE
file 'app/controllers/users_controller.rb', <<-FILE
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
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/models/user.rb', <<-FILE
class User < ActiveRecord::Base
acts_as_authentic
end
FILE
file 'app/controllers/application_controller.rb', <<-FILE
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
prepend_before_filter :activate_authlogic
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 self.require_user(options = {})
before_filter :require_user, options
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/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/_form.html.erb', <<-FILE
<%= form.label :login %><br />
<%= form.text_field :login %><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 />
<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/show.html.erb', <<-FILE
<p>Welcome <%=h @user.login %></p>
<%= link_to 'Edit Account', 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
# login/signup features
file 'features/log_in.feature', <<-FILE
Feature: log in
In order to use the system
As a user
I want to log in
Scenario: log in
Given a user "alex" with the password "testtest"
When I go to the start page
And I follow "Log in"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I press "Login"
Then I should see "Welcome alex"
And I should see "Login successful!"
Scenario: log out
Given a user "alex" with the password "testtest"
And "alex" is logged in
When I go to the account page
And I follow "Log out"
Then I should see "Log in"
And I should see "Logout successful!"
Scenario: edit account
Given a user "alex" with the password "testtest"
And "alex" is logged in
When I go to the account page
And I follow "Edit Account"
And I fill in "joe" for "Login"
And I press "Update"
Then I should see "Account updated!"
FILE
file 'features/sign_up.feature', <<-FILE
Feature: sign up
In order to use all the platform's features
As a user
I want to sign up
Scenario: sign up successfully
When I go to the start page
And I follow "Sign up"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I fill in "testtest" for "Password confirmation"
And I press "Register"
Then I should see "Welcome alex"
Scenario: signing up fails because login is taken
Given a user "alex"
When I go to the start page
And I follow "Sign up"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I fill in "testtest" for "Password confirmation"
And I press "Register"
Then I should not see "Welcome alex"
And I should see "Login ist bereits vergeben"
FILE
file 'features/step_definitions/user_steps.rb', <<-FILE
Before do
User.delete_all
end
Given /^a user "(.+)" with the password "(.+)"$/ do |login, password|
User.make :login => login, :password => password, :password_confirmation => password
end
Given /a user "([^"]+)"$/ do |login|
User.make :login => login
end
Given /^"([^"]+)" is logged in$/ do |login|
When 'I go to the start page'
When 'I follow "Log in"'
When "I fill in \\\"\#{login}\\\" for \\\"Login\\\""
When 'I fill in "testtest" for "Password"'
When 'I press "Login"'
end
FILE
file 'features/support/paths.rb', <<-FILE
def path_to(page_name)
case page_name
when /the start page/i
root_path
when /the account page/i
account_path
else
raise "Can't find mapping from \"\#{page_name}\" to a path."
end
end
FILE
# migrations
rake "db:migrate"
# capistrano
capify!
file 'config/deploy.rb', <<-FILE
default_run_options[:pty] = true
set :application, "#{app_name}"
set :repository, "[email protected]:#{ask('GitHub username for the git repository?')}/#{app_name}.git"
set :scm, "git"
set :ssh_options, { :forward_agent => true }
set :use_sudo, false
set :domain, "#{ask('What is the servername for deployment?')}"
set :user, "rails"
set :branch, "master"
set :deploy_via, :remote_cache
set :deploy_to, "/var/www/\#{application}"
role :app, domain
role :web, domain
role :db, domain, :primary => true
desc 'restart'
deploy.task :restart, :roles => :app do
run "touch \#{current_path}/tmp/restart.txt"
end
after 'deploy:finalize_update', :roles => :app do
run "ln -s \#{shared_path}/config/database.yml \#{release_path}/config/database.yml"
end
FILE
# Commit all work so far to the repository
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