Skip to content

Instantly share code, notes, and snippets.

@jerefrer
Forked from foca/rails-template.rb
Created January 5, 2010 13:13
Show Gist options
  • Save jerefrer/269366 to your computer and use it in GitHub Desktop.
Save jerefrer/269366 to your computer and use it in GitHub Desktop.
with_options :env => "test" do |test|
test.gem "cucumber"
test.gem "cucumber-rails"
end
with_options :env => "development" do |dev|
dev.gem "hirb"
end
gem "authlogic"
gem "will_paginate"
gem "haml" # only used for sass so compass works
gem "ruby-debug" # Needed so app:boostrap works
gem "highline" # Needed so app:boostrap works
plugin "app_bootstrap", :git => "git://github.com/technoweenie/app_bootstrap.git"
file "config/routes.rb", <<-CODE
ActionController::Routing::Routes.draw do |map|
map.login "/login", :controller => "user_sessions", :action => "new"
map.logout "/logout", :controller => "user_sessions", :action => "destroy"
map.resource :user_session
end
CODE
file ".gitignore", <<-CODE
config/database.yml
config/*.sphinx.conf
db/*.sqlite*
db/sphinx/*
log/*
tmp/*
.DS_Store
CODE
run "rm public/index.html"
run "rm log/*"
run "rm README"
run "cp config/database.yml config/database.sample.yml"
log "You need to press 'y<CR>n<CR>' because of compass' questions"
run "compass --rails -f blueprint ."
generate "session", "user_session"
user_fields = { :name => "string",
:email => "string",
:crypted_password => "string",
:password_salt => "string",
:persistence_token => "string",
:single_access_token => "string",
:perishable_token => "string",
:last_login_at => "datetime" }
generate "model", "user #{user_fields.map {|f,t| "#{f}:#{t}" }.join(" ")}"
run "rm -rf test/fixtures"
file "app/models/user.rb", <<-CODE
class User < ActiveRecord::Base
acts_as_authentic
end
CODE
file "app/controllers/user_sessions_controller.rb", <<-CODE
class UserSessionsController < ApplicationController
before_filter :allow_logged_in_only, :only => :destroy
before_filter :allow_logged_out_only, :only => [:new, :create]
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 root_url
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default root_url
end
end
CODE
file "app/controllers/application_controller.rb", <<-CODE
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
filter_parameter_logging :password
helper_method :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.record
end
def logged_in?
!current_user.nil?
end
def allow_logged_in_only
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 allow_logged_out_only
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.get? ? request.request_uri : request.referer
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end
CODE
run "mkdir test/blueprints"
file "test/blueprints.rb", <<-CODE
require 'machinist/active_record'
require 'sham'
Sham.define do
end
Dir[File.dirname(__FILE__) + "/blueprints/*.rb"].each {|f| require f }
CODE
file "test/test_helper.rb", <<-CODE
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require File.expand_path(File.dirname(__FILE__) + "/blueprints")
require 'test_help'
class ActiveSupport::TestCase
setup { Sham.reset }
end
CODE
git :init
git :add => "."
git :commit => "-m 'Initial commit'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment