README.md in devise-4.1.1 vs README.md in devise-4.2.0

- old
+ new

@@ -87,40 +87,43 @@ Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :smiley: ## Getting started -Devise 4.0 works with Rails 4.2 onwards. You can add it to your Gemfile with: +Devise 4.0 works with Rails 4.1 onwards. You can add it to your Gemfile with: ```ruby gem 'devise' ``` Run the bundle command to install it. -After you install Devise and add it to your Gemfile, you need to run the generator: +Next, you need to run the generator: ```console -rails generate devise:install +$ rails generate devise:install ``` -The generator will install an initializer which describes ALL of Devise's configuration options. It is *imperative* that you take a look at it. When you are done, you are ready to add Devise to any of your models using the generator: +At this point, a number of instructions will appear in the console. Among these instructions, you'll need to set up the default URL options for the Devise mailer in each environment. Here is a possible configuration for `config/environments/development.rb`: -```console -rails generate devise MODEL +```ruby +config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } ``` -Replace MODEL with the class name used for the application’s users (it’s frequently `User` but could also be `Admin`). This will create a model (if one does not exist) and configure it with the default Devise modules. The generator also configures your `config/routes.rb` file to point to the Devise controller. +The generator will install an initializer which describes ALL of Devise's configuration options. It is *imperative* that you take a look at it. When you are done, you are ready to add Devise to any of your models using the generator. -Next, check the MODEL for any additional configuration options you might want to add, such as confirmable or lockable. If you add an option, be sure to inspect the migration file (created by the generator if your ORM supports them) and uncomment the appropriate section. For example, if you add the confirmable option in the model, you'll need to uncomment the Confirmable section in the migration. Then run `rake db:migrate` -Next, you need to set up the default URL options for the Devise mailer in each environment. Here is a possible configuration for `config/environments/development.rb`: +In the following command you will replace `MODEL` with the class name used for the application’s users (it’s frequently `User` but could also be `Admin`). This will create a model (if one does not exist) and configure it with the default Devise modules. The generator also configures your `config/routes.rb` file to point to the Devise controller. -```ruby -config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } +```console +$ rails generate devise MODEL ``` +Next, check the MODEL for any additional configuration options you might want to add, such as confirmable or lockable. If you add an option, be sure to inspect the migration file (created by the generator if your ORM supports them) and uncomment the appropriate section. For example, if you add the confirmable option in the model, you'll need to uncomment the Confirmable section in the migration. + +Then run `rake db:migrate` + You should restart your application after changing Devise's configuration options. Otherwise, you will run into strange errors, for example, users being unable to login and route helpers being undefined. ### Controller filters and helpers Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_action (assuming your devise model is 'User'): @@ -152,11 +155,11 @@ ``` After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect to. For instance, when using a `:user` resource, the `user_root_path` will be used if it exists; otherwise, the default `root_path` will be used. This means that you need to set the root inside your routes: ```ruby -root to: "home#index" +root to: 'home#index' ``` You can also override `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks. Notice that if your Devise model is called `Member` instead of `User`, for example, then the helpers available are: @@ -268,36 +271,36 @@ We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it. Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after some time you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application: ```console -rails generate devise:views +$ rails generate devise:views ``` If you have more than one Devise model in your application (such as `User` and `Admin`), you will notice that Devise uses the same views for all models. Fortunately, Devise offers an easy way to customize views. All you need to do is set `config.scoped_views = true` inside the `config/initializers/devise.rb` file. After doing so, you will be able to have views based on the role like `users/sessions/new` and `admins/sessions/new`. If no view is found within the scope, Devise will use the default view at `devise/sessions/new`. You can also use the generator to generate scoped views: ```console -rails generate devise:views users +$ rails generate devise:views users ``` If you would like to generate only a few sets of views, like the ones for the `registerable` and `confirmable` module, you can pass a list of modules to the generator with the `-v` flag. ```console -rails generate devise:views -v registrations confirmations +$ rails generate devise:views -v registrations confirmations ``` ### Configuring controllers If the customization at the views level is not enough, you can customize each controller by following these steps: 1. Create your custom controllers using the generator which requires a scope: ```console - rails generate devise:controllers [scope] + $ rails generate devise:controllers [scope] ``` If you specify `users` as the scope, controllers will be created in `app/controllers/users/`. And the sessions controller will look like this: @@ -312,11 +315,11 @@ ``` 2. Tell the router to use this controller: ```ruby - devise_for :users, controllers: { sessions: "users/sessions" } + devise_for :users, controllers: { sessions: 'users/sessions' } ``` 3. Copy the views from `devise/sessions` to `users/sessions`. Since the controller was changed, it won't use the default views located in `devise/sessions`. 4. Finally, change or extend the desired controller actions. @@ -350,20 +353,20 @@ ### Configuring routes Devise also ships with default routes. If you need to customize them, you should probably be able to do it through the devise_for method. It accepts several options like :class_name, :path_prefix and so on, including the possibility to change path names for I18n: ```ruby -devise_for :users, path: "auth", path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' } +devise_for :users, path: 'auth', path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' } ``` Be sure to check `devise_for` [documentation](http://www.rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper%3Adevise_for) for details. If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is create your routes normally and wrap them in a `devise_scope` block in the router: ```ruby devise_scope :user do - get "sign_in", to: "devise/sessions#new" + get 'sign_in', to: 'devise/sessions#new' end ``` This way, you tell Devise to use the scope `:user` when "/sign_in" is accessed. Notice `devise_scope` is also aliased as `as` in your router. @@ -409,48 +412,97 @@ Caution: Devise Controllers inherit from ApplicationController. If your app uses multiple locales, you should be sure to set I18n.locale in ApplicationController. ### Test helpers -Devise includes some test helpers for functional specs. In order to use them, you need to include Devise in your functional tests by adding the following to the bottom of your `test/test_helper.rb` file (make sure you place it out of scope of `ActiveSupport::TestCase` which is the default class inside of `test/test_helper.rb`): +Devise includes some test helpers for controller and integration tests. +In order to use them, you need to include the respective module in your test +cases/specs. +### Controller tests + +Controller tests require that you include `Devise::Test::ControllerHelpers` on +your test case or its parent `ActionController::TestCase` superclass. + ```ruby -class ActionController::TestCase - include Devise::TestHelpers +class PostsControllerTest < ActionController::TestCase + include Devise::Test::ControllerHelpers end ``` -If you're using RSpec, you can put the following inside a file named `spec/support/devise.rb` or in your `spec/spec_helper.rb` (or `spec/rails_helper.rb` if you are using rspec-rails): +If you're using RSpec, you can put the following inside a file named +`spec/support/devise.rb` or in your `spec/spec_helper.rb` (or +`spec/rails_helper.rb` if you are using `rspec-rails`): ```ruby RSpec.configure do |config| - config.include Devise::TestHelpers, type: :controller - config.include Devise::TestHelpers, type: :view + config.include Devise::Test::ControllerHelpers, type: :controller + config.include Devise::Test::ControllerHelpers, type: :view end ``` Just be sure that this inclusion is made *after* the `require 'rspec/rails'` directive. -Now you are ready to use the `sign_in` and `sign_out` methods. Such methods have the same signature as in controllers: +Now you are ready to use the `sign_in` and `sign_out` methods on your controller +tests: ```ruby -sign_in :user, @user # sign_in(scope, resource) -sign_in @user # sign_in(resource) +sign_in @user +sign_in @user, scope: admin +``` -sign_out :user # sign_out(scope) -sign_out @user # sign_out(resource) +If you are testing Devise internal controllers or a controller that inherits +from Devise's, you need to tell Devise which mapping should be used before a +request. This is necessary because Devise gets this information from the router, +but since controller tests do not pass through the router, it needs to be stated +explicitly. For example, if you are testing the user scope, simply use: + +```ruby +test 'GET new' do + # Mimic the router behavior of setting the Devise scope through the env. + @request.env['devise.mapping'] = Devise.mappings[:user] + + # Use the sign_in helper to sign in a fixture `User` record. + sign_in users(:alice) + + get :new + + # assert something +end ``` -There are two things that are important to keep in mind: +### Integration tests -1. These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. It is undesirable even to include `Devise::TestHelpers` during integration tests. Instead, fill in the form or explicitly set the user in session; +Integration test helpers are available by including the +`Devise::Test::IntegrationHelpers` module. -2. If you are testing Devise internal controllers or a controller that inherits from Devise's, you need to tell Devise which mapping should be used before a request. This is necessary because Devise gets this information from the router, but since functional tests do not pass through the router, it needs to be stated explicitly. For example, if you are testing the user scope, simply use: +```ruby +class PostsTests < ActionDispatch::IntegrationTest + include Devise::Test::IntegrationHelpers +end +``` - ```ruby - @request.env["devise.mapping"] = Devise.mappings[:user] - get :new - ``` +Now you can use the following `sign_in` and `sign_out` methods in your integration +tests: + +```ruby +sign_in users(:bob) +sign_in users(:bob), scope: :admin + +sign_out :user +``` + +RSpec users can include the `IntegrationHelpers` module on their `:feature` specs. + +```ruby +RSpec.configure do |config| + config.include Devise::Test::IntegrationHelpers, type: :feature +end +``` + +Unlike controller tests, integration tests do not need to supply the +`devise.mapping` `env` value, as the mapping can be inferred by the routes that +are executed in your tests. You can read more about testing your Rails 3 - Rails 4 controllers with RSpec in the wiki: * https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29