forked from fortuity/rails3-mongoid-devise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.rb
441 lines (395 loc) · 13.4 KB
/
template.rb
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Application Generator Template
# Modifies a Rails app to use Mongoid and Devise
# Usage: rails new app_name -m https://github.com/fortuity/rails3-mongoid-devise/raw/master/template.rb
# 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 and Devise..."
puts "Any problems? See http://github.com/fortuity/rails3-mongoid-devise/issues"
#----------------------------------------------------------------------------
# Configure
#----------------------------------------------------------------------------
if yes?('Would you like to use the Haml template system? (yes/no)')
haml_flag = true
else
haml_flag = false
end
if yes?('Would you like to use jQuery instead of Prototype? (yes/no)')
jquery_flag = true
else
jquery_flag = false
end
if yes?('Do you want to install the Heroku gem so you can deploy to Heroku? (yes/no)')
heroku_flag = true
else
heroku_flag = false
end
#----------------------------------------------------------------------------
# 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 "banning spiders from your site by changing robots.txt..."
gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
#----------------------------------------------------------------------------
# Heroku Option
#----------------------------------------------------------------------------
if heroku_flag
puts "adding Heroku gem to the Gemfile..."
gem 'heroku', '1.13.7', :group => :development
end
#----------------------------------------------------------------------------
# Haml Option
#----------------------------------------------------------------------------
if haml_flag
puts "setting up Gemfile for Haml..."
append_file 'Gemfile', "\n# Bundle gems needed for Haml\n"
gem 'haml', '3.0.24'
gem 'haml-rails', '0.3.4', :group => :development
# the following gems are used to generate Devise views for Haml
gem 'hpricot', '0.8.3', :group => :development
gem 'ruby_parser', '2.0.5', :group => :development
end
#----------------------------------------------------------------------------
# jQuery Option
#----------------------------------------------------------------------------
if jquery_flag
gem 'jquery-rails', '0.2.5'
end
#----------------------------------------------------------------------------
# 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.20"
gem 'bson_ext', '1.1.2'
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/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
#----------------------------------------------------------------------------
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
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 jQuery
#----------------------------------------------------------------------------
if jquery_flag
run 'rm public/javascripts/rails.js'
puts "replacing Prototype with jQuery"
# "--ui" enables optional jQuery UI
run 'rails generate jquery:install --ui'
end
#----------------------------------------------------------------------------
# Set up Devise
#----------------------------------------------------------------------------
puts "setting up Gemfile for Devise..."
append_file 'Gemfile', "\n# Bundle gem needed for Devise\n"
gem 'devise', '1.1.3'
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'
run 'rails generate devise:views'
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, :remember_me
end
RUBY
end
#----------------------------------------------------------------------------
# Modify Devise views
#----------------------------------------------------------------------------
puts "modifying the default Devise user registration to add 'name'..."
if haml_flag
inject_into_file "app/views/devise/registrations/edit.html.haml", :after => "= devise_error_messages!\n" do
<<-RUBY
%p
= f.label :name
%br/
= f.text_field :name
RUBY
end
else
inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
<<-RUBY
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
RUBY
end
end
if haml_flag
inject_into_file "app/views/devise/registrations/new.html.haml", :after => "= devise_error_messages!\n" do
<<-RUBY
%p
= f.label :name
%br/
= f.text_field :name
RUBY
end
else
inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
<<-RUBY
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
RUBY
end
end
#----------------------------------------------------------------------------
# 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
if haml_flag
run 'rm app/views/home/index.html.haml'
# we have to use single-quote-style-heredoc to avoid interpolation
create_file 'app/views/home/index.html.haml' do
<<-'FILE'
- @users.each do |user|
%p User: #{link_to user.name, user}
FILE
end
else
append_file 'app/views/home/index.html.erb' do <<-FILE
<% @users.each do |user| %>
<p>User: <%=link_to user.name, user %></p>
<% end %>
FILE
end
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
if haml_flag
run 'rm app/views/users/show.html.haml'
# we have to use single-quote-style-heredoc to avoid interpolation
create_file 'app/views/users/show.html.haml' do <<-'FILE'
%p
User: #{@user.name}
FILE
end
else
append_file 'app/views/users/show.html.erb' do <<-FILE
<p>User: <%= @user.name %></p>
FILE
end
end
if haml_flag
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
else
create_file "app/views/devise/menu/_login_items.html.erb" do <<-FILE
<% if user_signed_in? %>
<li>
<%= link_to('Logout', destroy_user_session_path) %>
</li>
<% else %>
<li>
<%= link_to('Login', new_user_session_path) %>
</li>
<% end %>
FILE
end
end
if haml_flag
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
else
create_file "app/views/devise/menu/_registration_items.html.erb" do <<-FILE
<% if user_signed_in? %>
<li>
<%= link_to('Edit account', edit_user_registration_path) %>
</li>
<% else %>
<li>
<%= link_to('Sign up', new_user_registration_path) %>
</li>
<% end %>
FILE
end
end
#----------------------------------------------------------------------------
# Generate Application Layout
#----------------------------------------------------------------------------
if haml_flag
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 :defaults
= 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
else
inject_into_file 'app/views/layouts/application.html.erb', :after => "<body>\n" do
<<-RUBY
<ul class="hmenu">
<%= render 'devise/menu/registration_items' %>
<%= render 'devise/menu/login_items' %>
</ul>
<p style="color: green"><%= notice %></p>
<p style="color: red"><%= alert %></p>
RUBY
end
end
#----------------------------------------------------------------------------
# Add Stylesheets
#----------------------------------------------------------------------------
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
#----------------------------------------------------------------------------
# Create a default user
#----------------------------------------------------------------------------
puts "creating a default user"
append_file 'db/seeds.rb' do <<-FILE
puts 'EMPTY THE MONGODB DATABASE'
Mongoid.master.collections.reject { |c| c.name == 'system.indexes'}.each(&:drop)
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'First User', :email => '[email protected]', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name
FILE
end
run 'rake db:seed'
#----------------------------------------------------------------------------
# Finish up
#----------------------------------------------------------------------------
puts "checking everything into git..."
git :add => '.'
git :commit => "-am 'modified Rails app to use Mongoid and Devise'"
puts "Done setting up your Rails app with Mongoid and Devise."