├── .gitignore
├── README
├── README.textile
├── files
└── rails3-mongoid-devise
│ └── app
│ └── views
│ └── devise
│ └── registrations
│ ├── edit.html.haml
│ └── new.html.haml
├── rails3-devise-rspec-cucumber-template.rb
├── rails3-mongoid-devise-template.rb
├── rails3-mongoid-omniauth-template.rb
└── rails3-subdomain-devise-template.rb
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle
2 | db/*.sqlite3
3 | log/*.log
4 | tmp/**/*
5 | .DS_Store
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | Rails3-Application-Templates
2 | ========================
3 |
4 | You can use these templates to generate a Rails web application.
5 | ________________________
6 |
7 | See the README file on GitHub
8 |
9 | For more information, please see the updated README file on GitHub:
10 |
11 | http://github.com/fortuity/rails3-application-templates
12 |
13 | ________________________
14 |
15 | Public Domain Dedication
16 |
17 | This work is a compilation and derivation from other previously released works. With the exception of various included works, which may be restricted by other licenses, the author or authors of this code dedicate any and all copyright interest in this code to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/README.textile:
--------------------------------------------------------------------------------
1 | h1. Rails 3 Application Templates
2 |
3 | These application generator templates give you a Rails 3.0 or 3.1 starter app you can deploy in minutes.
4 |
5 | h2. The Project Has Moved!
6 |
7 | Please visit the project at its "new home":https://github.com/RailsApps/rails3-application-templates:
8 |
9 | h4. "Rails 3 Application Templates":https://github.com/RailsApps/rails3-application-templates
10 |
11 | h2. !http://twitter-badges.s3.amazonaws.com/t_logo-a.png(Follow on Twitter)!:http://www.twitter.com/rails_apps Follow on Twitter !http://railsapps.github.com/images/mailing-list-icon.jpg(Join the Mailing List)!:http://eepurl.com/dQx3o Join the Mailing List
12 |
13 | Follow the project on Twitter: "http://twitter.com/rails_apps":http://twitter.com/rails_apps. Please tweet some praise if you like what you've found.
14 |
15 | Join the email list (low volume, announcements only) for project updates.
16 |
17 | h2. Issues
18 |
19 | Any issues? Please create an "issue":http://github.com/RailsApps/rails3-application-templates/issues on GitHub.
20 |
21 | h2. Credits
22 |
23 | Daniel Kehoe ("http://danielkehoe.com/":http://danielkehoe.com/) created these templates.
24 |
25 | h2. License
26 |
27 | h4. Public Domain Dedication
28 |
29 | This work is a compilation and derivation from other previously released works. With the exception of various included works, which may be restricted by other licenses, the author or authors of this code dedicate any and all copyright interest in this code to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.
30 |
--------------------------------------------------------------------------------
/files/rails3-mongoid-devise/app/views/devise/registrations/edit.html.haml:
--------------------------------------------------------------------------------
1 | %h2
2 | Edit #{resource_name.to_s.humanize}
3 | = form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f|
4 | = devise_error_messages!
5 | %p
6 | = f.label :name
7 | %br/
8 | = f.text_field :name
9 | %p
10 | = f.label :email
11 | %br/
12 | = f.email_field :email
13 | %p
14 | = f.label :password
15 | %i (leave blank if you don't want to change it)
16 | %br/
17 | = f.password_field :password
18 | %p
19 | = f.label :password_confirmation
20 | %br/
21 | = f.password_field :password_confirmation
22 | %p
23 | = f.label :current_password
24 | %i (we need your current password to confirm your changes)
25 | %br/
26 | = f.password_field :current_password
27 | %p= f.submit "Update"
28 | %h3 Cancel my account
29 | %p
30 | Unhappy? #{link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete}.
31 | = link_to "Back", :back
32 |
--------------------------------------------------------------------------------
/files/rails3-mongoid-devise/app/views/devise/registrations/new.html.haml:
--------------------------------------------------------------------------------
1 | %h2 Sign up
2 | = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
3 | = devise_error_messages!
4 | %p
5 | = f.label :name
6 | %br/
7 | = f.text_field :name
8 | %p
9 | = f.label :email
10 | %br/
11 | = f.email_field :email
12 | %p
13 | = f.label :password
14 | %br/
15 | = f.password_field :password
16 | %p
17 | = f.label :password_confirmation
18 | %br/
19 | = f.password_field :password_confirmation
20 | %p= f.submit "Sign up"
21 | = render :partial => "devise/shared/links"
22 |
--------------------------------------------------------------------------------
/rails3-devise-rspec-cucumber-template.rb:
--------------------------------------------------------------------------------
1 | # Application Generator Template
2 | # Modifies a Rails app to use Devise with RSpec and Cucumber
3 | # Usage: rails new APP_NAME -m https://github.com/fortuity/rails3-application-templates/raw/master/rails3-devise-rspec-cucumber-template.rb -T -J
4 |
5 | # Information and a tutorial:
6 | # https://github.com/fortuity/rails3-devise-rspec-cucumber
7 |
8 | # Generated using the rails3_devise_wizard gem:
9 | # https://github.com/fortuity/rails3_devise_wizard/
10 |
11 | # Based on application template recipes by:
12 | # Michael Bleigh https://github.com/mbleigh
13 | # Fletcher Nichol https://github.com/fnichol
14 | # Daniel Kehoe https://github.com/fortuity
15 | # Ramon Brooker https://github.com/cognition
16 |
17 | # If you are customizing this template, you can use any methods provided by Thor::Actions
18 | # http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
19 | # and Rails::Generators::Actions
20 | # http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
21 |
22 | # >---------------------------------------------------------------------------<
23 | #
24 | # _____ _ _ __ ___ _
25 | # | __ \ (_) | \ \ / (_) | |
26 | # | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
27 | # | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
28 | # | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
29 | # |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
30 | #
31 | # This template was generated by rails3_devise_wizard, a custom version of
32 | # RailsWizard, the application template builder. For more information, see:
33 | # https://github.com/fortuity/rails3_devise_wizard/
34 | #
35 | # >---------------------------------------------------------------------------<
36 |
37 | # >----------------------------[ Initial Setup ]------------------------------<
38 |
39 | initializer 'generators.rb', <<-RUBY
40 | Rails.application.config.generators do |g|
41 | end
42 | RUBY
43 |
44 | @recipes = ["jquery", "haml", "rspec", "cucumber", "action_mailer", "devise", "add_user", "home_page", "home_page_users", "seed_database", "users_page", "css_setup", "application_layout", "devise_navigation", "cleanup", "ban_spiders", "git"]
45 |
46 | def recipes; @recipes end
47 | def recipe?(name); @recipes.include?(name) end
48 |
49 | def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
50 | def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
51 | def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end
52 |
53 | def ask_wizard(question)
54 | ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
55 | end
56 |
57 | def yes_wizard?(question)
58 | answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
59 | case answer.downcase
60 | when "yes", "y"
61 | true
62 | when "no", "n"
63 | false
64 | else
65 | yes_wizard?(question)
66 | end
67 | end
68 |
69 | def no_wizard?(question); !yes_wizard?(question) end
70 |
71 | def multiple_choice(question, choices)
72 | say_custom('question', question)
73 | values = {}
74 | choices.each_with_index do |choice,i|
75 | values[(i + 1).to_s] = choice[1]
76 | say_custom (i + 1).to_s + ')', choice[0]
77 | end
78 | answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
79 | values[answer]
80 | end
81 |
82 | @current_recipe = nil
83 | @configs = {}
84 |
85 | @after_blocks = []
86 | def after_bundler(&block); @after_blocks << [@current_recipe, block]; end
87 | @after_everything_blocks = []
88 | def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end
89 | @before_configs = {}
90 | def before_config(&block); @before_configs[@current_recipe] = block; end
91 |
92 |
93 | say_wizard "Checking configuration. Please confirm your preferences."
94 |
95 | # >--------------------------------[ jQuery ]---------------------------------<
96 |
97 | @current_recipe = "jquery"
98 | @before_configs["jquery"].call if @before_configs["jquery"]
99 | say_recipe 'jQuery'
100 |
101 | config = {}
102 | config['jquery'] = yes_wizard?("Would you like to use jQuery instead of Prototype?") if true && true unless config.key?('jquery')
103 | config['ui'] = yes_wizard?("Would you like to use jQuery UI?") if true && true unless config.key?('ui')
104 | @configs[@current_recipe] = config
105 |
106 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
107 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/jquery.rb
108 |
109 | if config['jquery']
110 | say_wizard "REMINDER: When creating a Rails app using jQuery..."
111 | say_wizard "you should add the '-J' flag to 'rails new'"
112 | after_bundler do
113 | say_wizard "jQuery recipe running 'after bundler'"
114 | # remove the Prototype adapter file
115 | remove_file 'public/javascripts/rails.js'
116 | # remove the Prototype files (if they exist)
117 | remove_file 'public/javascripts/controls.js'
118 | remove_file 'public/javascripts/dragdrop.js'
119 | remove_file 'public/javascripts/effects.js'
120 | remove_file 'public/javascripts/prototype.js'
121 | # add jQuery files
122 | inside "public/javascripts" do
123 | get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "rails.js"
124 | get "http://code.jquery.com/jquery-1.6.min.js", "jquery.js"
125 | if config['ui']
126 | get "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js", "jqueryui.js"
127 | end
128 | end
129 | # adjust the Javascript defaults
130 | if config['ui']
131 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery jqueryui rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
132 | else
133 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
134 | end
135 | gsub_file "config/application.rb", /config.action_view.javascript_expansions\[:defaults\] = \%w\(\)\n/, ""
136 | end
137 | else
138 | recipes.delete('jquery')
139 | end
140 |
141 |
142 | # >---------------------------------[ HAML ]----------------------------------<
143 |
144 | @current_recipe = "haml"
145 | @before_configs["haml"].call if @before_configs["haml"]
146 | say_recipe 'HAML'
147 |
148 | config = {}
149 | config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml')
150 | @configs[@current_recipe] = config
151 |
152 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
153 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/haml.rb
154 |
155 | if config['haml']
156 | gem 'haml', '>= 3.1.1'
157 | gem 'haml-rails', '>= 0.3.4', :group => :development
158 | else
159 | recipes.delete('haml')
160 | end
161 |
162 |
163 | # >---------------------------------[ RSpec ]---------------------------------<
164 |
165 | @current_recipe = "rspec"
166 | @before_configs["rspec"].call if @before_configs["rspec"]
167 | say_recipe 'RSpec'
168 |
169 | config = {}
170 | config['rspec'] = yes_wizard?("Would you like to use RSpec instead of TestUnit?") if true && true unless config.key?('rspec')
171 | config['factory_girl'] = yes_wizard?("Would you like to use factory_girl for test fixtures with RSpec?") if true && true unless config.key?('factory_girl')
172 | @configs[@current_recipe] = config
173 |
174 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
175 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/rspec.rb
176 |
177 | if config['rspec']
178 | say_wizard "REMINDER: When creating a Rails app using RSpec..."
179 | say_wizard "you should add the '-T' flag to 'rails new'"
180 | gem 'rspec-rails', '>= 2.5.0', :group => [:development, :test]
181 | if recipes.include? 'mongoid'
182 | # use the database_cleaner gem to reset the test database
183 | gem 'database_cleaner', '>= 0.6.7', :group => :test
184 | # include RSpec matchers from the mongoid-rspec gem
185 | gem 'mongoid-rspec', ">= 1.4.2", :group => :test
186 | end
187 | if config['factory_girl']
188 | # use the factory_girl gem for test fixtures
189 | gem 'factory_girl_rails', ">= 1.1.beta1", :group => :test
190 | end
191 | else
192 | recipes.delete('rspec')
193 | end
194 |
195 | # note: there is no need to specify the RSpec generator in the config/application.rb file
196 |
197 | if config['rspec']
198 | after_bundler do
199 | say_wizard "RSpec recipe running 'after bundler'"
200 | generate 'rspec:install'
201 |
202 | say_wizard "Removing test folder (not needed for RSpec)"
203 | run 'rm -rf test/'
204 |
205 | inject_into_file 'config/application.rb', :after => "Rails::Application\n" do <<-RUBY
206 |
207 | # don't generate RSpec tests for views and helpers
208 | config.generators do |g|
209 | g.view_specs false
210 | g.helper_specs false
211 | end
212 |
213 | RUBY
214 | end
215 |
216 |
217 | if recipes.include? 'mongoid'
218 |
219 | # remove ActiveRecord artifacts
220 | gsub_file 'spec/spec_helper.rb', /config.fixture_path/, '# config.fixture_path'
221 | gsub_file 'spec/spec_helper.rb', /config.use_transactional_fixtures/, '# config.use_transactional_fixtures'
222 |
223 | # reset your application database to a pristine state during testing
224 | inject_into_file 'spec/spec_helper.rb', :before => "\nend" do
225 | <<-RUBY
226 | \n
227 | # Clean up the database
228 | require 'database_cleaner'
229 | config.before(:suite) do
230 | DatabaseCleaner.strategy = :truncation
231 | DatabaseCleaner.orm = "mongoid"
232 | end
233 |
234 | config.before(:each) do
235 | DatabaseCleaner.clean
236 | end
237 | RUBY
238 | end
239 |
240 | # remove either possible occurrence of "require rails/test_unit/railtie"
241 | gsub_file 'config/application.rb', /require 'rails\/test_unit\/railtie'/, '# require "rails/test_unit/railtie"'
242 | gsub_file 'config/application.rb', /require "rails\/test_unit\/railtie"/, '# require "rails/test_unit/railtie"'
243 |
244 | # configure RSpec to use matchers from the mongoid-rspec gem
245 | create_file 'spec/support/mongoid.rb' do
246 | <<-RUBY
247 | RSpec.configure do |config|
248 | config.include Mongoid::Matchers
249 | end
250 | RUBY
251 | end
252 | end
253 |
254 | if recipes.include? 'devise'
255 | # add Devise test helpers
256 | create_file 'spec/support/devise.rb' do
257 | <<-RUBY
258 | RSpec.configure do |config|
259 | config.include Devise::TestHelpers, :type => :controller
260 | end
261 | RUBY
262 | end
263 | end
264 |
265 | end
266 | end
267 |
268 |
269 | # >-------------------------------[ Cucumber ]--------------------------------<
270 |
271 | @current_recipe = "cucumber"
272 | @before_configs["cucumber"].call if @before_configs["cucumber"]
273 | say_recipe 'Cucumber'
274 |
275 | config = {}
276 | config['cucumber'] = yes_wizard?("Would you like to use Cucumber for your BDD?") if true && true unless config.key?('cucumber')
277 | @configs[@current_recipe] = config
278 |
279 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
280 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cucumber.rb
281 |
282 | if config['cucumber']
283 | gem 'cucumber-rails', ">= 0.4.1", :group => :test
284 | gem 'capybara', ">= 0.4.1.2", :group => :test
285 | gem 'database_cleaner', '>= 0.6.7', :group => :test
286 | gem 'launchy', ">= 0.4.0", :group => :test
287 | else
288 | recipes.delete('cucumber')
289 | end
290 |
291 | if config['cucumber']
292 | after_bundler do
293 | say_wizard "Cucumber recipe running 'after bundler'"
294 | generate "cucumber:install --capybara#{' --rspec' if recipes.include?('rspec')}#{' -D' if recipes.include?('mongoid')}"
295 | if recipes.include? 'mongoid'
296 | gsub_file 'features/support/env.rb', /transaction/, "truncation"
297 | inject_into_file 'features/support/env.rb', :after => 'begin' do
298 | "\n DatabaseCleaner.orm = 'mongoid'"
299 | end
300 | end
301 | end
302 | end
303 |
304 | if config['cucumber']
305 | if recipes.include? 'devise'
306 | after_bundler do
307 | say_wizard "Copying Cucumber scenarios from the rails3-mongoid-devise examples"
308 | # copy all the Cucumber scenario files from the rails3-mongoid-devise example app
309 | inside 'features/users' do
310 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_in.feature', 'sign_in.feature'
311 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_out.feature', 'sign_out.feature'
312 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_up.feature', 'sign_up.feature'
313 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_edit.feature', 'user_edit.feature'
314 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_show.feature', 'user_show.feature'
315 | end
316 | inside 'features/step_definitions' do
317 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/step_definitions/user_steps.rb', 'user_steps.rb'
318 | end
319 | remove_file 'features/support/paths.rb'
320 | inside 'features/support' do
321 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/support/paths.rb', 'paths.rb'
322 | end
323 | end
324 | end
325 | end
326 |
327 |
328 | # >-----------------------------[ ActionMailer ]------------------------------<
329 |
330 | @current_recipe = "action_mailer"
331 | @before_configs["action_mailer"].call if @before_configs["action_mailer"]
332 | say_recipe 'ActionMailer'
333 |
334 |
335 | @configs[@current_recipe] = config
336 |
337 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
338 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/action_mailer.rb
339 |
340 | after_bundler do
341 | say_wizard "ActionMailer recipe running 'after bundler'"
342 | # modifying environment configuration files for ActionMailer
343 | gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '# ActionMailer Config'
344 | gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
345 | <<-RUBY
346 | config.action_mailer.default_url_options = { :host => 'localhost:3000' }
347 | # A dummy setup for development - no deliveries, but logged
348 | config.action_mailer.delivery_method = :smtp
349 | config.action_mailer.perform_deliveries = false
350 | config.action_mailer.raise_delivery_errors = true
351 | config.action_mailer.default :charset => "utf-8"
352 | RUBY
353 | end
354 | gsub_file 'config/environments/production.rb', /config.active_support.deprecation = :notify/ do
355 | <<-RUBY
356 | config.active_support.deprecation = :notify
357 |
358 | config.action_mailer.default_url_options = { :host => 'yourhost.com' }
359 | # ActionMailer Config
360 | # Setup for production - deliveries, no errors raised
361 | config.action_mailer.delivery_method = :smtp
362 | config.action_mailer.perform_deliveries = true
363 | config.action_mailer.raise_delivery_errors = false
364 | config.action_mailer.default :charset => "utf-8"
365 | RUBY
366 | end
367 |
368 | end
369 |
370 |
371 | # >--------------------------------[ Devise ]---------------------------------<
372 |
373 | @current_recipe = "devise"
374 | @before_configs["devise"].call if @before_configs["devise"]
375 | say_recipe 'Devise'
376 |
377 | config = {}
378 | config['devise'] = yes_wizard?("Would you like to use Devise for authentication?") if true && true unless config.key?('devise')
379 | @configs[@current_recipe] = config
380 |
381 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
382 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/devise.rb
383 |
384 | if config['devise']
385 | gem "devise", ">= 1.3.3"
386 | else
387 | recipes.delete('devise')
388 | end
389 |
390 |
391 | if config['devise']
392 | after_bundler do
393 |
394 | say_wizard "Devise recipe running 'after bundler'"
395 |
396 | # Run the Devise generator
397 | generate 'devise:install'
398 |
399 | if recipes.include? 'mongo_mapper'
400 | gem 'mm-devise'
401 | gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model'
402 | generate 'mongo_mapper:devise User'
403 | elsif recipes.include? 'mongoid'
404 | # Nothing to do (Devise changes its initializer automatically when Mongoid is detected)
405 | # gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
406 | end
407 |
408 | # Prevent logging of password_confirmation
409 | gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
410 |
411 | end
412 |
413 | after_everything do
414 |
415 | say_wizard "Devise recipe running 'after everything'"
416 |
417 | if recipes.include? 'rspec'
418 | say_wizard "Copying RSpec files from the rails3-devise-rspec-cucumber examples"
419 | # copy all the RSpec specs files from the rails3-devise-rspec-cucumber example app
420 | inside 'spec' do
421 | get 'https://github.com/fortuity/rails3-devise-rspec-cucumber/raw/master/spec/factories.rb', 'factories.rb'
422 | end
423 | remove_file 'spec/controllers/home_controller_spec.rb'
424 | remove_file 'spec/controllers/users_controller_spec.rb'
425 | inside 'spec/controllers' do
426 | get 'https://github.com/fortuity/rails3-devise-rspec-cucumber/raw/master/spec/controllers/home_controller_spec.rb', 'home_controller_spec.rb'
427 | get 'https://github.com/fortuity/rails3-devise-rspec-cucumber/raw/master/spec/controllers/users_controller_spec.rb', 'users_controller_spec.rb'
428 | end
429 | remove_file 'spec/models/user_spec.rb'
430 | inside 'spec/models' do
431 | get 'https://github.com/fortuity/rails3-devise-rspec-cucumber/raw/master/spec/models/user_spec.rb', 'user_spec.rb'
432 | end
433 | remove_file 'spec/views/home/index.html.erb_spec.rb'
434 | remove_file 'spec/views/home/index.html.haml_spec.rb'
435 | remove_file 'spec/views/users/show.html.erb_spec.rb'
436 | remove_file 'spec/views/users/show.html.haml_spec.rb'
437 | remove_file 'spec/helpers/home_helper_spec.rb'
438 | remove_file 'spec/helpers/users_helper_spec.rb'
439 | end
440 |
441 | end
442 | end
443 |
444 |
445 | # >--------------------------------[ AddUser ]--------------------------------<
446 |
447 | @current_recipe = "add_user"
448 | @before_configs["add_user"].call if @before_configs["add_user"]
449 | say_recipe 'AddUser'
450 |
451 |
452 | @configs[@current_recipe] = config
453 |
454 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
455 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/add_user.rb
456 |
457 | after_bundler do
458 |
459 | say_wizard "AddUser recipe running 'after bundler'"
460 |
461 | if recipes.include? 'omniauth'
462 | generate(:model, "user provider:string uid:string name:string email:string")
463 | gsub_file 'app/models/user.rb', /end/ do
464 | <<-RUBY
465 | attr_accessible :provider, :uid, :name, :email
466 | end
467 | RUBY
468 | end
469 | end
470 |
471 | if recipes.include? 'devise'
472 |
473 | # Generate models and routes for a User
474 | generate 'devise user'
475 |
476 | # Add a 'name' attribute to the User model
477 | if recipes.include? 'mongoid'
478 | gsub_file 'app/models/user.rb', /end/ do
479 | <<-RUBY
480 | field :name
481 | validates_presence_of :name
482 | validates_uniqueness_of :name, :email, :case_sensitive => false
483 | attr_accessible :name, :email, :password, :password_confirmation, :remember_me
484 | end
485 | RUBY
486 | end
487 | else
488 | # for ActiveRecord
489 | # Devise created a Users database, we'll modify it
490 | generate 'migration AddNameToUsers name:string'
491 | # Devise created a Users model, we'll modify it
492 | gsub_file 'app/models/user.rb', /attr_accessible :email/, 'attr_accessible :name, :email'
493 | inject_into_file 'app/models/user.rb', :before => 'validates_uniqueness_of' do
494 | "validates_presence_of :name\n"
495 | end
496 | gsub_file 'app/models/user.rb', /validates_uniqueness_of :email/, 'validates_uniqueness_of :name, :email'
497 | end
498 |
499 | unless recipes.include? 'haml'
500 |
501 | # Generate Devise views (unless you are using Haml)
502 | run 'rails generate devise:views'
503 |
504 | # Modify Devise views to add 'name'
505 | inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
506 | <<-ERB
507 |
<%= f.label :name %>
508 | <%= f.text_field :name %>
509 | ERB
510 | end
511 |
512 | inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
513 | <<-ERB
514 | <%= f.label :name %>
515 | <%= f.text_field :name %>
516 | ERB
517 | end
518 |
519 | else
520 |
521 | # copy Haml versions of modified Devise views
522 | inside 'app/views/devise/registrations' do
523 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/edit.html.haml', 'edit.html.haml'
524 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/new.html.haml', 'new.html.haml'
525 | end
526 |
527 | end
528 |
529 | end
530 |
531 | end
532 |
533 |
534 | # >-------------------------------[ HomePage ]--------------------------------<
535 |
536 | @current_recipe = "home_page"
537 | @before_configs["home_page"].call if @before_configs["home_page"]
538 | say_recipe 'HomePage'
539 |
540 |
541 | @configs[@current_recipe] = config
542 |
543 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
544 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page.rb
545 |
546 | after_bundler do
547 |
548 | say_wizard "HomePage recipe running 'after bundler'"
549 |
550 | # remove the default home page
551 | remove_file 'public/index.html'
552 |
553 | # create a home controller and view
554 | generate(:controller, "home index")
555 |
556 | # set up a simple home page (with placeholder content)
557 | if recipes.include? 'haml'
558 | remove_file 'app/views/home/index.html.haml'
559 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
560 | # We have to use single-quote-style-heredoc to avoid interpolation.
561 | create_file 'app/views/home/index.html.haml' do
562 | <<-'HAML'
563 | %h3 Home
564 | HAML
565 | end
566 | else
567 | remove_file 'app/views/home/index.html.erb'
568 | create_file 'app/views/home/index.html.erb' do
569 | <<-ERB
570 | Home
571 | ERB
572 | end
573 | end
574 |
575 | # set routes
576 | gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
577 |
578 | end
579 |
580 |
581 | # >-----------------------------[ HomePageUsers ]-----------------------------<
582 |
583 | @current_recipe = "home_page_users"
584 | @before_configs["home_page_users"].call if @before_configs["home_page_users"]
585 | say_recipe 'HomePageUsers'
586 |
587 |
588 | @configs[@current_recipe] = config
589 |
590 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
591 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page_users.rb
592 |
593 | after_bundler do
594 |
595 | say_wizard "HomePageUsers recipe running 'after bundler'"
596 |
597 | # Modify the home controller
598 | gsub_file 'app/controllers/home_controller.rb', /def index/ do
599 | <<-RUBY
600 | def index
601 | @users = User.all
602 | RUBY
603 | end
604 |
605 | # Replace the home page
606 | if recipes.include? 'haml'
607 | remove_file 'app/views/home/index.html.haml'
608 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
609 | # We have to use single-quote-style-heredoc to avoid interpolation.
610 | create_file 'app/views/home/index.html.haml' do
611 | <<-'HAML'
612 | %h3 Home
613 | - @users.each do |user|
614 | %p User: #{user.name}
615 | HAML
616 | end
617 | else
618 | append_file 'app/views/home/index.html.erb' do <<-ERB
619 | Home
620 | <% @users.each do |user| %>
621 | User: <%= user.name %>
622 | <% end %>
623 | ERB
624 | end
625 | end
626 |
627 | end
628 |
629 |
630 | # >-----------------------------[ SeedDatabase ]------------------------------<
631 |
632 | @current_recipe = "seed_database"
633 | @before_configs["seed_database"].call if @before_configs["seed_database"]
634 | say_recipe 'SeedDatabase'
635 |
636 |
637 | @configs[@current_recipe] = config
638 |
639 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
640 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/seed_database.rb
641 |
642 |
643 | after_bundler do
644 |
645 | say_wizard "SeedDatabase recipe running 'after bundler'"
646 |
647 | unless recipes.include? 'mongoid'
648 | run 'rake db:migrate'
649 | end
650 |
651 | if recipes.include? 'mongoid'
652 | append_file 'db/seeds.rb' do <<-FILE
653 | puts 'EMPTY THE MONGODB DATABASE'
654 | Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop)
655 | FILE
656 | end
657 | end
658 |
659 | if recipes.include? 'devise'
660 | # create a default user
661 | append_file 'db/seeds.rb' do <<-FILE
662 | puts 'SETTING UP DEFAULT USER LOGIN'
663 | user = User.create! :name => 'First User', :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
664 | puts 'New user created: ' << user.name
665 | FILE
666 | end
667 | end
668 |
669 | run 'rake db:seed'
670 |
671 | end
672 |
673 |
674 | # >-------------------------------[ UsersPage ]-------------------------------<
675 |
676 | @current_recipe = "users_page"
677 | @before_configs["users_page"].call if @before_configs["users_page"]
678 | say_recipe 'UsersPage'
679 |
680 |
681 | @configs[@current_recipe] = config
682 |
683 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
684 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/users_page.rb
685 |
686 | after_bundler do
687 |
688 | say_wizard "UsersPage recipe running 'after bundler'"
689 |
690 | #----------------------------------------------------------------------------
691 | # Create a users controller
692 | #----------------------------------------------------------------------------
693 | generate(:controller, "users show")
694 | gsub_file 'app/controllers/users_controller.rb', /def show/ do
695 | <<-RUBY
696 | before_filter :authenticate_user!
697 |
698 | def show
699 | @user = User.find(params[:id])
700 | RUBY
701 | end
702 |
703 | #----------------------------------------------------------------------------
704 | # Modify the routes
705 | #----------------------------------------------------------------------------
706 | # @devise_for :users@ route must be placed above @resources :users, :only => :show@.
707 | gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
708 | gsub_file 'config/routes.rb', /devise_for :users/ do
709 | <<-RUBY
710 | devise_for :users
711 | resources :users, :only => :show
712 | RUBY
713 | end
714 |
715 | #----------------------------------------------------------------------------
716 | # Create a users show page
717 | #----------------------------------------------------------------------------
718 | if recipes.include? 'haml'
719 | remove_file 'app/views/users/show.html.haml'
720 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
721 | # We have to use single-quote-style-heredoc to avoid interpolation.
722 | create_file 'app/views/users/show.html.haml' do <<-'HAML'
723 | %p
724 | User: #{@user.name}
725 | %p
726 | Email: #{@user.email if @user.email}
727 | HAML
728 | end
729 | else
730 | append_file 'app/views/users/show.html.erb' do <<-ERB
731 | User: <%= @user.name %>
732 | Email: <%= @user.email if @user.email %>
733 | ERB
734 | end
735 | end
736 |
737 | #----------------------------------------------------------------------------
738 | # Create a home page containing links to user show pages
739 | # (clobbers code from the home_page_users recipe)
740 | #----------------------------------------------------------------------------
741 | # set up the controller
742 | remove_file 'app/controllers/home_controller.rb'
743 | create_file 'app/controllers/home_controller.rb' do
744 | <<-RUBY
745 | class HomeController < ApplicationController
746 | def index
747 | @users = User.all
748 | end
749 | end
750 | RUBY
751 | end
752 |
753 | # modify the home page
754 | if recipes.include? 'haml'
755 | remove_file 'app/views/home/index.html.haml'
756 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
757 | # We have to use single-quote-style-heredoc to avoid interpolation.
758 | create_file 'app/views/home/index.html.haml' do
759 | <<-'HAML'
760 | %h3 Home
761 | - @users.each do |user|
762 | %p User: #{link_to user.name, user}
763 | HAML
764 | end
765 | else
766 | remove_file 'app/views/home/index.html.erb'
767 | create_file 'app/views/home/index.html.erb' do <<-ERB
768 | Home
769 | <% @users.each do |user| %>
770 | User: <%=link_to user.name, user %>
771 | <% end %>
772 | ERB
773 | end
774 | end
775 |
776 | end
777 |
778 |
779 | # >-------------------------------[ CssSetup ]--------------------------------<
780 |
781 | @current_recipe = "css_setup"
782 | @before_configs["css_setup"].call if @before_configs["css_setup"]
783 | say_recipe 'CssSetup'
784 |
785 |
786 | @configs[@current_recipe] = config
787 |
788 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
789 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/css_setup.rb
790 |
791 | after_bundler do
792 |
793 | say_wizard "CssSetup recipe running 'after bundler'"
794 |
795 | # Add a stylesheet with styles for a horizontal menu and flash messages
796 | create_file 'public/stylesheets/application.css' do <<-CSS
797 | ul.hmenu {
798 | list-style: none;
799 | margin: 0 0 2em;
800 | padding: 0;
801 | }
802 | ul.hmenu li {
803 | display: inline;
804 | }
805 | #flash_notice, #flash_alert {
806 | padding: 5px 8px;
807 | margin: 10px 0;
808 | }
809 | #flash_notice {
810 | background-color: #CFC;
811 | border: solid 1px #6C6;
812 | }
813 | #flash_alert {
814 | background-color: #FCC;
815 | border: solid 1px #C66;
816 | }
817 | CSS
818 | end
819 |
820 | end
821 |
822 |
823 | # >---------------------------[ ApplicationLayout ]---------------------------<
824 |
825 | @current_recipe = "application_layout"
826 | @before_configs["application_layout"].call if @before_configs["application_layout"]
827 | say_recipe 'ApplicationLayout'
828 |
829 |
830 | @configs[@current_recipe] = config
831 |
832 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
833 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/application_layout.rb
834 |
835 | after_bundler do
836 |
837 | say_wizard "ApplicationLayout recipe running 'after bundler'"
838 |
839 | # Set up the default application layout
840 | if recipes.include? 'haml'
841 | remove_file 'app/views/layouts/application.html.erb'
842 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
843 | create_file 'app/views/layouts/application.html.haml' do <<-HAML
844 | !!!
845 | %html
846 | %head
847 | %title #{app_name}
848 | = stylesheet_link_tag :all
849 | = javascript_include_tag :defaults
850 | = csrf_meta_tag
851 | %body
852 | - flash.each do |name, msg|
853 | = content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String)
854 | = yield
855 | HAML
856 | end
857 | else
858 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
859 | <<-ERB
860 | <%- flash.each do |name, msg| -%>
861 | <%= content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String) %>
862 | <%- end -%>
863 | ERB
864 | end
865 | end
866 |
867 | end
868 |
869 |
870 | # >---------------------------[ DeviseNavigation ]----------------------------<
871 |
872 | @current_recipe = "devise_navigation"
873 | @before_configs["devise_navigation"].call if @before_configs["devise_navigation"]
874 | say_recipe 'DeviseNavigation'
875 |
876 |
877 | @configs[@current_recipe] = config
878 |
879 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
880 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/devise_navigation.rb
881 |
882 | after_bundler do
883 |
884 | say_wizard "DeviseNavigation recipe running 'after bundler'"
885 |
886 | # Create navigation links for Devise
887 | if recipes.include? 'haml'
888 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
889 | # We have to use single-quote-style-heredoc to avoid interpolation.
890 | create_file "app/views/devise/menu/_login_items.html.haml" do <<-'HAML'
891 | - if user_signed_in?
892 | %li
893 | = link_to('Logout', destroy_user_session_path)
894 | - else
895 | %li
896 | = link_to('Login', new_user_session_path)
897 | HAML
898 | end
899 | else
900 | create_file "app/views/devise/menu/_login_items.html.erb" do <<-ERB
901 | <% if user_signed_in? %>
902 |
903 | <%= link_to('Logout', destroy_user_session_path) %>
904 |
905 | <% else %>
906 |
907 | <%= link_to('Login', new_user_session_path) %>
908 |
909 | <% end %>
910 | ERB
911 | end
912 | end
913 |
914 | if recipes.include? 'haml'
915 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
916 | # We have to use single-quote-style-heredoc to avoid interpolation.
917 | create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'HAML'
918 | - if user_signed_in?
919 | %li
920 | = link_to('Edit account', edit_user_registration_path)
921 | - else
922 | %li
923 | = link_to('Sign up', new_user_registration_path)
924 | HAML
925 | end
926 | else
927 | create_file "app/views/devise/menu/_registration_items.html.erb" do <<-ERB
928 | <% if user_signed_in? %>
929 |
930 | <%= link_to('Edit account', edit_user_registration_path) %>
931 |
932 | <% else %>
933 |
934 | <%= link_to('Sign up', new_user_registration_path) %>
935 |
936 | <% end %>
937 | ERB
938 | end
939 | end
940 |
941 | # Add navigation links to the default application layout
942 | if recipes.include? 'haml'
943 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
944 | inject_into_file 'app/views/layouts/application.html.haml', :after => "%body\n" do <<-HAML
945 | %ul.hmenu
946 | = render 'devise/menu/registration_items'
947 | = render 'devise/menu/login_items'
948 | HAML
949 | end
950 | else
951 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
952 | <<-ERB
953 |
957 | ERB
958 | end
959 | end
960 |
961 | end
962 |
963 |
964 | # >--------------------------------[ Cleanup ]--------------------------------<
965 |
966 | @current_recipe = "cleanup"
967 | @before_configs["cleanup"].call if @before_configs["cleanup"]
968 | say_recipe 'Cleanup'
969 |
970 |
971 | @configs[@current_recipe] = config
972 |
973 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
974 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cleanup.rb
975 |
976 | after_bundler do
977 |
978 | say_wizard "Cleanup recipe running 'after bundler'"
979 |
980 | # remove unnecessary files
981 | %w{
982 | README
983 | doc/README_FOR_APP
984 | public/index.html
985 | public/images/rails.png
986 | }.each { |file| remove_file file }
987 |
988 | # add placeholder READMEs
989 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.txt", "README"
990 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.textile", "README.textile"
991 | gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}"
992 | gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}"
993 |
994 | # remove commented lines from Gemfile
995 | # thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb
996 | gsub_file "Gemfile", /#.*\n/, "\n"
997 | gsub_file "Gemfile", /\n+/, "\n"
998 |
999 | end
1000 |
1001 |
1002 | # >------------------------------[ BanSpiders ]-------------------------------<
1003 |
1004 | @current_recipe = "ban_spiders"
1005 | @before_configs["ban_spiders"].call if @before_configs["ban_spiders"]
1006 | say_recipe 'BanSpiders'
1007 |
1008 | config = {}
1009 | config['ban_spiders'] = yes_wizard?("Would you like to set a robots.txt file to ban spiders?") if true && true unless config.key?('ban_spiders')
1010 | @configs[@current_recipe] = config
1011 |
1012 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1013 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/ban_spiders.rb
1014 |
1015 | if config['ban_spiders']
1016 | say_wizard "BanSpiders recipe running 'after bundler'"
1017 | after_bundler do
1018 | # ban spiders from your site by changing robots.txt
1019 | gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
1020 | gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
1021 | end
1022 | else
1023 | recipes.delete('ban_spiders')
1024 | end
1025 |
1026 |
1027 | # >----------------------------------[ Git ]----------------------------------<
1028 |
1029 | @current_recipe = "git"
1030 | @before_configs["git"].call if @before_configs["git"]
1031 | say_recipe 'Git'
1032 |
1033 |
1034 | @configs[@current_recipe] = config
1035 |
1036 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1037 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/git.rb
1038 |
1039 | after_everything do
1040 |
1041 | say_wizard "Git recipe running 'after everything'"
1042 |
1043 | # Git should ignore some files
1044 | remove_file '.gitignore'
1045 | get "https://github.com/fortuity/rails3-gitignore/raw/master/gitignore.txt", ".gitignore"
1046 |
1047 | if recipes.include? 'omniauth'
1048 | append_file '.gitignore' do <<-TXT
1049 | # keep OmniAuth service provider secrets out of the Git repo
1050 | config/initializers/omniauth.rb
1051 | TXT
1052 | end
1053 | end
1054 |
1055 | # Initialize new Git repo
1056 | git :init
1057 | git :add => '.'
1058 | git :commit => "-aqm 'new Rails app generated by rails3_devise_wizard'"
1059 | # Create a git branch
1060 | git :checkout => ' -b working_branch'
1061 | git :add => '.'
1062 | git :commit => "-m 'Initial commit of working_branch'"
1063 | end
1064 |
1065 |
1066 |
1067 |
1068 |
1069 | @current_recipe = nil
1070 |
1071 | # >-----------------------------[ Run Bundler ]-------------------------------<
1072 |
1073 | say_wizard "Running 'bundle install'. This will take a while."
1074 | run 'bundle install'
1075 | say_wizard "Running 'after bundler' callbacks."
1076 | @after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1077 |
1078 | @current_recipe = nil
1079 | say_wizard "Running 'after everything' callbacks."
1080 | @after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1081 |
1082 | @current_recipe = nil
1083 | say_wizard "Finished running the rails3_devise_wizard app template."
1084 | say_wizard "Your new Rails app is ready. Any problems?"
1085 | say_wizard "See http://github.com/fortuity/rails3-devise-rspec-cucumber/issues"
1086 |
--------------------------------------------------------------------------------
/rails3-mongoid-devise-template.rb:
--------------------------------------------------------------------------------
1 | # Application Generator Template
2 | # Modifies a Rails app to use Mongoid and Devise
3 | # Usage: rails new APP_NAME -m https://github.com/fortuity/rails3-application-templates/raw/master/rails3-mongoid-devise-template.rb -T -O -J
4 |
5 | # Information and a tutorial:
6 | # http://github.com/fortuity/rails3-mongoid-devise/
7 |
8 | # Generated using the rails3_devise_wizard gem:
9 | # https://github.com/fortuity/rails3_devise_wizard/
10 |
11 | # Based on application template recipes by:
12 | # Michael Bleigh https://github.com/mbleigh
13 | # Fletcher Nichol https://github.com/fnichol
14 | # Daniel Kehoe https://github.com/fortuity
15 | # Ramon Brooker https://github.com/cognition
16 |
17 | # If you are customizing this template, you can use any methods provided by Thor::Actions
18 | # http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
19 | # and Rails::Generators::Actions
20 | # http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
21 |
22 | # >---------------------------------------------------------------------------<
23 | #
24 | # _____ _ _ __ ___ _
25 | # | __ \ (_) | \ \ / (_) | |
26 | # | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
27 | # | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
28 | # | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
29 | # |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
30 | #
31 | # This template was generated by rails3_devise_wizard, a custom version of
32 | # RailsWizard, the application template builder. For more information, see:
33 | # https://github.com/fortuity/rails3_devise_wizard/
34 | #
35 | # >---------------------------------------------------------------------------<
36 |
37 | # >----------------------------[ Initial Setup ]------------------------------<
38 |
39 | initializer 'generators.rb', <<-RUBY
40 | Rails.application.config.generators do |g|
41 | end
42 | RUBY
43 |
44 | @recipes = ["jquery", "haml", "rspec", "cucumber", "mongoid", "action_mailer", "devise", "add_user_name", "home_page", "home_page_users", "seed_database", "users_page", "css_setup", "application_layout", "devise_navigation", "cleanup", "ban_spiders", "git"]
45 |
46 | def recipes; @recipes end
47 | def recipe?(name); @recipes.include?(name) end
48 |
49 | def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
50 | def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
51 | def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end
52 |
53 | def ask_wizard(question)
54 | ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
55 | end
56 |
57 | def yes_wizard?(question)
58 | answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
59 | case answer.downcase
60 | when "yes", "y"
61 | true
62 | when "no", "n"
63 | false
64 | else
65 | yes_wizard?(question)
66 | end
67 | end
68 |
69 | def no_wizard?(question); !yes_wizard?(question) end
70 |
71 | def multiple_choice(question, choices)
72 | say_custom('question', question)
73 | values = {}
74 | choices.each_with_index do |choice,i|
75 | values[(i + 1).to_s] = choice[1]
76 | say_custom (i + 1).to_s + ')', choice[0]
77 | end
78 | answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
79 | values[answer]
80 | end
81 |
82 | @current_recipe = nil
83 | @configs = {}
84 |
85 | @after_blocks = []
86 | def after_bundler(&block); @after_blocks << [@current_recipe, block]; end
87 | @after_everything_blocks = []
88 | def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end
89 | @before_configs = {}
90 | def before_config(&block); @before_configs[@current_recipe] = block; end
91 |
92 |
93 | say_wizard "Checking configuration. Please confirm your preferences."
94 |
95 | # >--------------------------------[ jQuery ]---------------------------------<
96 |
97 | @current_recipe = "jquery"
98 | @before_configs["jquery"].call if @before_configs["jquery"]
99 | say_recipe 'jQuery'
100 |
101 | config = {}
102 | config['jquery'] = yes_wizard?("Would you like to use jQuery instead of Prototype?") if true && true unless config.key?('jquery')
103 | config['ui'] = yes_wizard?("Would you like to use jQuery UI?") if true && true unless config.key?('ui')
104 | @configs[@current_recipe] = config
105 |
106 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
107 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/jquery.rb
108 |
109 | if config['jquery']
110 | say_wizard "REMINDER: When creating a Rails app using jQuery..."
111 | say_wizard "you should add the '-J' flag to 'rails new'"
112 | after_bundler do
113 | say_wizard "jQuery recipe running 'after bundler'"
114 | # remove the Prototype adapter file
115 | remove_file 'public/javascripts/rails.js'
116 | # remove the Prototype files (if they exist)
117 | remove_file 'public/javascripts/controls.js'
118 | remove_file 'public/javascripts/dragdrop.js'
119 | remove_file 'public/javascripts/effects.js'
120 | remove_file 'public/javascripts/prototype.js'
121 | # add jQuery files
122 | inside "public/javascripts" do
123 | get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "rails.js"
124 | get "https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js", "jquery.js"
125 | if config['ui']
126 | get "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js", "jqueryui.js"
127 | end
128 | end
129 | # adjust the Javascript defaults
130 | if config['ui']
131 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery jqueryui rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
132 | else
133 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
134 | end
135 | gsub_file "config/application.rb", /config.action_view.javascript_expansions\[:defaults\] = \%w\(\)\n/, ""
136 | end
137 | else
138 | recipes.delete('jquery')
139 | end
140 |
141 |
142 | # >---------------------------------[ HAML ]----------------------------------<
143 |
144 | @current_recipe = "haml"
145 | @before_configs["haml"].call if @before_configs["haml"]
146 | say_recipe 'HAML'
147 |
148 | config = {}
149 | config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml')
150 | @configs[@current_recipe] = config
151 |
152 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
153 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/haml.rb
154 |
155 | if config['haml']
156 | gem 'haml', '>= 3.0.25'
157 | gem 'haml-rails', '>= 0.3.4', :group => :development
158 | else
159 | recipes.delete('haml')
160 | end
161 |
162 |
163 | # >---------------------------------[ RSpec ]---------------------------------<
164 |
165 | @current_recipe = "rspec"
166 | @before_configs["rspec"].call if @before_configs["rspec"]
167 | say_recipe 'RSpec'
168 |
169 | config = {}
170 | config['rspec'] = yes_wizard?("Would you like to use RSpec instead of TestUnit?") if true && true unless config.key?('rspec')
171 | config['factory_girl'] = yes_wizard?("Would you like to use factory_girl for test fixtures with RSpec?") if true && true unless config.key?('factory_girl')
172 | @configs[@current_recipe] = config
173 |
174 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
175 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/rspec.rb
176 |
177 | if config['rspec']
178 | say_wizard "REMINDER: When creating a Rails app using RSpec..."
179 | say_wizard "you should add the '-T' flag to 'rails new'"
180 | gem 'rspec-rails', '>= 2.5.0', :group => [:development, :test]
181 | if recipes.include? 'mongoid'
182 | # use the database_cleaner gem to reset the test database
183 | gem 'database_cleaner', '>= 0.6.7', :group => :test
184 | # include RSpec matchers from the mongoid-rspec gem
185 | gem 'mongoid-rspec', ">= 1.4.1", :group => :test
186 | end
187 | if config['factory_girl']
188 | # use the factory_girl gem for test fixtures
189 | gem 'factory_girl_rails', ">= 1.1.beta1", :group => :test
190 | end
191 | else
192 | recipes.delete('rspec')
193 | end
194 |
195 | # note: there is no need to specify the RSpec generator in the config/application.rb file
196 |
197 | if config['rspec']
198 | after_bundler do
199 | say_wizard "RSpec recipe running 'after bundler'"
200 | generate 'rspec:install'
201 |
202 | say_wizard "Removing test folder (not needed for RSpec)"
203 | run 'rm -rf test/'
204 |
205 | if recipes.include? 'mongoid'
206 |
207 | # remove ActiveRecord artifacts
208 | gsub_file 'spec/spec_helper.rb', /config.fixture_path/, '# config.fixture_path'
209 | gsub_file 'spec/spec_helper.rb', /config.use_transactional_fixtures/, '# config.use_transactional_fixtures'
210 |
211 | # reset your application database to a pristine state during testing
212 | inject_into_file 'spec/spec_helper.rb', :before => "\nend" do
213 | <<-RUBY
214 | \n
215 | # Clean up the database
216 | require 'database_cleaner'
217 | config.before(:suite) do
218 | DatabaseCleaner.strategy = :truncation
219 | DatabaseCleaner.orm = "mongoid"
220 | end
221 |
222 | config.before(:each) do
223 | DatabaseCleaner.clean
224 | end
225 | RUBY
226 | end
227 |
228 | # remove either possible occurrence of "require rails/test_unit/railtie"
229 | gsub_file 'config/application.rb', /require 'rails\/test_unit\/railtie'/, '# require "rails/test_unit/railtie"'
230 | gsub_file 'config/application.rb', /require "rails\/test_unit\/railtie"/, '# require "rails/test_unit/railtie"'
231 |
232 | # configure RSpec to use matchers from the mongoid-rspec gem
233 | create_file 'spec/support/mongoid.rb' do
234 | <<-RUBY
235 | RSpec.configure do |config|
236 | config.include Mongoid::Matchers
237 | end
238 | RUBY
239 | end
240 | end
241 |
242 | if recipes.include? 'devise'
243 | # add Devise test helpers
244 | create_file 'spec/support/devise.rb' do
245 | <<-RUBY
246 | RSpec.configure do |config|
247 | config.include Devise::TestHelpers, :type => :controller
248 | end
249 | RUBY
250 | end
251 | end
252 |
253 | end
254 | end
255 |
256 |
257 | # >-------------------------------[ Cucumber ]--------------------------------<
258 |
259 | @current_recipe = "cucumber"
260 | @before_configs["cucumber"].call if @before_configs["cucumber"]
261 | say_recipe 'Cucumber'
262 |
263 | config = {}
264 | config['cucumber'] = yes_wizard?("Would you like to use Cucumber for your BDD?") if true && true unless config.key?('cucumber')
265 | @configs[@current_recipe] = config
266 |
267 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
268 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cucumber.rb
269 |
270 | if config['cucumber']
271 | gem 'cucumber-rails', ">= 0.4.1", :group => :test
272 | gem 'capybara', ">= 0.4.1.2", :group => :test
273 | gem 'database_cleaner', '>= 0.6.7', :group => :test
274 | gem 'launchy', ">= 0.4.0", :group => :test
275 | else
276 | recipes.delete('cucumber')
277 | end
278 |
279 | if config['cucumber']
280 | after_bundler do
281 | say_wizard "Cucumber recipe running 'after bundler'"
282 | generate "cucumber:install --capybara#{' --rspec' if recipes.include?('rspec')}#{' -D' if recipes.include?('mongoid')}"
283 | if recipes.include? 'mongoid'
284 | gsub_file 'features/support/env.rb', /transaction/, "truncation"
285 | inject_into_file 'features/support/env.rb', :after => 'begin' do
286 | "\n DatabaseCleaner.orm = 'mongoid'"
287 | end
288 | end
289 | end
290 | end
291 |
292 | if config['cucumber']
293 | if recipes.include? 'devise'
294 | after_bundler do
295 | say_wizard "Copying Cucumber scenarios from the rails3-mongoid-devise examples"
296 | # copy all the Cucumber scenario files from the rails3-mongoid-devise example app
297 | inside 'features/users' do
298 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_in.feature', 'sign_in.feature'
299 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_out.feature', 'sign_out.feature'
300 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_up.feature', 'sign_up.feature'
301 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_edit.feature', 'user_edit.feature'
302 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_show.feature', 'user_show.feature'
303 | end
304 | inside 'features/step_definitions' do
305 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/step_definitions/user_steps.rb', 'user_steps.rb'
306 | end
307 | remove_file 'features/support/paths.rb'
308 | inside 'features/support' do
309 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/support/paths.rb', 'paths.rb'
310 | end
311 | end
312 | end
313 | end
314 |
315 |
316 | # >--------------------------------[ Mongoid ]--------------------------------<
317 |
318 | @current_recipe = "mongoid"
319 | @before_configs["mongoid"].call if @before_configs["mongoid"]
320 | say_recipe 'Mongoid'
321 |
322 | config = {}
323 | config['mongoid'] = yes_wizard?("Would you like to use Mongoid to connect to a MongoDB database?") if true && true unless config.key?('mongoid')
324 | @configs[@current_recipe] = config
325 |
326 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
327 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/mongoid.rb
328 |
329 | if config['mongoid']
330 | say_wizard "REMINDER: When creating a Rails app using Mongoid..."
331 | say_wizard "you should add the '-O' flag to 'rails new'"
332 | gem 'bson_ext', '>= 1.3.0'
333 | gem 'mongoid', '>= 2.0.1'
334 | else
335 | recipes.delete('mongoid')
336 | end
337 |
338 | if config['mongoid']
339 | after_bundler do
340 | say_wizard "Mongoid recipe running 'after bundler'"
341 | # note: the mongoid generator automatically modifies the config/application.rb file
342 | # to remove the ActiveRecord dependency by commenting out "require active_record/railtie'"
343 | generate 'mongoid:config'
344 | # remove the unnecessary 'config/database.yml' file
345 | remove_file 'config/database.yml'
346 | end
347 | end
348 |
349 |
350 | # >-----------------------------[ ActionMailer ]------------------------------<
351 |
352 | @current_recipe = "action_mailer"
353 | @before_configs["action_mailer"].call if @before_configs["action_mailer"]
354 | say_recipe 'ActionMailer'
355 |
356 |
357 | @configs[@current_recipe] = config
358 |
359 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
360 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/action_mailer.rb
361 |
362 | after_bundler do
363 | say_wizard "ActionMailer recipe running 'after bundler'"
364 | # modifying environment configuration files for ActionMailer
365 | gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '# ActionMailer Config'
366 | gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
367 | <<-RUBY
368 | config.action_mailer.default_url_options = { :host => 'localhost:3000' }
369 | # A dummy setup for development - no deliveries, but logged
370 | config.action_mailer.delivery_method = :smtp
371 | config.action_mailer.perform_deliveries = false
372 | config.action_mailer.raise_delivery_errors = true
373 | config.action_mailer.default :charset => "utf-8"
374 | RUBY
375 | end
376 | gsub_file 'config/environments/production.rb', /config.active_support.deprecation = :notify/ do
377 | <<-RUBY
378 | config.active_support.deprecation = :notify
379 |
380 | config.action_mailer.default_url_options = { :host => 'yourhost.com' }
381 | # ActionMailer Config
382 | # Setup for production - deliveries, no errors raised
383 | config.action_mailer.delivery_method = :smtp
384 | config.action_mailer.perform_deliveries = true
385 | config.action_mailer.raise_delivery_errors = false
386 | config.action_mailer.default :charset => "utf-8"
387 | RUBY
388 | end
389 |
390 | end
391 |
392 |
393 | # >--------------------------------[ Devise ]---------------------------------<
394 |
395 | @current_recipe = "devise"
396 | @before_configs["devise"].call if @before_configs["devise"]
397 | say_recipe 'Devise'
398 |
399 | config = {}
400 | config['devise'] = yes_wizard?("Would you like to use Devise for authentication?") if true && true unless config.key?('devise')
401 | @configs[@current_recipe] = config
402 |
403 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
404 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/devise.rb
405 |
406 | if config['devise']
407 | gem "devise", ">= 1.3.3"
408 | else
409 | recipes.delete('devise')
410 | end
411 |
412 |
413 | if config['devise']
414 | after_bundler do
415 |
416 | say_wizard "Devise recipe running 'after bundler'"
417 |
418 | # Run the Devise generator
419 | generate 'devise:install'
420 |
421 | if recipes.include? 'mongo_mapper'
422 | gem 'mm-devise'
423 | gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model'
424 | generate 'mongo_mapper:devise User'
425 | elsif recipes.include? 'mongoid'
426 | # Nothing to do (Devise changes its initializer automatically when Mongoid is detected)
427 | # gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
428 | end
429 |
430 | # Prevent logging of password_confirmation
431 | gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
432 |
433 | # Generate models and routes for a User
434 | generate 'devise user'
435 |
436 | end
437 |
438 | after_everything do
439 |
440 | say_wizard "Devise recipe running 'after everything'"
441 |
442 | if recipes.include? 'rspec'
443 | say_wizard "Copying RSpec files from the rails3-mongoid-devise examples"
444 | # copy all the RSpec specs files from the rails3-mongoid-devise example app
445 | inside 'spec' do
446 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/spec/factories.rb', 'factories.rb'
447 | end
448 | remove_file 'spec/controllers/home_controller_spec.rb'
449 | remove_file 'spec/controllers/users_controller_spec.rb'
450 | inside 'spec/controllers' do
451 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/spec/controllers/home_controller_spec.rb', 'home_controller_spec.rb'
452 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/spec/controllers/users_controller_spec.rb', 'users_controller_spec.rb'
453 | end
454 | remove_file 'spec/models/user_spec.rb'
455 | inside 'spec/models' do
456 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/spec/models/user_spec.rb', 'user_spec.rb'
457 | end
458 | remove_file 'spec/views/home/index.html.erb_spec.rb'
459 | remove_file 'spec/views/home/index.html.haml_spec.rb'
460 | remove_file 'spec/views/users/show.html.erb_spec.rb'
461 | remove_file 'spec/views/users/show.html.haml_spec.rb'
462 | remove_file 'spec/helpers/home_helper_spec.rb'
463 | remove_file 'spec/helpers/users_helper_spec.rb'
464 | end
465 |
466 | end
467 | end
468 |
469 |
470 | # >------------------------------[ AddUserName ]------------------------------<
471 |
472 | @current_recipe = "add_user_name"
473 | @before_configs["add_user_name"].call if @before_configs["add_user_name"]
474 | say_recipe 'AddUserName'
475 |
476 |
477 | @configs[@current_recipe] = config
478 |
479 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
480 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/add_user_name.rb
481 |
482 | after_bundler do
483 |
484 | say_wizard "AddUserName recipe running 'after bundler'"
485 |
486 | # Add a 'name' attribute to the User model
487 | if recipes.include? 'mongoid'
488 | gsub_file 'app/models/user.rb', /end/ do
489 | <<-RUBY
490 | field :name
491 | validates_presence_of :name
492 | validates_uniqueness_of :name, :email, :case_sensitive => false
493 | attr_accessible :name, :email, :password, :password_confirmation, :remember_me
494 | end
495 | RUBY
496 | end
497 | else
498 | # for ActiveRecord
499 | # Devise created a Users database, we'll modify it
500 | generate 'migration AddNameToUsers name:string'
501 | # Devise created a Users model, we'll modify it
502 | gsub_file 'app/models/user.rb', /attr_accessible :email/, 'attr_accessible :name, :email'
503 | inject_into_file 'app/models/user.rb', :before => 'validates_uniqueness_of' do
504 | "validates_presence_of :name\n"
505 | end
506 | gsub_file 'app/models/user.rb', /validates_uniqueness_of :email/, 'validates_uniqueness_of :name, :email'
507 | end
508 |
509 | if recipes.include? 'devise'
510 | unless recipes.include? 'haml'
511 |
512 | # Generate Devise views (unless you are using Haml)
513 | run 'rails generate devise:views'
514 |
515 | # Modify Devise views to add 'name'
516 | inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
517 | <<-ERB
518 | <%= f.label :name %>
519 | <%= f.text_field :name %>
520 | ERB
521 | end
522 |
523 | inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
524 | <<-ERB
525 | <%= f.label :name %>
526 | <%= f.text_field :name %>
527 | ERB
528 | end
529 |
530 | else
531 |
532 | # copy Haml versions of modified Devise views
533 | inside 'app/views/devise/registrations' do
534 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/edit.html.haml', 'edit.html.haml'
535 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/new.html.haml', 'new.html.haml'
536 | end
537 |
538 | end
539 |
540 | end
541 |
542 | end
543 |
544 |
545 | # >-------------------------------[ HomePage ]--------------------------------<
546 |
547 | @current_recipe = "home_page"
548 | @before_configs["home_page"].call if @before_configs["home_page"]
549 | say_recipe 'HomePage'
550 |
551 |
552 | @configs[@current_recipe] = config
553 |
554 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
555 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page.rb
556 |
557 | after_bundler do
558 |
559 | say_wizard "HomePage recipe running 'after bundler'"
560 |
561 | # remove the default home page
562 | remove_file 'public/index.html'
563 |
564 | # create a home controller and view
565 | generate(:controller, "home index")
566 |
567 | # set up a simple home page (with placeholder content)
568 | if recipes.include? 'haml'
569 | remove_file 'app/views/home/index.html.haml'
570 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
571 | # We have to use single-quote-style-heredoc to avoid interpolation.
572 | create_file 'app/views/home/index.html.haml' do
573 | <<-'HAML'
574 | %h3 Home
575 | HAML
576 | end
577 | else
578 | remove_file 'app/views/home/index.html.erb'
579 | create_file 'app/views/home/index.html.erb' do
580 | <<-ERB
581 | Home
582 | ERB
583 | end
584 | end
585 |
586 | # set routes
587 | gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
588 |
589 | end
590 |
591 |
592 | # >-----------------------------[ HomePageUsers ]-----------------------------<
593 |
594 | @current_recipe = "home_page_users"
595 | @before_configs["home_page_users"].call if @before_configs["home_page_users"]
596 | say_recipe 'HomePageUsers'
597 |
598 |
599 | @configs[@current_recipe] = config
600 |
601 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
602 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page_users.rb
603 |
604 | after_bundler do
605 |
606 | say_wizard "HomePageUsers recipe running 'after bundler'"
607 |
608 | # Modify the home controller
609 | gsub_file 'app/controllers/home_controller.rb', /def index/ do
610 | <<-RUBY
611 | def index
612 | @users = User.all
613 | RUBY
614 | end
615 |
616 | # Replace the home page
617 | if recipes.include? 'haml'
618 | remove_file 'app/views/home/index.html.haml'
619 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
620 | # We have to use single-quote-style-heredoc to avoid interpolation.
621 | create_file 'app/views/home/index.html.haml' do
622 | <<-'HAML'
623 | %h3 Home
624 | - @users.each do |user|
625 | %p User: #{user.name}
626 | HAML
627 | end
628 | else
629 | append_file 'app/views/home/index.html.erb' do <<-ERB
630 | Home
631 | <% @users.each do |user| %>
632 | User: <%= user.name %>
633 | <% end %>
634 | ERB
635 | end
636 | end
637 |
638 | end
639 |
640 |
641 | # >-----------------------------[ SeedDatabase ]------------------------------<
642 |
643 | @current_recipe = "seed_database"
644 | @before_configs["seed_database"].call if @before_configs["seed_database"]
645 | say_recipe 'SeedDatabase'
646 |
647 |
648 | @configs[@current_recipe] = config
649 |
650 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
651 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/seed_database.rb
652 |
653 |
654 | after_bundler do
655 |
656 | say_wizard "SeedDatabase recipe running 'after bundler'"
657 |
658 | if recipes.include? 'mongoid'
659 | # create a default user
660 | append_file 'db/seeds.rb' do <<-FILE
661 | puts 'EMPTY THE MONGODB DATABASE'
662 | Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop)
663 | FILE
664 | end
665 | end
666 |
667 | # create a default user
668 | append_file 'db/seeds.rb' do <<-FILE
669 | puts 'SETTING UP DEFAULT USER LOGIN'
670 | user = User.create! :name => 'First User', :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
671 | puts 'New user created: ' << user.name
672 | FILE
673 | end
674 |
675 | run 'rake db:seed'
676 |
677 | end
678 |
679 |
680 | # >-------------------------------[ UsersPage ]-------------------------------<
681 |
682 | @current_recipe = "users_page"
683 | @before_configs["users_page"].call if @before_configs["users_page"]
684 | say_recipe 'UsersPage'
685 |
686 |
687 | @configs[@current_recipe] = config
688 |
689 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
690 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/users_page.rb
691 |
692 | after_bundler do
693 |
694 | say_wizard "UsersPage recipe running 'after bundler'"
695 |
696 | #----------------------------------------------------------------------------
697 | # Create a users controller
698 | #----------------------------------------------------------------------------
699 | generate(:controller, "users show")
700 | gsub_file 'app/controllers/users_controller.rb', /def show/ do
701 | <<-RUBY
702 | before_filter :authenticate_user!
703 |
704 | def show
705 | @user = User.find(params[:id])
706 | RUBY
707 | end
708 |
709 | #----------------------------------------------------------------------------
710 | # Modify the routes
711 | #----------------------------------------------------------------------------
712 | # @devise_for :users@ route must be placed above @resources :users, :only => :show@.
713 | gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
714 | gsub_file 'config/routes.rb', /devise_for :users/ do
715 | <<-RUBY
716 | devise_for :users
717 | resources :users, :only => :show
718 | RUBY
719 | end
720 |
721 | #----------------------------------------------------------------------------
722 | # Create a users show page
723 | #----------------------------------------------------------------------------
724 | if recipes.include? 'haml'
725 | remove_file 'app/views/users/show.html.haml'
726 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
727 | # We have to use single-quote-style-heredoc to avoid interpolation.
728 | create_file 'app/views/users/show.html.haml' do <<-'HAML'
729 | %p
730 | User: #{@user.name}
731 | HAML
732 | end
733 | else
734 | append_file 'app/views/users/show.html.erb' do <<-ERB
735 | User: <%= @user.name %>
736 | ERB
737 | end
738 | end
739 |
740 | #----------------------------------------------------------------------------
741 | # Create a home page containing links to user show pages
742 | # (clobbers code from the home_page_users recipe)
743 | #----------------------------------------------------------------------------
744 | # set up the controller
745 | remove_file 'app/controllers/home_controller.rb'
746 | create_file 'app/controllers/home_controller.rb' do
747 | <<-RUBY
748 | class HomeController < ApplicationController
749 | def index
750 | @users = User.all
751 | end
752 | end
753 | RUBY
754 | end
755 |
756 | # modify the home page
757 | if recipes.include? 'haml'
758 | remove_file 'app/views/home/index.html.haml'
759 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
760 | # We have to use single-quote-style-heredoc to avoid interpolation.
761 | create_file 'app/views/home/index.html.haml' do
762 | <<-'HAML'
763 | %h3 Home
764 | - @users.each do |user|
765 | %p User: #{link_to user.name, user}
766 | HAML
767 | end
768 | else
769 | remove_file 'app/views/home/index.html.erb'
770 | create_file 'app/views/home/index.html.erb' do <<-ERB
771 | Home
772 | <% @users.each do |user| %>
773 | User: <%=link_to user.name, user %>
774 | <% end %>
775 | ERB
776 | end
777 | end
778 |
779 | end
780 |
781 |
782 | # >-------------------------------[ CssSetup ]--------------------------------<
783 |
784 | @current_recipe = "css_setup"
785 | @before_configs["css_setup"].call if @before_configs["css_setup"]
786 | say_recipe 'CssSetup'
787 |
788 |
789 | @configs[@current_recipe] = config
790 |
791 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
792 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/css_setup.rb
793 |
794 | after_bundler do
795 |
796 | say_wizard "CssSetup recipe running 'after bundler'"
797 |
798 | # Add a stylesheet with styles for a horizontal menu and flash messages
799 | create_file 'public/stylesheets/application.css' do <<-CSS
800 | ul.hmenu {
801 | list-style: none;
802 | margin: 0 0 2em;
803 | padding: 0;
804 | }
805 | ul.hmenu li {
806 | display: inline;
807 | }
808 | #flash_notice, #flash_alert {
809 | padding: 5px 8px;
810 | margin: 10px 0;
811 | }
812 | #flash_notice {
813 | background-color: #CFC;
814 | border: solid 1px #6C6;
815 | }
816 | #flash_alert {
817 | background-color: #FCC;
818 | border: solid 1px #C66;
819 | }
820 | CSS
821 | end
822 |
823 | end
824 |
825 |
826 | # >---------------------------[ ApplicationLayout ]---------------------------<
827 |
828 | @current_recipe = "application_layout"
829 | @before_configs["application_layout"].call if @before_configs["application_layout"]
830 | say_recipe 'ApplicationLayout'
831 |
832 |
833 | @configs[@current_recipe] = config
834 |
835 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
836 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/application_layout.rb
837 |
838 | after_bundler do
839 |
840 | say_wizard "ApplicationLayout recipe running 'after bundler'"
841 |
842 | # Set up the default application layout
843 | if recipes.include? 'haml'
844 | remove_file 'app/views/layouts/application.html.erb'
845 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
846 | create_file 'app/views/layouts/application.html.haml' do <<-HAML
847 | !!!
848 | %html
849 | %head
850 | %title #{app_name}
851 | = stylesheet_link_tag :all
852 | = javascript_include_tag :defaults
853 | = csrf_meta_tag
854 | %body
855 | - flash.each do |name, msg|
856 | = content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String)
857 | = yield
858 | HAML
859 | end
860 | else
861 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
862 | <<-ERB
863 | <%- flash.each do |name, msg| -%>
864 | <%= content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String) %>
865 | <%- end -%>
866 | ERB
867 | end
868 | end
869 |
870 | end
871 |
872 |
873 | # >---------------------------[ DeviseNavigation ]----------------------------<
874 |
875 | @current_recipe = "devise_navigation"
876 | @before_configs["devise_navigation"].call if @before_configs["devise_navigation"]
877 | say_recipe 'DeviseNavigation'
878 |
879 |
880 | @configs[@current_recipe] = config
881 |
882 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
883 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/devise_navigation.rb
884 |
885 | after_bundler do
886 |
887 | say_wizard "DeviseNavigation recipe running 'after bundler'"
888 |
889 | # Create navigation links for Devise
890 | if recipes.include? 'haml'
891 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
892 | # We have to use single-quote-style-heredoc to avoid interpolation.
893 | create_file "app/views/devise/menu/_login_items.html.haml" do <<-'HAML'
894 | - if user_signed_in?
895 | %li
896 | = link_to('Logout', destroy_user_session_path)
897 | - else
898 | %li
899 | = link_to('Login', new_user_session_path)
900 | HAML
901 | end
902 | else
903 | create_file "app/views/devise/menu/_login_items.html.erb" do <<-ERB
904 | <% if user_signed_in? %>
905 |
906 | <%= link_to('Logout', destroy_user_session_path) %>
907 |
908 | <% else %>
909 |
910 | <%= link_to('Login', new_user_session_path) %>
911 |
912 | <% end %>
913 | ERB
914 | end
915 | end
916 |
917 | if recipes.include? 'haml'
918 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
919 | # We have to use single-quote-style-heredoc to avoid interpolation.
920 | create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'HAML'
921 | - if user_signed_in?
922 | %li
923 | = link_to('Edit account', edit_user_registration_path)
924 | - else
925 | %li
926 | = link_to('Sign up', new_user_registration_path)
927 | HAML
928 | end
929 | else
930 | create_file "app/views/devise/menu/_registration_items.html.erb" do <<-ERB
931 | <% if user_signed_in? %>
932 |
933 | <%= link_to('Edit account', edit_user_registration_path) %>
934 |
935 | <% else %>
936 |
937 | <%= link_to('Sign up', new_user_registration_path) %>
938 |
939 | <% end %>
940 | ERB
941 | end
942 | end
943 |
944 | # Add navigation links to the default application layout
945 | if recipes.include? 'haml'
946 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
947 | inject_into_file 'app/views/layouts/application.html.haml', :after => "%body\n" do <<-HAML
948 | %ul.hmenu
949 | = render 'devise/menu/registration_items'
950 | = render 'devise/menu/login_items'
951 | HAML
952 | end
953 | else
954 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
955 | <<-ERB
956 |
960 | ERB
961 | end
962 | end
963 |
964 | end
965 |
966 |
967 | # >--------------------------------[ Cleanup ]--------------------------------<
968 |
969 | @current_recipe = "cleanup"
970 | @before_configs["cleanup"].call if @before_configs["cleanup"]
971 | say_recipe 'Cleanup'
972 |
973 |
974 | @configs[@current_recipe] = config
975 |
976 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
977 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cleanup.rb
978 |
979 | after_bundler do
980 |
981 | say_wizard "Cleanup recipe running 'after bundler'"
982 |
983 | # remove unnecessary files
984 | %w{
985 | README
986 | doc/README_FOR_APP
987 | public/index.html
988 | public/images/rails.png
989 | }.each { |file| remove_file file }
990 |
991 | # add placeholder READMEs
992 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.txt", "README"
993 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.textile", "README.textile"
994 | gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}"
995 | gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}"
996 |
997 | # remove commented lines from Gemfile
998 | # thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb
999 | gsub_file "Gemfile", /#.*\n/, "\n"
1000 | gsub_file "Gemfile", /\n+/, "\n"
1001 |
1002 | end
1003 |
1004 |
1005 | # >------------------------------[ BanSpiders ]-------------------------------<
1006 |
1007 | @current_recipe = "ban_spiders"
1008 | @before_configs["ban_spiders"].call if @before_configs["ban_spiders"]
1009 | say_recipe 'BanSpiders'
1010 |
1011 | config = {}
1012 | config['ban_spiders'] = yes_wizard?("Would you like to set a robots.txt file to ban spiders?") if true && true unless config.key?('ban_spiders')
1013 | @configs[@current_recipe] = config
1014 |
1015 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1016 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/ban_spiders.rb
1017 |
1018 | if config['ban_spiders']
1019 | say_wizard "BanSpiders recipe running 'after bundler'"
1020 | after_bundler do
1021 | # ban spiders from your site by changing robots.txt
1022 | gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
1023 | gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
1024 | end
1025 | else
1026 | recipes.delete('ban_spiders')
1027 | end
1028 |
1029 |
1030 | # >----------------------------------[ Git ]----------------------------------<
1031 |
1032 | @current_recipe = "git"
1033 | @before_configs["git"].call if @before_configs["git"]
1034 | say_recipe 'Git'
1035 |
1036 |
1037 | @configs[@current_recipe] = config
1038 |
1039 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1040 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/git.rb
1041 |
1042 | after_everything do
1043 | say_wizard "Git recipe running 'after everything'"
1044 | # Git should ignore some files
1045 | remove_file '.gitignore'
1046 | get "https://github.com/fortuity/rails3-gitignore/raw/master/gitignore.txt", ".gitignore"
1047 | # Initialize new Git repo
1048 | git :init
1049 | git :add => '.'
1050 | git :commit => "-aqm 'Initial commit of a new Rails app'"
1051 | # Create a git branch
1052 | git :checkout => ' -b working_branch'
1053 | git :add => '.'
1054 | git :commit => "-m 'Initial commit of working_branch'"
1055 | end
1056 |
1057 |
1058 |
1059 |
1060 |
1061 | @current_recipe = nil
1062 |
1063 | # >-----------------------------[ Run Bundler ]-------------------------------<
1064 |
1065 | say_wizard "Running 'bundle install'. This will take a while."
1066 | run 'bundle install'
1067 | say_wizard "Running 'after bundler' callbacks."
1068 | @after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1069 |
1070 | @current_recipe = nil
1071 | say_wizard "Running 'after everything' callbacks."
1072 | @after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1073 |
1074 | @current_recipe = nil
1075 | say_wizard "Finished running the rails3_devise_wizard app template."
1076 | say_wizard "Your new Rails app is ready. Any problems?"
1077 | say_wizard "See http://github.com/fortuity/rails3-mongoid-devise/issues"
1078 |
--------------------------------------------------------------------------------
/rails3-mongoid-omniauth-template.rb:
--------------------------------------------------------------------------------
1 | # Application Generator Template
2 | # Modifies a Rails app to use Mongoid and OmniAuth
3 | # Usage: rails new APP_NAME -m https://github.com/fortuity/rails3-application-templates/raw/master/rails3-mongoid-omniauth-template.rb -T -O -J
4 |
5 | # Information and a tutorial:
6 | # http://github.com/fortuity/rails3-mongoid-omniauth/
7 |
8 | # Generated using the rails3_devise_wizard gem:
9 | # https://github.com/fortuity/rails3_devise_wizard/
10 |
11 | # Based on application template recipes by:
12 | # Michael Bleigh https://github.com/mbleigh
13 | # Fletcher Nichol https://github.com/fnichol
14 | # Daniel Kehoe https://github.com/fortuity
15 | # Ramon Brooker https://github.com/cognition
16 |
17 | # If you are customizing this template, you can use any methods provided by Thor::Actions
18 | # http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
19 | # and Rails::Generators::Actions
20 | # http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
21 |
22 | # >---------------------------------------------------------------------------<
23 | #
24 | # _____ _ _ __ ___ _
25 | # | __ \ (_) | \ \ / (_) | |
26 | # | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
27 | # | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
28 | # | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
29 | # |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
30 | #
31 | # This template was generated by rails3_devise_wizard, a custom version of
32 | # RailsWizard, the application template builder. For more information, see:
33 | # https://github.com/fortuity/rails3_devise_wizard/
34 | #
35 | # >---------------------------------------------------------------------------<
36 |
37 | # >----------------------------[ Initial Setup ]------------------------------<
38 |
39 | initializer 'generators.rb', <<-RUBY
40 | Rails.application.config.generators do |g|
41 | end
42 | RUBY
43 |
44 | @recipes = ["jquery", "haml", "rspec", "cucumber", "mongoid", "seed_database", "add_user", "omniauth", "home_page", "home_page_users", "css_setup", "application_layout", "navigation", "users_page", "omniauth_email", "cleanup", "ban_spiders", "git"]
45 |
46 | def recipes; @recipes end
47 | def recipe?(name); @recipes.include?(name) end
48 |
49 | def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
50 | def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
51 | def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end
52 |
53 | def ask_wizard(question)
54 | ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
55 | end
56 |
57 | def yes_wizard?(question)
58 | answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
59 | case answer.downcase
60 | when "yes", "y"
61 | true
62 | when "no", "n"
63 | false
64 | else
65 | yes_wizard?(question)
66 | end
67 | end
68 |
69 | def no_wizard?(question); !yes_wizard?(question) end
70 |
71 | def multiple_choice(question, choices)
72 | say_custom('question', question)
73 | values = {}
74 | choices.each_with_index do |choice,i|
75 | values[(i + 1).to_s] = choice[1]
76 | say_custom (i + 1).to_s + ')', choice[0]
77 | end
78 | answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
79 | values[answer]
80 | end
81 |
82 | @current_recipe = nil
83 | @configs = {}
84 |
85 | @after_blocks = []
86 | def after_bundler(&block); @after_blocks << [@current_recipe, block]; end
87 | @after_everything_blocks = []
88 | def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end
89 | @before_configs = {}
90 | def before_config(&block); @before_configs[@current_recipe] = block; end
91 |
92 |
93 | say_wizard "Checking configuration. Please confirm your preferences."
94 |
95 | # >--------------------------------[ jQuery ]---------------------------------<
96 |
97 | @current_recipe = "jquery"
98 | @before_configs["jquery"].call if @before_configs["jquery"]
99 | say_recipe 'jQuery'
100 |
101 | config = {}
102 | config['jquery'] = yes_wizard?("Would you like to use jQuery instead of Prototype?") if true && true unless config.key?('jquery')
103 | config['ui'] = yes_wizard?("Would you like to use jQuery UI?") if true && true unless config.key?('ui')
104 | @configs[@current_recipe] = config
105 |
106 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
107 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/jquery.rb
108 |
109 | if config['jquery']
110 | say_wizard "REMINDER: When creating a Rails app using jQuery..."
111 | say_wizard "you should add the '-J' flag to 'rails new'"
112 | after_bundler do
113 | say_wizard "jQuery recipe running 'after bundler'"
114 | # remove the Prototype adapter file
115 | remove_file 'public/javascripts/rails.js'
116 | # remove the Prototype files (if they exist)
117 | remove_file 'public/javascripts/controls.js'
118 | remove_file 'public/javascripts/dragdrop.js'
119 | remove_file 'public/javascripts/effects.js'
120 | remove_file 'public/javascripts/prototype.js'
121 | # add jQuery files
122 | inside "public/javascripts" do
123 | get "https://github.com/rails/jquery-ujs/raw/master/src/rails.js", "rails.js"
124 | get "http://code.jquery.com/jquery-1.6.min.js", "jquery.js"
125 | if config['ui']
126 | get "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js", "jqueryui.js"
127 | end
128 | end
129 | # adjust the Javascript defaults
130 | if config['ui']
131 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery jqueryui rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
132 | else
133 | inject_into_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w(jquery rails)\n", :after => "config.action_view.javascript_expansions[:defaults] = %w()\n", :verbose => false
134 | end
135 | gsub_file "config/application.rb", /config.action_view.javascript_expansions\[:defaults\] = \%w\(\)\n/, ""
136 | end
137 | else
138 | recipes.delete('jquery')
139 | end
140 |
141 |
142 | # >---------------------------------[ HAML ]----------------------------------<
143 |
144 | @current_recipe = "haml"
145 | @before_configs["haml"].call if @before_configs["haml"]
146 | say_recipe 'HAML'
147 |
148 | config = {}
149 | config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml')
150 | @configs[@current_recipe] = config
151 |
152 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
153 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/haml.rb
154 |
155 | if config['haml']
156 | gem 'haml', '>= 3.1.1'
157 | gem 'haml-rails', '>= 0.3.4', :group => :development
158 | else
159 | recipes.delete('haml')
160 | end
161 |
162 |
163 | # >---------------------------------[ RSpec ]---------------------------------<
164 |
165 | @current_recipe = "rspec"
166 | @before_configs["rspec"].call if @before_configs["rspec"]
167 | say_recipe 'RSpec'
168 |
169 | config = {}
170 | config['rspec'] = yes_wizard?("Would you like to use RSpec instead of TestUnit?") if true && true unless config.key?('rspec')
171 | config['factory_girl'] = yes_wizard?("Would you like to use factory_girl for test fixtures with RSpec?") if true && true unless config.key?('factory_girl')
172 | @configs[@current_recipe] = config
173 |
174 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
175 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/rspec.rb
176 |
177 | if config['rspec']
178 | say_wizard "REMINDER: When creating a Rails app using RSpec..."
179 | say_wizard "you should add the '-T' flag to 'rails new'"
180 | gem 'rspec-rails', '>= 2.5.0', :group => [:development, :test]
181 | if recipes.include? 'mongoid'
182 | # use the database_cleaner gem to reset the test database
183 | gem 'database_cleaner', '>= 0.6.7', :group => :test
184 | # include RSpec matchers from the mongoid-rspec gem
185 | gem 'mongoid-rspec', ">= 1.4.2", :group => :test
186 | end
187 | if config['factory_girl']
188 | # use the factory_girl gem for test fixtures
189 | gem 'factory_girl_rails', ">= 1.1.beta1", :group => :test
190 | end
191 | else
192 | recipes.delete('rspec')
193 | end
194 |
195 | # note: there is no need to specify the RSpec generator in the config/application.rb file
196 |
197 | if config['rspec']
198 | after_bundler do
199 | say_wizard "RSpec recipe running 'after bundler'"
200 | generate 'rspec:install'
201 |
202 | say_wizard "Removing test folder (not needed for RSpec)"
203 | run 'rm -rf test/'
204 |
205 | # don't generate spec tests for views and helpers
206 | inject_into_file 'config/application.rb', :after => "Rails::Application\n" do <<-RUBY
207 |
208 | config.generators do |g|
209 | g.view_specs false
210 | g.helper_specs false
211 | end
212 |
213 | RUBY
214 | end
215 |
216 |
217 | if recipes.include? 'mongoid'
218 |
219 | # remove ActiveRecord artifacts
220 | gsub_file 'spec/spec_helper.rb', /config.fixture_path/, '# config.fixture_path'
221 | gsub_file 'spec/spec_helper.rb', /config.use_transactional_fixtures/, '# config.use_transactional_fixtures'
222 |
223 | # reset your application database to a pristine state during testing
224 | inject_into_file 'spec/spec_helper.rb', :before => "\nend" do
225 | <<-RUBY
226 | \n
227 | # Clean up the database
228 | require 'database_cleaner'
229 | config.before(:suite) do
230 | DatabaseCleaner.strategy = :truncation
231 | DatabaseCleaner.orm = "mongoid"
232 | end
233 |
234 | config.before(:each) do
235 | DatabaseCleaner.clean
236 | end
237 | RUBY
238 | end
239 |
240 | # remove either possible occurrence of "require rails/test_unit/railtie"
241 | gsub_file 'config/application.rb', /require 'rails\/test_unit\/railtie'/, '# require "rails/test_unit/railtie"'
242 | gsub_file 'config/application.rb', /require "rails\/test_unit\/railtie"/, '# require "rails/test_unit/railtie"'
243 |
244 | # configure RSpec to use matchers from the mongoid-rspec gem
245 | create_file 'spec/support/mongoid.rb' do
246 | <<-RUBY
247 | RSpec.configure do |config|
248 | config.include Mongoid::Matchers
249 | end
250 | RUBY
251 | end
252 | end
253 |
254 | if recipes.include? 'devise'
255 | # add Devise test helpers
256 | create_file 'spec/support/devise.rb' do
257 | <<-RUBY
258 | RSpec.configure do |config|
259 | config.include Devise::TestHelpers, :type => :controller
260 | end
261 | RUBY
262 | end
263 | end
264 |
265 | end
266 | end
267 |
268 |
269 | # >-------------------------------[ Cucumber ]--------------------------------<
270 |
271 | @current_recipe = "cucumber"
272 | @before_configs["cucumber"].call if @before_configs["cucumber"]
273 | say_recipe 'Cucumber'
274 |
275 | config = {}
276 | config['cucumber'] = yes_wizard?("Would you like to use Cucumber for your BDD?") if true && true unless config.key?('cucumber')
277 | @configs[@current_recipe] = config
278 |
279 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
280 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cucumber.rb
281 |
282 | if config['cucumber']
283 | gem 'cucumber-rails', ">= 0.4.1", :group => :test
284 | gem 'capybara', ">= 0.4.1.2", :group => :test
285 | gem 'database_cleaner', '>= 0.6.7', :group => :test
286 | gem 'launchy', ">= 0.4.0", :group => :test
287 | else
288 | recipes.delete('cucumber')
289 | end
290 |
291 | if config['cucumber']
292 | after_bundler do
293 | say_wizard "Cucumber recipe running 'after bundler'"
294 | generate "cucumber:install --capybara#{' --rspec' if recipes.include?('rspec')}#{' -D' if recipes.include?('mongoid')}"
295 | if recipes.include? 'mongoid'
296 | gsub_file 'features/support/env.rb', /transaction/, "truncation"
297 | inject_into_file 'features/support/env.rb', :after => 'begin' do
298 | "\n DatabaseCleaner.orm = 'mongoid'"
299 | end
300 | end
301 | end
302 | end
303 |
304 | if config['cucumber']
305 | if recipes.include? 'devise'
306 | after_bundler do
307 | say_wizard "Copying Cucumber scenarios from the rails3-mongoid-devise examples"
308 | # copy all the Cucumber scenario files from the rails3-mongoid-devise example app
309 | inside 'features/users' do
310 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_in.feature', 'sign_in.feature'
311 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_out.feature', 'sign_out.feature'
312 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/sign_up.feature', 'sign_up.feature'
313 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_edit.feature', 'user_edit.feature'
314 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/users/user_show.feature', 'user_show.feature'
315 | end
316 | inside 'features/step_definitions' do
317 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/step_definitions/user_steps.rb', 'user_steps.rb'
318 | end
319 | remove_file 'features/support/paths.rb'
320 | inside 'features/support' do
321 | get 'https://github.com/fortuity/rails3-mongoid-devise/raw/master/features/support/paths.rb', 'paths.rb'
322 | end
323 | end
324 | end
325 | end
326 |
327 |
328 | # >--------------------------------[ Mongoid ]--------------------------------<
329 |
330 | @current_recipe = "mongoid"
331 | @before_configs["mongoid"].call if @before_configs["mongoid"]
332 | say_recipe 'Mongoid'
333 |
334 | config = {}
335 | config['mongoid'] = yes_wizard?("Would you like to use Mongoid to connect to a MongoDB database?") if true && true unless config.key?('mongoid')
336 | @configs[@current_recipe] = config
337 |
338 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
339 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/mongoid.rb
340 |
341 | if config['mongoid']
342 | say_wizard "REMINDER: When creating a Rails app using Mongoid..."
343 | say_wizard "you should add the '-O' flag to 'rails new'"
344 | gem 'bson_ext', '>= 1.3.0'
345 | gem 'mongoid', '>= 2.0.1'
346 | else
347 | recipes.delete('mongoid')
348 | end
349 |
350 | if config['mongoid']
351 | after_bundler do
352 | say_wizard "Mongoid recipe running 'after bundler'"
353 | # note: the mongoid generator automatically modifies the config/application.rb file
354 | # to remove the ActiveRecord dependency by commenting out "require active_record/railtie'"
355 | generate 'mongoid:config'
356 | # remove the unnecessary 'config/database.yml' file
357 | remove_file 'config/database.yml'
358 | end
359 | end
360 |
361 |
362 | # >-----------------------------[ SeedDatabase ]------------------------------<
363 |
364 | @current_recipe = "seed_database"
365 | @before_configs["seed_database"].call if @before_configs["seed_database"]
366 | say_recipe 'SeedDatabase'
367 |
368 |
369 | @configs[@current_recipe] = config
370 |
371 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
372 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/seed_database.rb
373 |
374 |
375 | after_bundler do
376 |
377 | say_wizard "SeedDatabase recipe running 'after bundler'"
378 |
379 | unless recipes.include? 'mongoid'
380 | run 'rake db:migrate'
381 | end
382 |
383 | if recipes.include? 'mongoid'
384 | append_file 'db/seeds.rb' do <<-FILE
385 | puts 'EMPTY THE MONGODB DATABASE'
386 | Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop)
387 | FILE
388 | end
389 | end
390 |
391 | if recipes.include? 'devise'
392 | # create a default user
393 | append_file 'db/seeds.rb' do <<-FILE
394 | puts 'SETTING UP DEFAULT USER LOGIN'
395 | user = User.create! :name => 'First User', :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
396 | puts 'New user created: ' << user.name
397 | FILE
398 | end
399 | end
400 |
401 | run 'rake db:seed'
402 |
403 | end
404 |
405 |
406 | # >--------------------------------[ AddUser ]--------------------------------<
407 |
408 | @current_recipe = "add_user"
409 | @before_configs["add_user"].call if @before_configs["add_user"]
410 | say_recipe 'AddUser'
411 |
412 |
413 | @configs[@current_recipe] = config
414 |
415 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
416 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/add_user.rb
417 |
418 | after_bundler do
419 |
420 | say_wizard "AddUser recipe running 'after bundler'"
421 |
422 | if recipes.include? 'omniauth'
423 | generate(:model, "user provider:string uid:string name:string email:string")
424 | gsub_file 'app/models/user.rb', /end/ do
425 | <<-RUBY
426 | attr_accessible :provider, :uid, :name, :email
427 | end
428 | RUBY
429 | end
430 | end
431 |
432 | if recipes.include? 'devise'
433 |
434 | # Generate models and routes for a User
435 | generate 'devise user'
436 |
437 | # Add a 'name' attribute to the User model
438 | if recipes.include? 'mongoid'
439 | gsub_file 'app/models/user.rb', /end/ do
440 | <<-RUBY
441 | field :name
442 | validates_presence_of :name
443 | validates_uniqueness_of :name, :email, :case_sensitive => false
444 | attr_accessible :name, :email, :password, :password_confirmation, :remember_me
445 | end
446 | RUBY
447 | end
448 | else
449 | # for ActiveRecord
450 | # Devise created a Users database, we'll modify it
451 | generate 'migration AddNameToUsers name:string'
452 | # Devise created a Users model, we'll modify it
453 | gsub_file 'app/models/user.rb', /attr_accessible :email/, 'attr_accessible :name, :email'
454 | inject_into_file 'app/models/user.rb', :before => 'validates_uniqueness_of' do
455 | "validates_presence_of :name\n"
456 | end
457 | gsub_file 'app/models/user.rb', /validates_uniqueness_of :email/, 'validates_uniqueness_of :name, :email'
458 | end
459 |
460 | unless recipes.include? 'haml'
461 |
462 | # Generate Devise views (unless you are using Haml)
463 | run 'rails generate devise:views'
464 |
465 | # Modify Devise views to add 'name'
466 | inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
467 | <<-ERB
468 | <%= f.label :name %>
469 | <%= f.text_field :name %>
470 | ERB
471 | end
472 |
473 | inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
474 | <<-ERB
475 | <%= f.label :name %>
476 | <%= f.text_field :name %>
477 | ERB
478 | end
479 |
480 | else
481 |
482 | # copy Haml versions of modified Devise views
483 | inside 'app/views/devise/registrations' do
484 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/edit.html.haml', 'edit.html.haml'
485 | get 'https://github.com/fortuity/rails3-application-templates/raw/master/files/rails3-mongoid-devise/app/views/devise/registrations/new.html.haml', 'new.html.haml'
486 | end
487 |
488 | end
489 |
490 | end
491 |
492 | end
493 |
494 |
495 | # >-------------------------------[ OmniAuth ]--------------------------------<
496 |
497 | @current_recipe = "omniauth"
498 | @before_configs["omniauth"].call if @before_configs["omniauth"]
499 | say_recipe 'OmniAuth'
500 |
501 | config = {}
502 | config['omniauth'] = yes_wizard?("Would you like to use OmniAuth for authentication?") if true && true unless config.key?('omniauth')
503 | config['provider'] = multiple_choice("Which service provider will you use?", [["Twitter", "twitter"], ["Facebook", "facebook"], ["GitHub", "github"], ["LinkedIn", "linked_in"], ["Other", "provider"]]) if true && true unless config.key?('provider')
504 | @configs[@current_recipe] = config
505 |
506 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
507 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/omniauth.rb
508 |
509 | if config['omniauth']
510 | gem 'omniauth', '>= 0.2.4'
511 | else
512 | recipes.delete('omniauth')
513 | end
514 |
515 | if config['omniauth']
516 | after_bundler do
517 |
518 | # Don't use single-quote-style-heredoc: we want interpolation.
519 | create_file 'config/initializers/omniauth.rb' do <<-RUBY
520 | Rails.application.config.middleware.use OmniAuth::Builder do
521 | provider :#{config['provider']}, 'KEY', 'SECRET'
522 | end
523 | RUBY
524 | end
525 |
526 | route "match '/auth/failure' => 'sessions#failure'"
527 | route "match '/signout' => 'sessions#destroy', :as => :signout"
528 | route "match '/signin' => 'sessions#new', :as => :signin"
529 | route "match '/auth/:provider/callback' => 'sessions#create'"
530 | route "resources :users, :only => [ :show, :edit, :update ]"
531 |
532 | inject_into_file 'app/models/user.rb', :before => 'end' do <<-RUBY
533 |
534 | def self.create_with_omniauth(auth)
535 | create! do |user|
536 | user.provider = auth['provider']
537 | user.uid = auth['uid']
538 | user.name = auth['user_info']['name'] if auth['user_info']['name'] # Twitter, Google, Yahoo, GitHub
539 | user.email = auth['user_info']['email'] if auth['user_info']['email'] # Google, Yahoo, GitHub
540 | user.name = auth['extra']['user_hash']['name'] if auth['extra']['user_hash']['name'] # Facebook
541 | user.email = auth['extra']['user_hash']['email'] if auth['extra']['user_hash']['email'] # Facebook
542 | end
543 | end
544 |
545 | RUBY
546 | end
547 |
548 | # We have to use single-quote-style-heredoc to avoid interpolation.
549 | create_file 'app/controllers/sessions_controller.rb', do <<-'RUBY'
550 | class SessionsController < ApplicationController
551 |
552 | def create
553 | auth = request.env["omniauth.auth"]
554 | user = User.where(:provider => auth['provider'],
555 | :uid => auth['uid']).first || User.create_with_omniauth(auth)
556 | session[:user_id] = user.id
557 | redirect_to root_url, :notice => 'Signed in!'
558 | end
559 |
560 | def destroy
561 | session[:user_id] = nil
562 | redirect_to root_url, :notice => 'Signed out!'
563 | end
564 |
565 | def failure
566 | redirect_to root_url, :alert => "Authentication error: #{params[:message].humanize}"
567 | end
568 |
569 | end
570 | RUBY
571 | end
572 |
573 | # Don't use single-quote-style-heredoc: we want interpolation.
574 | inject_into_class 'app/controllers/sessions_controller.rb', 'SessionsController' do <<-RUBY
575 |
576 | def new
577 | redirect_to '/auth/#{config['provider']}'
578 | end
579 |
580 | RUBY
581 | end
582 |
583 | inject_into_file 'app/controllers/application_controller.rb', :before => 'end' do <<-RUBY
584 | helper_method :current_user
585 | helper_method :user_signed_in?
586 | helper_method :correct_user?
587 |
588 | private
589 | def current_user
590 | begin
591 | @current_user ||= User.find(session[:user_id]) if session[:user_id]
592 | rescue Mongoid::Errors::DocumentNotFound
593 | nil
594 | end
595 | end
596 |
597 | def user_signed_in?
598 | return true if current_user
599 | end
600 |
601 | def correct_user?
602 | @user = User.find(params[:id])
603 | unless current_user == @user
604 | redirect_to root_url, :alert => "Access denied."
605 | end
606 | end
607 |
608 | def authenticate_user!
609 | if !current_user
610 | redirect_to root_url, :alert => 'You need to sign in for access to this page.'
611 | end
612 | end
613 |
614 | RUBY
615 | end
616 |
617 | end
618 | end
619 |
620 |
621 | # >-------------------------------[ HomePage ]--------------------------------<
622 |
623 | @current_recipe = "home_page"
624 | @before_configs["home_page"].call if @before_configs["home_page"]
625 | say_recipe 'HomePage'
626 |
627 |
628 | @configs[@current_recipe] = config
629 |
630 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
631 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page.rb
632 |
633 | after_bundler do
634 |
635 | say_wizard "HomePage recipe running 'after bundler'"
636 |
637 | # remove the default home page
638 | remove_file 'public/index.html'
639 |
640 | # create a home controller and view
641 | generate(:controller, "home index")
642 |
643 | # set up a simple home page (with placeholder content)
644 | if recipes.include? 'haml'
645 | remove_file 'app/views/home/index.html.haml'
646 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
647 | # We have to use single-quote-style-heredoc to avoid interpolation.
648 | create_file 'app/views/home/index.html.haml' do
649 | <<-'HAML'
650 | %h3 Home
651 | HAML
652 | end
653 | else
654 | remove_file 'app/views/home/index.html.erb'
655 | create_file 'app/views/home/index.html.erb' do
656 | <<-ERB
657 | Home
658 | ERB
659 | end
660 | end
661 |
662 | # set routes
663 | gsub_file 'config/routes.rb', /get \"home\/index\"/, 'root :to => "home#index"'
664 |
665 | end
666 |
667 |
668 | # >-----------------------------[ HomePageUsers ]-----------------------------<
669 |
670 | @current_recipe = "home_page_users"
671 | @before_configs["home_page_users"].call if @before_configs["home_page_users"]
672 | say_recipe 'HomePageUsers'
673 |
674 |
675 | @configs[@current_recipe] = config
676 |
677 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
678 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/home_page_users.rb
679 |
680 | after_bundler do
681 |
682 | say_wizard "HomePageUsers recipe running 'after bundler'"
683 |
684 | # Modify the home controller
685 | gsub_file 'app/controllers/home_controller.rb', /def index/ do
686 | <<-RUBY
687 | def index
688 | @users = User.all
689 | RUBY
690 | end
691 |
692 | # Replace the home page
693 | if recipes.include? 'haml'
694 | remove_file 'app/views/home/index.html.haml'
695 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
696 | # We have to use single-quote-style-heredoc to avoid interpolation.
697 | create_file 'app/views/home/index.html.haml' do
698 | <<-'HAML'
699 | %h3 Home
700 | - @users.each do |user|
701 | %p User: #{user.name}
702 | HAML
703 | end
704 | else
705 | append_file 'app/views/home/index.html.erb' do <<-ERB
706 | Home
707 | <% @users.each do |user| %>
708 | User: <%= user.name %>
709 | <% end %>
710 | ERB
711 | end
712 | end
713 |
714 | end
715 |
716 |
717 | # >-------------------------------[ CssSetup ]--------------------------------<
718 |
719 | @current_recipe = "css_setup"
720 | @before_configs["css_setup"].call if @before_configs["css_setup"]
721 | say_recipe 'CssSetup'
722 |
723 |
724 | @configs[@current_recipe] = config
725 |
726 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
727 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/css_setup.rb
728 |
729 | after_bundler do
730 |
731 | say_wizard "CssSetup recipe running 'after bundler'"
732 |
733 | # Add a stylesheet with styles for a horizontal menu and flash messages
734 | create_file 'public/stylesheets/application.css' do <<-CSS
735 | ul.hmenu {
736 | list-style: none;
737 | margin: 0 0 2em;
738 | padding: 0;
739 | }
740 | ul.hmenu li {
741 | display: inline;
742 | }
743 | #flash_notice, #flash_alert {
744 | padding: 5px 8px;
745 | margin: 10px 0;
746 | }
747 | #flash_notice {
748 | background-color: #CFC;
749 | border: solid 1px #6C6;
750 | }
751 | #flash_alert {
752 | background-color: #FCC;
753 | border: solid 1px #C66;
754 | }
755 | CSS
756 | end
757 |
758 | end
759 |
760 |
761 | # >---------------------------[ ApplicationLayout ]---------------------------<
762 |
763 | @current_recipe = "application_layout"
764 | @before_configs["application_layout"].call if @before_configs["application_layout"]
765 | say_recipe 'ApplicationLayout'
766 |
767 |
768 | @configs[@current_recipe] = config
769 |
770 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
771 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/application_layout.rb
772 |
773 | after_bundler do
774 |
775 | say_wizard "ApplicationLayout recipe running 'after bundler'"
776 |
777 | # Set up the default application layout
778 | if recipes.include? 'haml'
779 | remove_file 'app/views/layouts/application.html.erb'
780 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
781 | create_file 'app/views/layouts/application.html.haml' do <<-HAML
782 | !!!
783 | %html
784 | %head
785 | %title #{app_name}
786 | = stylesheet_link_tag :all
787 | = javascript_include_tag :defaults
788 | = csrf_meta_tag
789 | %body
790 | - flash.each do |name, msg|
791 | = content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String)
792 | = yield
793 | HAML
794 | end
795 | else
796 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
797 | <<-ERB
798 | <%- flash.each do |name, msg| -%>
799 | <%= content_tag :div, msg, :id => "flash_\#{name}" if msg.is_a?(String) %>
800 | <%- end -%>
801 | ERB
802 | end
803 | end
804 |
805 | end
806 |
807 |
808 | # >------------------------------[ Navigation ]-------------------------------<
809 |
810 | @current_recipe = "navigation"
811 | @before_configs["navigation"].call if @before_configs["navigation"]
812 | say_recipe 'Navigation'
813 |
814 |
815 | @configs[@current_recipe] = config
816 |
817 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
818 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/navigation.rb
819 |
820 | after_bundler do
821 |
822 | say_wizard "Navigation recipe running 'after bundler'"
823 |
824 | # Create navigation links
825 | if recipes.include? 'haml'
826 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
827 | # We have to use single-quote-style-heredoc to avoid interpolation.
828 | create_file "app/views/shared/_navigation.html.haml" do <<-'HAML'
829 | - if user_signed_in?
830 | %li
831 | Logged in as #{current_user.name}
832 | %li
833 | = link_to('Logout', signout_path)
834 | - else
835 | %li
836 | = link_to('Login', signin_path)
837 | HAML
838 | end
839 | else
840 | create_file "app/views/shared/_navigation.html.erb" do <<-ERB
841 | <% if user_signed_in? %>
842 |
843 | Logged in as <%= current_user.name %>
844 |
845 |
846 | <%= link_to('Logout', signout_path) %>
847 |
848 | <% else %>
849 |
850 | <%= link_to('Login', signin_path) %>
851 |
852 | <% end %>
853 | ERB
854 | end
855 | end
856 |
857 | # Add navigation links to the default application layout
858 | if recipes.include? 'haml'
859 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
860 | inject_into_file 'app/views/layouts/application.html.haml', :after => "%body\n" do <<-HAML
861 | %ul.hmenu
862 | = render 'shared/navigation'
863 | HAML
864 | end
865 | else
866 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
867 | <<-ERB
868 |
871 | ERB
872 | end
873 | end
874 |
875 | end
876 |
877 |
878 | # >-------------------------------[ UsersPage ]-------------------------------<
879 |
880 | @current_recipe = "users_page"
881 | @before_configs["users_page"].call if @before_configs["users_page"]
882 | say_recipe 'UsersPage'
883 |
884 |
885 | @configs[@current_recipe] = config
886 |
887 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
888 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/users_page.rb
889 |
890 | after_bundler do
891 |
892 | say_wizard "UsersPage recipe running 'after bundler'"
893 |
894 | #----------------------------------------------------------------------------
895 | # Create a users controller
896 | #----------------------------------------------------------------------------
897 | generate(:controller, "users show")
898 | gsub_file 'app/controllers/users_controller.rb', /def show/ do
899 | <<-RUBY
900 | before_filter :authenticate_user!
901 |
902 | def show
903 | @user = User.find(params[:id])
904 | RUBY
905 | end
906 |
907 | #----------------------------------------------------------------------------
908 | # Modify the routes
909 | #----------------------------------------------------------------------------
910 | # @devise_for :users@ route must be placed above @resources :users, :only => :show@.
911 | gsub_file 'config/routes.rb', /get \"users\/show\"/, '#get \"users\/show\"'
912 | gsub_file 'config/routes.rb', /devise_for :users/ do
913 | <<-RUBY
914 | devise_for :users
915 | resources :users, :only => :show
916 | RUBY
917 | end
918 |
919 | #----------------------------------------------------------------------------
920 | # Create a users show page
921 | #----------------------------------------------------------------------------
922 | if recipes.include? 'haml'
923 | remove_file 'app/views/users/show.html.haml'
924 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
925 | # We have to use single-quote-style-heredoc to avoid interpolation.
926 | create_file 'app/views/users/show.html.haml' do <<-'HAML'
927 | %p
928 | User: #{@user.name}
929 | %p
930 | Email: #{@user.email if @user.email}
931 | HAML
932 | end
933 | else
934 | append_file 'app/views/users/show.html.erb' do <<-ERB
935 | User: <%= @user.name %>
936 | Email: <%= @user.email if @user.email %>
937 | ERB
938 | end
939 | end
940 |
941 | #----------------------------------------------------------------------------
942 | # Create a home page containing links to user show pages
943 | # (clobbers code from the home_page_users recipe)
944 | #----------------------------------------------------------------------------
945 | # set up the controller
946 | remove_file 'app/controllers/home_controller.rb'
947 | create_file 'app/controllers/home_controller.rb' do
948 | <<-RUBY
949 | class HomeController < ApplicationController
950 | def index
951 | @users = User.all
952 | end
953 | end
954 | RUBY
955 | end
956 |
957 | # modify the home page
958 | if recipes.include? 'haml'
959 | remove_file 'app/views/home/index.html.haml'
960 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
961 | # We have to use single-quote-style-heredoc to avoid interpolation.
962 | create_file 'app/views/home/index.html.haml' do
963 | <<-'HAML'
964 | %h3 Home
965 | - @users.each do |user|
966 | %p User: #{link_to user.name, user}
967 | HAML
968 | end
969 | else
970 | remove_file 'app/views/home/index.html.erb'
971 | create_file 'app/views/home/index.html.erb' do <<-ERB
972 | Home
973 | <% @users.each do |user| %>
974 | User: <%=link_to user.name, user %>
975 | <% end %>
976 | ERB
977 | end
978 | end
979 |
980 | end
981 |
982 |
983 | # >-----------------------------[ OmniAuthEmail ]-----------------------------<
984 |
985 | @current_recipe = "omniauth_email"
986 | @before_configs["omniauth_email"].call if @before_configs["omniauth_email"]
987 | say_recipe 'OmniAuthEmail'
988 |
989 |
990 | @configs[@current_recipe] = config
991 |
992 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
993 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/omniauth_email.rb
994 |
995 | after_bundler do
996 |
997 | say_wizard "OmniAuthEmail recipe running 'after bundler'"
998 |
999 | #----------------------------------------------------------------------------
1000 | # Modify a users controller
1001 | #----------------------------------------------------------------------------
1002 | inject_into_file 'app/controllers/users_controller.rb', :after => "before_filter :authenticate_user!\n" do <<-RUBY
1003 | before_filter :correct_user?
1004 | RUBY
1005 | end
1006 |
1007 | inject_into_file 'app/controllers/users_controller.rb', :before => 'def show' do <<-RUBY
1008 | def edit
1009 | @user = User.find(params[:id])
1010 | end
1011 |
1012 | def update
1013 | @user = User.find(params[:id])
1014 | if @user.update_attributes(params[:user])
1015 | redirect_to @user
1016 | else
1017 | render :edit
1018 | end
1019 | end
1020 | \n
1021 | RUBY
1022 | end
1023 |
1024 | #----------------------------------------------------------------------------
1025 | # Create a users edit page
1026 | #----------------------------------------------------------------------------
1027 | if recipes.include? 'haml'
1028 | remove_file 'app/views/users/edit.html.haml'
1029 | # There is Haml code in this script. Changing the indentation is perilous between HAMLs.
1030 | # We have to use single-quote-style-heredoc to avoid interpolation.
1031 | create_file 'app/views/users/edit.html.haml' do <<-'HAML'
1032 | = form_for(@user) do |f|
1033 | = f.label :email
1034 | = f.text_field :email
1035 | %br/
1036 | = f.submit "Sign in"
1037 | HAML
1038 | end
1039 | else
1040 | create_file 'app/views/users/edit.html.erb' do <<-ERB
1041 | <%= form_for(@user) do |f| %>
1042 | <%= f.label :email %>
1043 | <%= f.text_field :email %>
1044 |
1045 | <%= f.submit "Sign in" %>
1046 | <% end %>
1047 | ERB
1048 | end
1049 | end
1050 |
1051 | #----------------------------------------------------------------------------
1052 | # Modify a Sessions controller
1053 | #----------------------------------------------------------------------------
1054 | gsub_file 'app/controllers/sessions_controller.rb', /redirect_to root_url, :notice => 'Signed in!'/ do
1055 | <<-RUBY
1056 | if !user.email
1057 | redirect_to edit_user_path(user), :alert => "Please enter your email address."
1058 | else
1059 | redirect_to root_url, :notice => 'Signed in!'
1060 | end
1061 | RUBY
1062 | end
1063 |
1064 | end
1065 |
1066 |
1067 | # >--------------------------------[ Cleanup ]--------------------------------<
1068 |
1069 | @current_recipe = "cleanup"
1070 | @before_configs["cleanup"].call if @before_configs["cleanup"]
1071 | say_recipe 'Cleanup'
1072 |
1073 |
1074 | @configs[@current_recipe] = config
1075 |
1076 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1077 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cleanup.rb
1078 |
1079 | after_bundler do
1080 |
1081 | say_wizard "Cleanup recipe running 'after bundler'"
1082 |
1083 | # remove unnecessary files
1084 | %w{
1085 | README
1086 | doc/README_FOR_APP
1087 | public/index.html
1088 | public/images/rails.png
1089 | }.each { |file| remove_file file }
1090 |
1091 | # add placeholder READMEs
1092 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.txt", "README"
1093 | get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.textile", "README.textile"
1094 | gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}"
1095 | gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}"
1096 |
1097 | # remove commented lines from Gemfile
1098 | # thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb
1099 | gsub_file "Gemfile", /#.*\n/, "\n"
1100 | gsub_file "Gemfile", /\n+/, "\n"
1101 |
1102 | end
1103 |
1104 |
1105 | # >------------------------------[ BanSpiders ]-------------------------------<
1106 |
1107 | @current_recipe = "ban_spiders"
1108 | @before_configs["ban_spiders"].call if @before_configs["ban_spiders"]
1109 | say_recipe 'BanSpiders'
1110 |
1111 | config = {}
1112 | config['ban_spiders'] = yes_wizard?("Would you like to set a robots.txt file to ban spiders?") if true && true unless config.key?('ban_spiders')
1113 | @configs[@current_recipe] = config
1114 |
1115 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1116 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/ban_spiders.rb
1117 |
1118 | if config['ban_spiders']
1119 | say_wizard "BanSpiders recipe running 'after bundler'"
1120 | after_bundler do
1121 | # ban spiders from your site by changing robots.txt
1122 | gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
1123 | gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
1124 | end
1125 | else
1126 | recipes.delete('ban_spiders')
1127 | end
1128 |
1129 |
1130 | # >----------------------------------[ Git ]----------------------------------<
1131 |
1132 | @current_recipe = "git"
1133 | @before_configs["git"].call if @before_configs["git"]
1134 | say_recipe 'Git'
1135 |
1136 |
1137 | @configs[@current_recipe] = config
1138 |
1139 | # Application template recipe for the rails3_devise_wizard. Check for a newer version here:
1140 | # https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/git.rb
1141 |
1142 | after_everything do
1143 |
1144 | say_wizard "Git recipe running 'after everything'"
1145 |
1146 | # Git should ignore some files
1147 | remove_file '.gitignore'
1148 | get "https://github.com/fortuity/rails3-gitignore/raw/master/gitignore.txt", ".gitignore"
1149 |
1150 | if recipes.include? 'omniauth'
1151 | append_file '.gitignore' do <<-TXT
1152 | # keep OmniAuth service provider secrets out of the Git repo
1153 | config/initializers/omniauth.rb
1154 | TXT
1155 | end
1156 | end
1157 |
1158 | # Initialize new Git repo
1159 | git :init
1160 | git :add => '.'
1161 | git :commit => "-aqm 'new Rails app generated by rails3_devise_wizard'"
1162 | # Create a git branch
1163 | git :checkout => ' -b working_branch'
1164 | git :add => '.'
1165 | git :commit => "-m 'Initial commit of working_branch'"
1166 | end
1167 |
1168 |
1169 |
1170 |
1171 |
1172 | @current_recipe = nil
1173 |
1174 | # >-----------------------------[ Run Bundler ]-------------------------------<
1175 |
1176 | say_wizard "Running 'bundle install'. This will take a while."
1177 | run 'bundle install'
1178 | say_wizard "Running 'after bundler' callbacks."
1179 | @after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1180 |
1181 | @current_recipe = nil
1182 | say_wizard "Running 'after everything' callbacks."
1183 | @after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
1184 |
1185 | @current_recipe = nil
1186 | say_wizard "Finished running the rails3_devise_wizard app template."
1187 | say_wizard "Your new Rails app is ready. Any problems?"
1188 | say_wizard "See http://github.com/fortuity/rails3-mongoid-omniauth/issues"
1189 |
--------------------------------------------------------------------------------
/rails3-subdomain-devise-template.rb:
--------------------------------------------------------------------------------
1 | # Application Generator Template
2 | # Modifies a Rails app to set up subdomains with Devise
3 | # Usage: rails new app_name -m https://github.com/fortuity/rails3-application-templates/raw/master/rails3-subdomain-devise-template.rb
4 |
5 | # More info: http://github.com/fortuity/rails3-subdomain-devise/
6 |
7 | # If you are customizing this template, you can use any methods provided by Thor::Actions
8 | # http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html
9 | # and Rails::Generators::Actions
10 | # http://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb
11 |
12 | puts "Modifying a new Rails app to set up subdomains with Devise..."
13 | puts "Any problems? See http://github.com/fortuity/rails3-subdomain-devise/issues"
14 |
15 | #----------------------------------------------------------------------------
16 | # Configure
17 | #----------------------------------------------------------------------------
18 |
19 | if yes?('Would you like to use Mongoid instead of ActiveRecord for database access? (yes/no)')
20 | mongoid_flag = true
21 | else
22 | mongoid_flag = false
23 | end
24 |
25 | if yes?('Would you like to use the Haml template system? (yes/no)')
26 | haml_flag = true
27 | else
28 | haml_flag = false
29 | end
30 |
31 | if yes?('Would you like to use jQuery instead of Prototype? (yes/no)')
32 | jquery_flag = true
33 | else
34 | jquery_flag = false
35 | end
36 |
37 | if yes?('Do you want to install the Heroku gem so you can deploy to Heroku? (yes/no)')
38 | heroku_flag = true
39 | else
40 | heroku_flag = false
41 | end
42 |
43 | #----------------------------------------------------------------------------
44 | # Set up git
45 | #----------------------------------------------------------------------------
46 | puts "setting up source control with 'git'..."
47 | # specific to Mac OS X
48 | append_file '.gitignore' do
49 | '.DS_Store'
50 | end
51 | git :init
52 | git :add => '.'
53 | git :commit => "-m 'Initial commit of unmodified new Rails app'"
54 |
55 | #----------------------------------------------------------------------------
56 | # Remove the usual cruft
57 | #----------------------------------------------------------------------------
58 | puts "removing unneeded files..."
59 | run 'rm public/index.html'
60 | run 'rm public/favicon.ico'
61 | run 'rm public/images/rails.png'
62 | run 'rm README'
63 | run 'touch README'
64 |
65 | puts "banning spiders from your site by changing robots.txt..."
66 | gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
67 | gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
68 |
69 | puts "setting up the Gemfile..."
70 | run 'rm Gemfile'
71 | create_file 'Gemfile', "source 'http://rubygems.org'\n"
72 | gem 'rails', '3.0.4'
73 | if !mongoid_flag
74 | gem 'sqlite3'
75 | gem 'friendly_id', '3.2.1'
76 | end
77 | gem 'devise', '1.2.rc'
78 |
79 | #----------------------------------------------------------------------------
80 | # Heroku Option
81 | #----------------------------------------------------------------------------
82 | if heroku_flag
83 | puts "adding Heroku gem to the Gemfile..."
84 | gem 'heroku', '1.17.13', :group => :development
85 | end
86 |
87 | #----------------------------------------------------------------------------
88 | # Haml Option
89 | #----------------------------------------------------------------------------
90 | if haml_flag
91 | puts "setting up Gemfile for Haml..."
92 | append_file 'Gemfile', "\n# Bundle gems needed for Haml\n"
93 | gem 'haml', '3.0.25'
94 | gem 'haml-rails', '0.3.4', :group => :development
95 | # the following gems are used to generate Devise views for Haml
96 | gem 'hpricot', '0.8.3', :group => :development
97 | gem 'ruby_parser', '2.0.5', :group => :development
98 | end
99 |
100 | #----------------------------------------------------------------------------
101 | # jQuery Option
102 | #----------------------------------------------------------------------------
103 | if jquery_flag
104 | puts "setting up Gemfile for jQuery..."
105 | gem 'jquery-rails', '0.2.7'
106 | end
107 |
108 | #----------------------------------------------------------------------------
109 | # Mongoid Option
110 | #----------------------------------------------------------------------------
111 | if mongoid_flag
112 | puts "setting up Gemfile for Mongoid..."
113 | gsub_file 'Gemfile', /gem \'sqlite3-ruby/, '# gem \'sqlite3-ruby'
114 | append_file 'Gemfile', "\n# Bundle gems needed for Mongoid\n"
115 | gem "mongoid", "2.0.0.rc.7"
116 | gem 'bson_ext', '1.2.2'
117 | end
118 |
119 | puts "installing gems (takes a few minutes!)..."
120 | run 'bundle install'
121 |
122 | #----------------------------------------------------------------------------
123 | # Set up Mongoid
124 | #----------------------------------------------------------------------------
125 | if mongoid_flag
126 | puts "creating 'config/mongoid.yml' Mongoid configuration file..."
127 | run 'rails generate mongoid:config'
128 |
129 | puts "modifying 'config/application.rb' file for Mongoid..."
130 | gsub_file 'config/application.rb', /require 'rails\/all'/ do
131 | <<-RUBY
132 | # If you are deploying to Heroku and MongoHQ,
133 | # you supply connection information here.
134 | require 'uri'
135 | if ENV['MONGOHQ_URL']
136 | mongo_uri = URI.parse(ENV['MONGOHQ_URL'])
137 | ENV['MONGOID_HOST'] = mongo_uri.host
138 | ENV['MONGOID_PORT'] = mongo_uri.port.to_s
139 | ENV['MONGOID_USERNAME'] = mongo_uri.user
140 | ENV['MONGOID_PASSWORD'] = mongo_uri.password
141 | ENV['MONGOID_DATABASE'] = mongo_uri.path.gsub('/', '')
142 | end
143 |
144 | require 'mongoid/railtie'
145 | require 'action_controller/railtie'
146 | require 'action_mailer/railtie'
147 | require 'active_resource/railtie'
148 | require 'rails/test_unit/railtie'
149 | RUBY
150 | end
151 | end
152 |
153 | if mongoid_flag
154 | run 'rm config/database.yml'
155 | end
156 |
157 | #----------------------------------------------------------------------------
158 | # Tweak config/application.rb for Mongoid
159 | #----------------------------------------------------------------------------
160 | if mongoid_flag
161 | gsub_file 'config/application.rb', /# Configure the default encoding used in templates for Ruby 1.9./ do
162 | <<-RUBY
163 | config.generators do |g|
164 | g.orm :mongoid
165 | end
166 |
167 | # Configure the default encoding used in templates for Ruby 1.9.
168 | RUBY
169 | end
170 | end
171 |
172 | puts "prevent logging of passwords"
173 | gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
174 |
175 | if !mongoid_flag
176 | puts "setting up a migration for use with FriendlyId..."
177 | run 'rails generate friendly_id'
178 | end
179 |
180 | #----------------------------------------------------------------------------
181 | # Set up jQuery
182 | #----------------------------------------------------------------------------
183 | if jquery_flag
184 | run 'rm public/javascripts/rails.js'
185 | puts "replacing Prototype with jQuery"
186 | # "--ui" enables optional jQuery UI
187 | run 'rails generate jquery:install --ui'
188 | end
189 |
190 | #----------------------------------------------------------------------------
191 | # Set up Devise
192 | #----------------------------------------------------------------------------
193 | puts "creating 'config/initializers/devise.rb' Devise configuration file..."
194 | run 'rails generate devise:install'
195 | run 'rails generate devise:views'
196 |
197 | puts "modifying environment configuration files for Devise..."
198 | gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '### ActionMailer Config'
199 | gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
200 | <<-RUBY
201 | config.action_mailer.default_url_options = { :host => 'localhost:3000' }
202 | # A dummy setup for development - no deliveries, but logged
203 | config.action_mailer.delivery_method = :smtp
204 | config.action_mailer.perform_deliveries = false
205 | config.action_mailer.raise_delivery_errors = true
206 | config.action_mailer.default :charset => "utf-8"
207 | RUBY
208 | end
209 | gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
210 | <<-RUBY
211 | config.i18n.fallbacks = true
212 |
213 | config.action_mailer.default_url_options = { :host => 'yourhost.com' }
214 | ### ActionMailer Config
215 | # Setup for production - deliveries, no errors raised
216 | config.action_mailer.delivery_method = :smtp
217 | config.action_mailer.perform_deliveries = true
218 | config.action_mailer.raise_delivery_errors = false
219 | config.action_mailer.default :charset => "utf-8"
220 | RUBY
221 | end
222 |
223 | puts "creating a User model and modifying routes for Devise..."
224 | run 'rails generate devise User'
225 |
226 | puts "adding a 'name' attribute to the User model"
227 | if mongoid_flag
228 | inject_into_file 'app/models/user.rb', :after => ":recoverable, :rememberable, :trackable, :validatable\n" do
229 | <<-RUBY
230 | field :name
231 | validates_presence_of :name
232 | validates_uniqueness_of :name, :email, :case_sensitive => false
233 | attr_accessible :name, :email, :password, :password_confirmation, :remember_me
234 | key :name
235 | references_many :subdomains, :dependent => :destroy
236 | RUBY
237 | end
238 | else
239 | run 'rm app/models/user.rb'
240 | create_file 'app/models/user.rb' do
241 | <<-RUBY
242 | class User < ActiveRecord::Base
243 | devise :database_authenticatable, :registerable,
244 | :recoverable, :rememberable, :trackable, :validatable
245 | has_many :subdomains, :dependent => :destroy
246 | validates_presence_of :name
247 | validates_uniqueness_of :name, :email, :case_sensitive => false
248 | attr_accessible :name, :email, :password, :password_confirmation, :remember_me
249 | has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
250 | end
251 | RUBY
252 | end
253 | end
254 |
255 | if !mongoid_flag
256 | puts "creating a database migration to add 'name' to the User table"
257 | generate(:migration, "AddNameToUsers name:string")
258 | end
259 |
260 | #----------------------------------------------------------------------------
261 | # Modify Devise views
262 | #----------------------------------------------------------------------------
263 | puts "modifying the default Devise user registration to add 'name'..."
264 | if haml_flag
265 | inject_into_file "app/views/devise/registrations/edit.html.haml", :after => "= devise_error_messages!\n" do
266 | <<-RUBY
267 | %p
268 | = f.label :name
269 | %br/
270 | = f.text_field :name
271 | RUBY
272 | end
273 | else
274 | inject_into_file "app/views/devise/registrations/edit.html.erb", :after => "<%= devise_error_messages! %>\n" do
275 | <<-RUBY
276 | <%= f.label :name %>
277 | <%= f.text_field :name %>
278 | RUBY
279 | end
280 | end
281 |
282 | if haml_flag
283 | inject_into_file "app/views/devise/registrations/new.html.haml", :after => "= devise_error_messages!\n" do
284 | <<-RUBY
285 | %p
286 | = f.label :name
287 | %br/
288 | = f.text_field :name
289 | RUBY
290 | end
291 | else
292 | inject_into_file "app/views/devise/registrations/new.html.erb", :after => "<%= devise_error_messages! %>\n" do
293 | <<-RUBY
294 | <%= f.label :name %>
295 | <%= f.text_field :name %>
296 | RUBY
297 | end
298 | end
299 |
300 | #----------------------------------------------------------------------------
301 | # Create a Subdomain model
302 | #----------------------------------------------------------------------------
303 | puts "creating a Subdomain model..."
304 | if mongoid_flag
305 | generate(:model, "Subdomain name:string")
306 | inject_into_file 'app/models/subdomain.rb', :after => "field :name, :type => String\n" do
307 | <<-RUBY
308 | key :name
309 | referenced_in :user
310 | validates_uniqueness_of :name, :case_sensitive => false
311 | validates_presence_of :name
312 | RUBY
313 | end
314 | else
315 | generate(:model, "Subdomain name:string user:references")
316 | run 'rm app/models/subdomain.rb'
317 | create_file 'app/models/subdomain.rb' do
318 | <<-RUBY
319 | class Subdomain < ActiveRecord::Base
320 | belongs_to :user
321 | has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
322 | validates_uniqueness_of :name, :case_sensitive => false
323 | validates_presence_of :name
324 | end
325 | RUBY
326 | end
327 | end
328 |
329 | puts "setting up the Site model"
330 | create_file 'app/models/site.rb' do
331 | <<-RUBY
332 | class Site < Subdomain
333 | end
334 | RUBY
335 | end
336 |
337 | #----------------------------------------------------------------------------
338 | # User controller and views
339 | #----------------------------------------------------------------------------
340 | puts "generating controller and views to display users"
341 | generate(:controller, "users index show")
342 | gsub_file 'config/routes.rb', /get \"users\/index\"/, ''
343 | gsub_file 'config/routes.rb', /get \"users\/show\"/, ''
344 |
345 | inject_into_file "app/controllers/users_controller.rb", :after => "def index\n" do
346 | <<-RUBY
347 | @users = User.all
348 | RUBY
349 | end
350 |
351 | inject_into_file "app/controllers/users_controller.rb", :after => "def show\n" do
352 | <<-RUBY
353 | @user = User.find(params[:id])
354 | RUBY
355 | end
356 |
357 | if haml_flag
358 | run 'rm app/views/users/show.html.haml'
359 | # we have to use single-quote-style-heredoc to avoid interpolation
360 | create_file 'app/views/users/show.html.haml' do <<-'FILE'
361 | %h1= @user.name
362 | %p
363 | Email: #{@user.email}
364 | = link_to 'Edit', edit_user_registration_path
365 | |
366 | \#{link_to 'List of Users', users_path}
367 | %h3
368 | = @user.name
369 | Subdomains
370 | %table
371 | - @user.subdomains.each do |subdomain|
372 | %tr
373 | %td= link_to subdomain.name, subdomain
374 | %td= link_to 'Edit', edit_subdomain_path(subdomain)
375 | %td= link_to 'Destroy', subdomain, :confirm => 'Are you sure?', :method => :delete
376 | %td= link_to "Visit #{root_url(:subdomain => subdomain.name)}", root_url(:subdomain => subdomain.name)
377 | %br/
378 | = link_to "Add New Subdomain", new_user_subdomain_path(@user)
379 | FILE
380 | end
381 | else
382 | run 'rm app/views/users/show.html.erb'
383 | # we have to use single-quote-style-heredoc to avoid interpolation
384 | create_file 'app/views/users/show.html.erb' do <<-'FILE'
385 | <%= @user.name %>
386 | Email: <%= @user.email %>
387 | <%= link_to 'Edit', edit_user_registration_path %> |
388 | <%= link_to 'List of Users', users_path %>
389 | <%= @user.name %>'s Subdomains
390 |
391 | <% @user.subdomains.each do |subdomain| %>
392 |
393 | <%= link_to subdomain.name, subdomain %> |
394 | <%= link_to 'Edit', edit_subdomain_path(subdomain) %> |
395 | <%= link_to 'Destroy', subdomain, :confirm => 'Are you sure?', :method => :delete %> |
396 | <%= link_to "Visit #{root_url(:subdomain => subdomain.name)}", root_url(:subdomain => subdomain.name) %> |
397 |
398 | <% end %>
399 |
400 |
401 | <%= link_to "Add New Subdomain", new_user_subdomain_path(@user) %>
402 | FILE
403 | end
404 | end
405 |
406 | if haml_flag
407 | run 'rm app/views/users/index.html.haml'
408 | # we have to use single-quote-style-heredoc to avoid interpolation
409 | create_file 'app/views/users/index.html.haml' do <<-'FILE'
410 | %h1 Users
411 | %table
412 | - @users.each do |user|
413 | %tr
414 | %td= link_to user.name, user
415 | FILE
416 | end
417 | else
418 | run 'rm app/views/users/index.html.erb'
419 | create_file 'app/views/users/index.html.erb' do <<-FILE
420 | Users
421 |
422 | <% @users.each do |user| %>
423 |
424 | <%= link_to user.name, user %> |
425 |
426 | <% end %>
427 |
428 | FILE
429 | end
430 | end
431 |
432 | #----------------------------------------------------------------------------
433 | # Devise navigation
434 | #----------------------------------------------------------------------------
435 | if haml_flag
436 | create_file "app/views/devise/menu/_login_items.html.haml" do <<-'FILE'
437 | - if user_signed_in?
438 | %li
439 | = link_to('Logout', destroy_user_session_path)
440 | - else
441 | %li
442 | = link_to('Login', new_user_session_path)
443 | %li
444 | User:
445 | - if current_user
446 | = current_user.name
447 | - else
448 | (not logged in)
449 | FILE
450 | end
451 | else
452 | create_file "app/views/devise/menu/_login_items.html.erb" do <<-FILE
453 | <% if user_signed_in? %>
454 |
455 | <%= link_to('Logout', destroy_user_session_path) %>
456 |
457 | <% else %>
458 |
459 | <%= link_to('Login', new_user_session_path) %>
460 |
461 | <% end %>
462 |
463 | User:
464 | <% if current_user %>
465 | <%= current_user.name %>
466 | <% else %>
467 | (not logged in)
468 | <% end %>
469 |
470 | FILE
471 | end
472 | end
473 |
474 | if haml_flag
475 | create_file "app/views/devise/menu/_registration_items.html.haml" do <<-'FILE'
476 | - if user_signed_in?
477 | %li
478 | = link_to('Edit account', edit_user_registration_path)
479 | - else
480 | %li
481 | = link_to('Sign up', new_user_registration_path)
482 | FILE
483 | end
484 | else
485 | create_file "app/views/devise/menu/_registration_items.html.erb" do <<-FILE
486 | <% if user_signed_in? %>
487 |
488 | <%= link_to('Edit account', edit_user_registration_path) %>
489 |
490 | <% else %>
491 |
492 | <%= link_to('Sign up', new_user_registration_path) %>
493 |
494 | <% end %>
495 | FILE
496 | end
497 | end
498 |
499 | #----------------------------------------------------------------------------
500 | # Generate Application Layout
501 | #----------------------------------------------------------------------------
502 | if haml_flag
503 | run 'rm app/views/layouts/application.html.erb'
504 | create_file 'app/views/layouts/application.html.haml' do <<-FILE
505 | !!!
506 | %html
507 | %head
508 | %title Testapp
509 | = stylesheet_link_tag :all
510 | = javascript_include_tag :defaults
511 | = csrf_meta_tag
512 | %body
513 | %ul.hmenu
514 | = render 'devise/menu/registration_items'
515 | = render 'devise/menu/login_items'
516 | %p{:style => "color: green"}= notice
517 | %p{:style => "color: red"}= alert
518 | = yield
519 | FILE
520 | end
521 | else
522 | inject_into_file 'app/views/layouts/application.html.erb', :after => "\n" do
523 | <<-RUBY
524 |
528 | <%= notice %>
529 | <%= alert %>
530 | RUBY
531 | end
532 | end
533 |
534 | #----------------------------------------------------------------------------
535 | # Add Stylesheets
536 | #----------------------------------------------------------------------------
537 | create_file 'public/stylesheets/application.css' do <<-FILE
538 | ul.hmenu {
539 | list-style: none;
540 | margin: 0 0 2em;
541 | padding: 0;
542 | }
543 |
544 | ul.hmenu li {
545 | display: inline;
546 | }
547 | FILE
548 | end
549 |
550 | #----------------------------------------------------------------------------
551 | # Home Controller and View
552 | #----------------------------------------------------------------------------
553 | puts "create a home controller and view"
554 | generate(:controller, "home index")
555 | gsub_file 'config/routes.rb', /get \"home\/index\"/, ''
556 |
557 | if haml_flag
558 | run 'rm app/views/home/index.html.haml'
559 | # we have to use single-quote-style-heredoc to avoid interpolation
560 | create_file 'app/views/home/index.html.haml' do <<-'FILE'
561 | %h1 Rails3-Subdomain-Devise
562 | %p= link_to "View List of Users", users_path
563 | FILE
564 | end
565 | else
566 | run 'rm app/views/home/index.html.erb'
567 | create_file 'app/views/home/index.html.erb' do <<-FILE
568 | Rails3-Subdomain-Devise
569 | <%= link_to "View List of Users", users_path %>
570 | FILE
571 | end
572 | end
573 |
574 | #----------------------------------------------------------------------------
575 | # Subdomains Controller and View
576 | #----------------------------------------------------------------------------
577 | puts "create a controller and views to manage subdomains"
578 | generate(:scaffold_controller, "Subdomains")
579 | run 'rm app/controllers/subdomains_controller.rb'
580 | create_file 'app/controllers/subdomains_controller.rb' do <<-FILE
581 | class SubdomainsController < ApplicationController
582 | before_filter :authenticate_user!, :except => [:index, :show]
583 | before_filter :find_user, :except => [:index, :show]
584 | respond_to :html
585 |
586 | def index
587 | @subdomains = Subdomain.all
588 | respond_with(@subdomains)
589 | end
590 |
591 | def show
592 | @subdomain = Subdomain.find(params[:id])
593 | respond_with(@subdomain)
594 | end
595 |
596 | def new
597 | @subdomain = Subdomain.new(:user => @user)
598 | respond_with(@subdomain)
599 | end
600 |
601 | def create
602 | @subdomain = Subdomain.new(params[:subdomain])
603 | if @subdomain.save
604 | flash[:notice] = "Successfully created subdomain."
605 | end
606 | redirect_to @user
607 | end
608 |
609 | def edit
610 | @subdomain = Subdomain.find(params[:id])
611 | respond_with(@subdomain)
612 | end
613 |
614 | def update
615 | @subdomain = Subdomain.find(params[:id])
616 | if @subdomain.update_attributes(params[:subdomain])
617 | flash[:notice] = "Successfully updated subdomain."
618 | end
619 | respond_with(@subdomain)
620 | end
621 |
622 | def destroy
623 | @subdomain = Subdomain.find(params[:id])
624 | @subdomain.destroy
625 | flash[:notice] = "Successfully destroyed subdomain."
626 | redirect_to @user
627 | end
628 |
629 | protected
630 |
631 | def find_user
632 | if params[:user_id]
633 | @user = User.find(params[:user_id])
634 | else
635 | @subdomain = Subdomain.find(params[:id])
636 | @user = @subdomain.user
637 | end
638 | unless current_user == @user
639 | redirect_to @user, :alert => "Are you logged in properly? You are not allowed to create or change someone else's subdomain."
640 | end
641 | end
642 |
643 | end
644 | FILE
645 | end
646 |
647 | if haml_flag
648 | run 'rm app/views/subdomains/_form.html.haml'
649 | # we have to use single-quote-style-heredoc to avoid interpolation
650 | create_file 'app/views/subdomains/_form.html.haml' do <<-'FILE'
651 | - if @subdomain.errors.any?
652 | #error_explanation
653 | %h2
654 | = pluralize(@subdomain.errors.count, "error")
655 | prohibited this subdomain from being saved:
656 | %ul
657 | - @subdomain.errors.full_messages.each do |msg|
658 | %li= msg
659 | = fields_for @subdomain do |f|
660 | %div
661 | = f.label :name
662 | = f.text_field :name
663 | = f.hidden_field (:user_id, :value => @subdomain.user_id)
664 | %br/
665 | .actions
666 | = f.submit
667 | FILE
668 | end
669 | else
670 | run 'rm app/views/subdomains/_form.html.erb'
671 | create_file 'app/views/subdomains/_form.html.erb' do <<-FILE
672 | <% if @subdomain.errors.any? %>
673 |
674 |
<%= pluralize(@subdomain.errors.count, "error") %> prohibited this subdomain from being saved:
675 |
676 | <% @subdomain.errors.full_messages.each do |msg| %>
677 | - <%= msg %>
678 | <% end %>
679 |
680 |
681 | <% end %>
682 | <%= fields_for @subdomain do |f| %>
683 |
684 | <%= f.label :name %>
685 | <%= f.text_field :name %>
686 | <%= f.hidden_field (:user_id, :value => @subdomain.user_id) %>
687 |
688 |
689 |
690 | <%= f.submit %>
691 |
692 | <% end %>
693 | FILE
694 | end
695 | end
696 |
697 | if haml_flag
698 | run 'rm app/views/subdomains/edit.html.haml'
699 | # we have to use single-quote-style-heredoc to avoid interpolation
700 | create_file 'app/views/subdomains/edit.html.haml' do <<-'FILE'
701 | %h1 Editing subdomain
702 | = form_for(@subdomain) do |f|
703 | = render 'form'
704 | = link_to 'Show', @subdomain
705 | |
706 | \#{link_to @subdomain.user.name, user_path(@subdomain.user)}
707 | FILE
708 | end
709 | else
710 | run 'rm app/views/subdomains/edit.html.erb'
711 | create_file 'app/views/subdomains/edit.html.erb' do <<-FILE
712 | Editing subdomain
713 | <%= form_for(@subdomain) do |f| %>
714 | <%= render 'form' %>
715 | <% end %><%= link_to 'Show', @subdomain %> |
716 | <%= link_to @subdomain.user.name, user_path(@subdomain.user) %>
717 | FILE
718 | end
719 | end
720 |
721 | if haml_flag
722 | run 'rm app/views/subdomains/index.html.haml'
723 | # we have to use single-quote-style-heredoc to avoid interpolation
724 | create_file 'app/views/subdomains/index.html.haml' do <<-'FILE'
725 | %h1 Subdomains
726 | %table
727 | - @subdomains.each do |subdomain|
728 | %tr
729 | %td= link_to subdomain.name, subdomain
730 | %td
731 | (belongs to #{link_to subdomain.user.name, user_url(subdomain.user)})
732 | %td= link_to 'Edit', edit_subdomain_path(subdomain)
733 | %td= link_to 'Destroy', subdomain, :confirm => 'Are you sure?', :method => :delete
734 | FILE
735 | end
736 | else
737 | run 'rm app/views/subdomains/index.html.erb'
738 | create_file 'app/views/subdomains/index.html.erb' do <<-FILE
739 | Subdomains
740 |
741 | <% @subdomains.each do |subdomain| %>
742 |
743 | <%= link_to subdomain.name, subdomain %> |
744 | (belongs to <%= link_to subdomain.user.name, user_url(subdomain.user) %>) |
745 | <%= link_to 'Edit', edit_subdomain_path(subdomain) %> |
746 | <%= link_to 'Destroy', subdomain, :confirm => 'Are you sure?', :method => :delete %> |
747 |
748 | <% end %>
749 |
750 | FILE
751 | end
752 | end
753 |
754 | if haml_flag
755 | run 'rm app/views/subdomains/new.html.haml'
756 | # we have to use single-quote-style-heredoc to avoid interpolation
757 | create_file 'app/views/subdomains/new.html.haml' do <<-'FILE'
758 | %h1 New subdomain
759 | = form_for([@user, @subdomain]) do |f|
760 | = render 'form'
761 | = link_to @subdomain.user.name, user_path(@subdomain.user)
762 | FILE
763 | end
764 | else
765 | run 'rm app/views/subdomains/new.html.erb'
766 | create_file 'app/views/subdomains/new.html.erb' do <<-FILE
767 | New subdomain
768 | <%= form_for([@user, @subdomain]) do |f| %>
769 | <%= render 'form' %>
770 | <% end %>
771 | <%= link_to @subdomain.user.name, user_path(@subdomain.user) %>
772 | FILE
773 | end
774 | end
775 |
776 | if haml_flag
777 | run 'rm app/views/subdomains/show.html.haml'
778 | # we have to use single-quote-style-heredoc to avoid interpolation
779 | create_file 'app/views/subdomains/show.html.haml' do <<-'FILE'
780 | %h1= @subdomain.name
781 | %p
782 | Belongs to: #{link_to @subdomain.user.name, user_url(@subdomain.user)}
783 | = link_to 'Edit', edit_subdomain_path(@subdomain)
784 | FILE
785 | end
786 | else
787 | run 'rm app/views/subdomains/show.html.erb'
788 | create_file 'app/views/subdomains/show.html.erb' do <<-FILE
789 | <%= @subdomain.name %>
790 | Belongs to: <%= link_to @subdomain.user.name, user_url(@subdomain.user) %>
791 | <%= link_to 'Edit', edit_subdomain_path(@subdomain) %>
792 | FILE
793 | end
794 | end
795 |
796 | #----------------------------------------------------------------------------
797 | # Sites Controller and View
798 | #----------------------------------------------------------------------------
799 | puts "create a controller and views to display subdomain sites"
800 | generate(:controller, "Sites show")
801 | gsub_file 'config/routes.rb', /get \"sites\/show\"/, ''
802 | inject_into_file "app/controllers/sites_controller.rb", :after => "ApplicationController\n" do
803 | <<-RUBY
804 | skip_before_filter :limit_subdomain_access
805 | RUBY
806 | end
807 | if mongoid_flag
808 | inject_into_file "app/controllers/sites_controller.rb", :after => "def show\n" do
809 | <<-RUBY
810 | # this is not quite right, needs investigation, should be "Site" not "Subdomain"
811 | @site = Subdomain.first(:conditions => { :name => request.subdomain })
812 | RUBY
813 | end
814 | else
815 | inject_into_file "app/controllers/sites_controller.rb", :after => "def show\n" do
816 | <<-RUBY
817 | @site = Site.find_by_name!(request.subdomain)
818 | RUBY
819 | end
820 | end
821 | if haml_flag
822 | run 'rm app/views/sites/show.html.haml'
823 | # we have to use single-quote-style-heredoc to avoid interpolation
824 | create_file 'app/views/sites/show.html.haml' do <<-'FILE'
825 | %h1
826 | Site: #{@site.name}
827 | %p
828 | Belongs to: #{link_to @site.user.name, user_url(@site.user, :subdomain => false)}
829 | %p= link_to 'Home', root_url(:subdomain => false)
830 | FILE
831 | end
832 | else
833 | run 'rm app/views/sites/show.html.erb'
834 | create_file 'app/views/sites/show.html.erb' do <<-FILE
835 | Site: <%= @site.name %>
836 | Belongs to: <%= link_to @site.user.name, user_url(@site.user, :subdomain => false) %>
837 | <%= link_to 'Home', root_url(:subdomain => false) %>
838 | FILE
839 | end
840 | end
841 |
842 | #----------------------------------------------------------------------------
843 | # URL helper
844 | #----------------------------------------------------------------------------
845 | puts "create a URL helper for navigation between sites"
846 | create_file 'app/helpers/url_helper.rb' do < false)
885 | end
886 | end
887 |
888 | end
889 | RUBY
890 | end
891 |
892 | #----------------------------------------------------------------------------
893 | # Create Routes
894 | #----------------------------------------------------------------------------
895 | puts "creating routes"
896 | inject_into_file 'config/routes.rb', :after => "devise_for :users\n" do
897 | <<-RUBY
898 | resources :users, :only => [:index, :show] do
899 | resources :subdomains, :shallow => true
900 | end
901 | match '/' => 'home#index', :constraints => { :subdomain => 'www' }
902 | match '/' => 'sites#show', :constraints => { :subdomain => /.+/ }
903 | root :to => "home#index"
904 | RUBY
905 | end
906 |
907 | puts "allow cookies to be shared across subdomains"
908 | inject_into_file 'config/initializers/session_store.rb', ":domain => :all, ", :after => ":cookie_store, "
909 |
910 | #----------------------------------------------------------------------------
911 | # Create a default user and subdomains
912 | #----------------------------------------------------------------------------
913 | if !mongoid_flag
914 | puts "create and migrate the database"
915 | run 'rake db:create'
916 | run 'rake db:migrate'
917 | end
918 |
919 | puts "creating default users and subdomains"
920 | append_file 'db/seeds.rb' do <<-FILE
921 | puts 'SETTING UP EXAMPLE USERS'
922 | user1 = User.create! :name => 'First User', :email => 'user@test.com', :password => 'please', :password_confirmation => 'please'
923 | puts 'New user created: ' << user1.name
924 | user2 = User.create! :name => 'Other User', :email => 'otheruser@test.com', :password => 'please', :password_confirmation => 'please'
925 | puts 'New user created: ' << user2.name
926 | puts 'SETTING UP EXAMPLE SUBDOMAINS'
927 | subdomain1 = Subdomain.create! :name => 'foo'
928 | puts 'Created subdomain: ' << subdomain1.name
929 | subdomain2 = Subdomain.create! :name => 'bar'
930 | puts 'Created subdomain: ' << subdomain2.name
931 | user1.subdomains << subdomain1
932 | user1.save
933 | user2.subdomains << subdomain2
934 | user2.save
935 | FILE
936 | end
937 | run 'rake db:seed'
938 |
939 | #----------------------------------------------------------------------------
940 | # Finish up
941 | #----------------------------------------------------------------------------
942 | puts "checking everything into git..."
943 | git :add => '.'
944 | git :commit => "-m 'modified Rails app to use subdomains with Devise'"
945 |
946 | puts "Done setting up your Rails app using subdomains with Devise."
947 |
--------------------------------------------------------------------------------