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