# rails application template for generating customized rails apps
#
# == requires ==
#
# * rails 2.3+, rspec, cucumber, culerity (langalex-culerity gem), 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/culerity for testing
# * jquery set up
#
# == how to use ==
#
# * install the required gems
#
# == TODO ==
# * add forgot password method
# * make registration/login use resource_controller
app_name = `pwd`.split('/').last.strip
# ===========================================================================
# Remove Rails boilerplate
# ===========================================================================
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 "curl -L http://jqueryjs.googlecode.com/files/jquery-1.3.2.js > public/javascripts/jquery.js"
# ===========================================================================
# Set up environment, db, and routes
# ===========================================================================
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'
# config.i18n.default_locale = :de
end
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 default layout
# ===========================================================================
file 'app/views/layouts/default.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>
<title>#{app_name}</title>
<%= stylesheet_link_tag 'screen', :media => 'all' %>
<%= stylesheet_link_tag 'layout', :media => 'screen' %>
<%= stylesheet_link_tag 'print', :media => 'print' %>
<%= javascript_include_tag 'jquery', :cache => true %>
<%= yield(:head) %>
<script type="text/javascript">
$(function() {
<%= yield(:jquery) %>
});
</script>
</head>
<body id="www-#{app_name}-com">
<div id="AccessibilityLinks">
<a name="PageTop" id="PageTop"><!-- link target --></a>
<p><strong>Jump to:</strong>
<a href="#ContentTop" title="Skip to this page's main content">Page Content</a>,
<a href="#SiteNavigation" title="Skip to the site navigation">Site Navigation</a>,
<a href="#SiteSearch" title="Skip to the site-wide search form">Site Search</a>,
</p>
</div>
<!-- BEGIN: page -->
<div id="PageWrapper">
<!-- BEGIN: layout -->
<div id="LayoutWrapper">
<!-- BEGIN: header -->
<div id="HeaderWrapper">
</div>
<!-- END: header -->
<hr />
<!-- BEGIN: body -->
<div id="BodyWrapper">
<a id="ContentTop" name="ContentTop"><!-- link target --></a>
<!-- BEGIN: main column -->
<div id="MainColumn">
<%- if flash[:notice] -%>
<div id="flash"><%= flash[:notice] %></div>
<%- end -%>
<%= yield %>
</div>
<!-- END: main column -->
<!-- BEGIN: side column -->
<div id="SideColumn">
</div>
<!-- END: side column -->
</div>
<!-- END: body -->
<hr />
<!-- BEGIN: footer -->
<div id="FooterWrapper">
</div>
<!-- END: footer -->
</div>
<!-- END: layout -->
</div>
<!-- END: page -->
</body>
</html>
FILE
# ===========================================================================
# Initialize Git repository
# ===========================================================================
# newgit.rb
# from Joao Vitor
# Creates a new rails application using git
# Initializes the git based on the sake task published on
# http://gist.github.com/6750
# task 'git:rails:new_app', :needs => [ 'rails:rm_tmp_dirs', 'git:hold_empty_dirs' ]
# rails:rm_tmp_dirs
["./tmp/pids", "./tmp/sessions", "./tmp/sockets", "./tmp/cache"].each do |f|
run("rmdir ./#{f}")
end
# git:hold_empty_dirs
run("find . \\( -type d -empty \\) -and \\( -not -regex ./\\.git.* \\) -exec touch {}/.gitignore \\;")
# git:rails:new_app
git :init
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', <<-CODE
log/\\*.log
log/\\*.pid
db/\\*.db
db/\\*.sqlite3
db/schema.rb
tmp/\\*\\*/\\*
.DS_Store
doc/api
doc/app
config/database.yml
CODE
git :add => "."
git :commit => "-a -m 'Setting up a new rails app. Copy config/database.yml.example to config/database.yml and customize.'"
# gems
gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
gem 'authlogic'
gem 'giraffesoft-resource_controller', :lib => 'resource_controller', :source => 'http://gems.github.com'
rake 'gems:install', :sudo => true
# generators
generate("rspec")
generate("rspec-rails")
# 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
require_user :only => :destroy
resource_controller
actions :new, :create
create do
flash "Login successful!"
success.wants.html do
redirect_back_or_default account_url
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
require_user :only => [:show, :edit, :update]
resource_controller
actions :new, :create, :show, :edit, :update
create do
flash "Account registered!"
success.wants.html do
redirect_back_or_default account_url
end
end
update.flash "Account updated!"
def object
@object ||= current_user
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 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"
# Commit all work so far to the repository
git :init
git :add => '.'
git :commit => "-a -m 'Initial commit'"
rake "test"