public
Last active — forked from fortuity/gist:452364

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
# Application Generator Template
# Modifies a Rails app to use Mongoid, Devise, jQuery, Haml
# Usage: rails new app_name -m http://gist.github.com/raw/555181/gistfile1.txt
 
# More info: http://github.com/fortuity/rails3-mongoid-devise/
 
# If you are customizing this template, you can use any methods provided by Thor::Actions
# http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
# and Rails::Generators::Actions
# http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
 
puts "Modifying a new Rails app to use Mongoid, Devise, jQuery, Haml..."
 
#----------------------------------------------------------------------------
# Set up git
#----------------------------------------------------------------------------
puts "setting up source control with 'git'..."
# specific to Mac OS X
append_file '.gitignore' do
'.DS_Store'
end
git :init
git :add => '.'
git :commit => "-m 'Initial commit of unmodified new Rails app'"
 
#----------------------------------------------------------------------------
# Remove the usual cruft
#----------------------------------------------------------------------------
puts "removing unneeded files..."
run 'rm config/database.yml'
run 'rm public/index.html'
run 'rm public/favicon.ico'
run 'rm public/images/rails.png'
run 'rm README'
run 'touch README'
 
puts "ban spiders from your site..."
gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
 
#----------------------------------------------------------------------------
# Set up Heroku
#----------------------------------------------------------------------------
puts "setting up Gemfile for Heroku..."
append_file 'Gemfile', "\n# Bundle gem needed for Heroku\n"
gem 'heroku', '1.9.13', :group => :development
 
#----------------------------------------------------------------------------
# Set up Haml
#----------------------------------------------------------------------------
puts "setting up Gemfile for Haml..."
append_file 'Gemfile', "\n# Bundle gems needed for Haml\n"
gem 'haml', '3.0.15'
gem "rails3-generators", '0.11.0', :group => :development
 
#----------------------------------------------------------------------------
# Set up Mongoid
#----------------------------------------------------------------------------
puts "setting up Gemfile for Mongoid..."
gsub_file 'Gemfile', /gem \'sqlite3-ruby/, '# gem \'sqlite3-ruby'
append_file 'Gemfile', "\n# Bundle gems needed for Mongoid\n"
#gem 'mongoid', '2.0.0.beta.16'
gem "mongoid", :git => "http://github.com/mongoid/mongoid.git"
gem 'bson_ext', '1.0.4'
 
puts "installing Mongoid gems (takes a few minutes!)..."
run 'bundle install'
 
puts "creating 'config/mongoid.yml' Mongoid configuration file..."
run 'rails generate mongoid:config'
 
puts "modifying 'config/mongoid.yml' Mongoid Configuration file..."
puts "create a host entry for mongo1, be it localhost or VM."
gsub_file 'config/mongoid.yml', /host: localhost/, 'host: mongo1'
 
 
puts "modifying 'config/application.rb' file for Mongoid..."
gsub_file 'config/application.rb', /require 'rails\/all'/ do
<<-RUBY
# If you are deploying to Heroku and MongoHQ,
# you supply connection information here.
require 'uri'
if ENV['MONGOHQ_URL']
mongo_uri = URI.parse(ENV['MONGOHQ_URL'])
ENV['MONGOID_HOST'] = mongo_uri.host
ENV['MONGOID_PORT'] = mongo_uri.port.to_s
ENV['MONGOID_USERNAME'] = mongo_uri.user
ENV['MONGOID_PASSWORD'] = mongo_uri.password
ENV['MONGOID_DATABASE'] = mongo_uri.path.gsub('/', '')
end
 
require 'mongoid/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'active_resource/railtie'
require 'rails/test_unit/railtie'
RUBY
end
 
#----------------------------------------------------------------------------
# Tweak config/application.rb for Mongoid and Haml
#----------------------------------------------------------------------------
gsub_file 'config/application.rb', /# Configure the default encoding used in templates for Ruby 1.9./ do
<<-RUBY
config.generators do |g|
g.orm :mongoid
g.template_engine :haml
end
# Configure the default encoding used in templates for Ruby 1.9.
RUBY
end
 
puts "prevent logging of passwords"
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
 
#----------------------------------------------------------------------------
# Set up Devise
#----------------------------------------------------------------------------
puts "setting up Gemfile for Devise..."
append_file 'Gemfile', "\n# Bundle gem needed for Devise\n"
gem 'devise', '1.1.1'
 
puts "installing Devise gem (takes a few minutes!)..."
run 'bundle install'
 
puts "creating 'config/initializers/devise.rb' Devise configuration file..."
run 'rails generate devise:install'
 
puts "modifying environment configuration files for Devise..."
gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '### ActionMailer Config'
gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
<<-RUBY
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# A dummy setup for development - no deliveries, but logged
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
RUBY
end
gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
<<-RUBY
config.i18n.fallbacks = true
 
config.action_mailer.default_url_options = { :host => 'yourhost.com' }
### ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
RUBY
end
 
puts "creating a User model and modifying routes for Devise..."
run 'rails generate devise User'
 
puts "adding a 'name' attribute to the User model"
gsub_file 'app/models/user.rb', /end/ do
<<-RUBY
field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation
end
RUBY
end
 
#----------------------------------------------------------------------------
# Create a default user
#----------------------------------------------------------------------------
puts "creating a default user"
append_file 'db/seeds.rb' do <<-FILE
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name
FILE
end
run 'rake db:seed'
 
#----------------------------------------------------------------------------
# Create a home page
#----------------------------------------------------------------------------
puts "create a home controller and view"
generate(:controller, "home index")
gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
 
puts "set up a simple demonstration of Devise"
gsub_file 'app/controllers/home_controller.rb', /def index/ do
<<-RUBY
def index
@users = User.all
RUBY
end
 
# we have to use single-quote-style-heredoc to avoid interpolation
append_file 'app/views/home/index.html.haml' do <<-'FILE'
 
- @users.each do |user|
%p
User: #{link_to user.name, user}
FILE
end
 
#----------------------------------------------------------------------------
# Create a users page
#----------------------------------------------------------------------------
generate(:controller, "users show")
gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
gsub_file 'config/routes.rb', /devise_for :users/ do
<<-RUBY
devise_for :users
resources :users, :only => :show
RUBY
end
 
gsub_file 'app/controllers/users_controller.rb', /def show/ do
<<-RUBY
before_filter :authenticate_user!
 
def show
@user = User.find(params[:id])
RUBY
end
 
# we have to use single-quote-style-heredoc to avoid interpolation
append_file 'app/views/users/show.html.haml' do <<-'FILE'
 
%p
User: #{@user.name}
FILE
end
 
create_file "app/views/devise/menu/_login_items.html.haml" do <<-'FILE'
- if user_signed_in?
%li
= link_to('Logout', destroy_user_session_path)
- else
%li
= link_to('Login', new_user_session_path)
FILE
end
 
create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'FILE'
- if user_signed_in?
%li
= link_to('Edit account', edit_user_registration_path)
- else
%li
= link_to('Sign up', new_user_registration_path)
FILE
end
 
#----------------------------------------------------------------------------
# Generate Application Layout
#----------------------------------------------------------------------------
run 'rm app/views/layouts/application.html.erb'
create_file 'app/views/layouts/application.html.haml' do <<-FILE
!!!
%html
%head
%title Testapp
= stylesheet_link_tag :all
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js'
= javascript_include_tag 'rails'
= csrf_meta_tag
%body
%ul.hmenu
= render 'devise/menu/registration_items'
= render 'devise/menu/login_items'
%p{:style => "color: green"}= notice
%p{:style => "color: red"}= alert
= yield
FILE
end
 
create_file 'public/stylesheets/application.css' do <<-FILE
ul.hmenu {
list-style: none;
margin: 0 0 2em;
padding: 0;
}
 
ul.hmenu li {
display: inline;
}
FILE
end
 
puts "checking everything into git..."
git :add => '.'
git :commit => "-a -m 'modified Rails app to use Mongoid, Devise, jQuery, Haml'"
 
puts "Done setting up your Rails app with Mongoid, Devise, jQuery, Haml."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.