Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 444 lines (371 sloc) 13.302 kb
0cb65ba5 »
2010-11-17 added mongoid template
1 # Application Generator Template
2 # Usage: rails new app_name -m rails3-mongoid.rb
3
4 #----------------------------------------------------------------------------
5 # Set up git
6 #----------------------------------------------------------------------------
7 puts "setting up source control with 'git'..."
8 # specific to Mac OS X
9 append_file '.gitignore' do
10 '.DS_Store'
11 end
12 git :init
13 git :add => '.'
14 git :commit => "-m 'Initial commit of unmodified new Rails app'"
15
16 #----------------------------------------------------------------------------
17 # Remove the usual cruft
18 #----------------------------------------------------------------------------
19 puts "removing unneeded files..."
20 run 'rm config/database.yml'
21 run 'rm public/index.html'
22 run 'rm public/favicon.ico'
23 run 'rm public/images/rails.png'
24 run 'rm README'
25 run 'touch README.md'
26
27 #----------------------------------------------------------------------------
28 # Gems
29 #----------------------------------------------------------------------------
30 puts "setting up Gemfile..."
7a0dd287 »
2011-01-20 updated gems
31
e271cdcb »
2011-01-20 fixed the mongoid issues
32 gsub_file 'Gemfile', /gem \'sqlite3-ruby/, '# gem \'sqlite3-ruby'
7a0dd287 »
2011-01-20 updated gems
33
e271cdcb »
2011-01-20 fixed the mongoid issues
34 open('Gemfile', 'a') do |f|
35 f.puts <<-RUBY
36 gem 'rails', '3.0.3'
37
38 # mondodb document database
39 gem 'mongoid', '2.0.0.rc.6'
40 gem 'bson_ext', '1.2.0'
41
42 # for markdown content
43 gem 'rdiscount', '1.6.5'
44
45 # haml alternative to erb for template markup
46 gem 'haml', '3.0.25'
47 gem 'jquery-rails', '0.2.6'
48
49 # accounts and authentication
50 gem 'devise', '1.1.5'
51
52 # image uploads
53 gem 'mini_magick', '3.2'
54 gem 'carrierwave', '0.5.1'
55
56 # thin controllers
57 gem 'inherited_resources', '1.1.2'
58 gem 'has_scope', '0.5.0'
59 gem 'responders', '0.6.2'
60
61 # pagination
62 gem 'will_paginate', '3.0.pre2'
63
64 # simple forms
65 gem 'simple_form', '1.3.0'
66
67 group :development do
68 gem 'haml-rails', '0.3.4'
69 gem 'hpricot', '0.8.3'
70 gem 'ruby_parser', '2.0.5'
71 gem 'nifty-generators', '0.4.4'
72 gem RUBY_VERSION >= "1.9" ? 'ruby-debug19' : 'ruby-debug'
73 gem 'capistrano', '2.5.19'
74 gem 'mongrel', '1.2.0.pre2'
75 end
76
77 group :development, :test do
78 # testing
79 gem 'shoulda', '2.11.3'
80 gem 'factory_girl_rails', '1.1.beta1'
81 end
82 RUBY
7a0dd287 »
2011-01-20 updated gems
83 end
0cb65ba5 »
2010-11-17 added mongoid template
84
85 puts "installing gems (takes a few minutes!)..."
86 run 'bundle install'
87
88 #----------------------------------------------------------------------------
89 # Set up Mongoid
90 #----------------------------------------------------------------------------
91
92 puts "modifying 'config/application.rb' file for Mongoid..."
93 gsub_file 'config/application.rb', /require 'rails\/all'/ do
94 <<-RUBY
95 # If you are deploying to Heroku and MongoHQ,
96 # you supply connection information here.
97 require 'uri'
98 if ENV['MONGOHQ_URL']
99 mongo_uri = URI.parse(ENV['MONGOHQ_URL'])
100 ENV['MONGOID_HOST'] = mongo_uri.host
101 ENV['MONGOID_PORT'] = mongo_uri.port.to_s
102 ENV['MONGOID_USERNAME'] = mongo_uri.user
103 ENV['MONGOID_PASSWORD'] = mongo_uri.password
104 ENV['MONGOID_DATABASE'] = mongo_uri.path.gsub('/', '')
105 end
106
107 require 'mongoid/railtie'
108 require 'action_controller/railtie'
109 require 'action_mailer/railtie'
110 require 'active_resource/railtie'
111 require 'rails/test_unit/railtie'
112 RUBY
113 end
114
c8e08244 »
2010-11-17 fixed haml indentation
115 puts "creating 'config/mongoid.yml' Mongoid configuration file..."
116 run 'rails generate mongoid:config'
117
0cb65ba5 »
2010-11-17 added mongoid template
118 #----------------------------------------------------------------------------
119 # Tweak config/application.rb for Mongoid
120 #----------------------------------------------------------------------------
121 gsub_file 'config/application.rb', /# Configure the default encoding used in templates for Ruby 1.9./ do
122 <<-RUBY
123 config.generators do |g|
124 g.orm :mongoid
125 end
126
127 # Configure the default encoding used in templates for Ruby 1.9.
128 RUBY
129 end
130
131 puts "prevent logging of passwords"
132 gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
133
134 #----------------------------------------------------------------------------
135 # Generate the application config (nifty gen)
136 #----------------------------------------------------------------------------
137 run 'rails g nifty:config'
138
139 #----------------------------------------------------------------------------
140 # Set up jQuery
141 #----------------------------------------------------------------------------
142 run 'rm public/javascripts/rails.js'
143 puts "replacing Prototype with jQuery"
144 # "--ui" enables optional jQuery UI
145 run 'rails generate jquery:install --ui'
146
147 #----------------------------------------------------------------------------
148 # Set up Devise
149 #----------------------------------------------------------------------------
150 puts "creating 'config/initializers/devise.rb' Devise configuration file..."
151 run 'rails generate devise:install'
152 run 'rails generate devise:views'
153
154 puts "modifying environment configuration files for Devise..."
155 gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '### ActionMailer Config'
156 gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
157 <<-RUBY
158 config.action_mailer.default_url_options = { :host => 'localhost:3000' }
159 # A dummy setup for development - no deliveries, but logged
160 config.action_mailer.delivery_method = :smtp
161 config.action_mailer.perform_deliveries = false
162 config.action_mailer.raise_delivery_errors = true
163 config.action_mailer.default :charset => "utf-8"
164 RUBY
165 end
166 gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
167 <<-RUBY
168 config.i18n.fallbacks = true
169
170 config.action_mailer.default_url_options = { :host => 'yourhost.com' }
171 ### ActionMailer Config
172 # Setup for production - deliveries, no errors raised
173 config.action_mailer.delivery_method = :smtp
174 config.action_mailer.perform_deliveries = true
175 config.action_mailer.raise_delivery_errors = false
176 config.action_mailer.default :charset => "utf-8"
177 RUBY
178 end
179
180 puts "creating a User model and modifying routes for Devise..."
181 run 'rails generate devise User'
182
183 puts "adding a 'name' attribute to the User model"
184 gsub_file 'app/models/user.rb', 'attr_accessible ', 'attr_accessible :name, '
185 gsub_file 'app/models/user.rb', /end/ do
186 <<-RUBY
187 field :name
188 validates_presence_of :name
189 validates_uniqueness_of :name, :email, :case_sensitive => false
190 end
191 RUBY
192 end
193
194
195 #----------------------------------------------------------------------------
196 # Modify Devise views
197 #----------------------------------------------------------------------------
198 puts "implement simple_form in devise views"
199 gsub_file 'app/views/devise/confirmations/new.html.haml', 'form_for', 'simple_form_for'
200 gsub_file 'app/views/devise/passwords/new.html.haml', 'form_for', 'simple_form_for'
201 gsub_file 'app/views/devise/passwords/edit.html.haml', 'form_for', 'simple_form_for'
202 gsub_file 'app/views/devise/registrations/new.html.haml', 'form_for', 'simple_form_for'
203 gsub_file 'app/views/devise/registrations/edit.html.haml', 'form_for', 'simple_form_for'
204 gsub_file 'app/views/devise/sessions/new.html.haml', 'form_for', 'simple_form_for'
205 gsub_file 'app/views/devise/unlocks/new.html.haml', 'form_for', 'simple_form_for'
206
207 puts "modifying the default Devise user registration to add 'name'..."
208 inject_into_file "app/views/devise/registrations/edit.html.haml", :after => "= devise_error_messages!\n" do
209 <<-RUBY
c8e08244 »
2010-11-17 fixed haml indentation
210 %p
211 = f.label :name
212 %br/
213 = f.text_field :name
0cb65ba5 »
2010-11-17 added mongoid template
214 RUBY
215 end
216
217 inject_into_file "app/views/devise/registrations/new.html.haml", :after => "= devise_error_messages!\n" do
218 <<-RUBY
c8e08244 »
2010-11-17 fixed haml indentation
219 %p
220 = f.label :name
221 %br/
222 = f.text_field :name
0cb65ba5 »
2010-11-17 added mongoid template
223 RUBY
224 end
225
226
227 #----------------------------------------------------------------------------
228 # Create a home page
229 #----------------------------------------------------------------------------
230 puts "create a home controller and view"
231 generate(:controller, "home index")
232 gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
233
234 puts "set up a simple demonstration of Devise"
235 gsub_file 'app/controllers/home_controller.rb', /def index/ do
236 <<-RUBY
237 def index
238 @users = User.all
239 RUBY
240 end
241
242 run 'rm app/views/home/index.html.haml'
243 # we have to use single-quote-style-heredoc to avoid interpolation
244 create_file 'app/views/home/index.html.haml' do
245 <<-'FILE'
246 - @users.each do |user|
247 %p User: #{link_to user.name, user}
248 FILE
249 end
250
251
252 #----------------------------------------------------------------------------
253 # Create a users page
254 #----------------------------------------------------------------------------
255 generate(:controller, "users show")
256 gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
257 gsub_file 'config/routes.rb', /devise_for :users/ do
258 <<-RUBY
259 devise_for :users
260 resources :users, :only => :show
261 RUBY
262 end
263
264 gsub_file 'app/controllers/users_controller.rb', /def show/ do
265 <<-RUBY
266 before_filter :authenticate_user!
267
268 def show
269 @user = User.find(params[:id])
270 RUBY
271 end
272
273 run 'rm app/views/users/show.html.haml'
274 # we have to use single-quote-style-heredoc to avoid interpolation
275 create_file 'app/views/users/show.html.haml' do <<-'FILE'
276 %p
277 User: #{@user.name}
278 FILE
279 end
280
281
282 create_file "app/views/devise/menu/_login_items.html.haml" do <<-'FILE'
283 - if user_signed_in?
284 %li
285 = link_to('Logout', destroy_user_session_path)
286 - else
287 %li
288 = link_to('Login', new_user_session_path)
289 FILE
290 end
291
292
293 create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'FILE'
294 - if user_signed_in?
295 %li
296 = link_to('Edit account', edit_user_registration_path)
297 - else
298 %li
299 = link_to('Sign up', new_user_registration_path)
300 FILE
301 end
302
303
304 #----------------------------------------------------------------------------
305 # Create the admin namespace and user management
306 #----------------------------------------------------------------------------
307 puts "Setting up admin namespace and user management."
308
309 create_file 'app/controllers/admin/users_controller.rb' do <<-FILE
310 class Admin::UsersController < InheritedResources::Base
311 layout 'admin'
312 respond_to :html
313 before_filter :authenticate_admin!
314
315 protected #----
316 def collection
317 @users ||= end_of_association_chain.paginate(:page => params[:page], :per_page => 20, :sort => 'name')
318 end
319 end
320 FILE
321 end
322
323 inject_into_file "app/controllers/application_controller.rb", :after => "protect_from_forgery\n" do
324 <<-RUBY
325
326 protected #--------
327
328 # just using basic auth for the admin section
329 def authenticate_admin!
330 authenticate_or_request_with_http_basic do |user_name, password|
331 user_name == 'replace_me' && password == 'replace_me'
332 end if RAILS_ENV == 'production' || params[:admin_http]
333 end
334
335 RUBY
336 end
337
338 # namespace
339 inject_into_file "config/routes.rb", :after => "resources :users, :only => :show\n" do
340 <<-RUBY
341
342 match 'admin' => 'admin/users#index'
343 namespace :admin do
344 resources :users
345 end
346
347 RUBY
348 end
349
350 #----------------------------------------------------------------------------
351 # Generate Application Layout
352 #----------------------------------------------------------------------------
353
354 run 'rm app/views/layouts/application.html.erb'
355 create_file 'app/views/layouts/application.html.haml' do <<-FILE
356 !!!
357 %html
358 %head
359 %title Testapp
360 = stylesheet_link_tag :all
361 = javascript_include_tag :defaults
362 = csrf_meta_tag
363 %body
364 %ul.hmenu
365 = render 'devise/menu/registration_items'
366 = render 'devise/menu/login_items'
367 %p{:style => "color: green"}= notice
368 %p{:style => "color: red"}= alert
369 = yield
370 FILE
371 end
372
373 create_file 'app/views/layouts/admin.html.haml' do <<-FILE
374 !!!
375 %html
376 %head
377 %title Testapp Admin
378 = stylesheet_link_tag :all
379 = javascript_include_tag :defaults
380 = csrf_meta_tag
381 %body
382 %p{:style => "color: green"}= notice
383 %p{:style => "color: red"}= alert
384 = yield
385 FILE
386 end
387
388
389 #----------------------------------------------------------------------------
390 # Add Stylesheets
391 #----------------------------------------------------------------------------
392 create_file 'public/stylesheets/sass/application.sass' do <<-FILE
393 ul.hmenu
394 list-style: none
395 margin: 0 0 2em
396 padding: 0
397
398 ul.hmenu li
399 display: inline
400 FILE
401 end
402
403 create_file 'public/stylesheets/sass/admin.sass' do <<-FILE
404 // admin styles
405 FILE
406 end
407
408
409 #----------------------------------------------------------------------------
7a0dd287 »
2011-01-20 updated gems
410 # Simple Form
411 #----------------------------------------------------------------------------
412 run "rails generate simple_form:install"
413
414
415 #----------------------------------------------------------------------------
0cb65ba5 »
2010-11-17 added mongoid template
416 # Creatives
417 #----------------------------------------------------------------------------
418 create_file 'creatives/src/README.mb' do <<-FILE
419 # Photoshop files etc.
420 FILE
421 end
422
423
424 #----------------------------------------------------------------------------
425 # Create a default user
426 #----------------------------------------------------------------------------
427 append_file 'db/seeds.rb' do <<-FILE
428 puts 'Creating default user...'
429 user = User.create! :name => 'First User', :email => 'user@test.com', :password => 'password', :password_confirmation => 'password'
430 puts 'New user created: ' << user.name
431 FILE
432 end
433 puts "Seed the database"
c8e08244 »
2010-11-17 fixed haml indentation
434 run 'rake db:drop'
0cb65ba5 »
2010-11-17 added mongoid template
435 run 'rake db:seed'
436
437 #----------------------------------------------------------------------------
438 # Finish up
439 #----------------------------------------------------------------------------
440 puts "checking everything into git..."
441 git :add => '.'
442 git :commit => "-am 'Initial commit'"
443
444 puts "Done setting up your Rails app."
Something went wrong with that request. Please try again.