Skip to content

HTTPS clone URL

Subversion checkout URL

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