Skip to content

Commit

Permalink
Cleaning up README to be more compatible with the latest stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 5, 2010
1 parent 543fe07 commit a789f08
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 71 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rdoc
@@ -1,3 +1,9 @@
* enhancements
* Warden 0.8.0 compatibility

* deprecation
* Removed DeviseMailer.sender

== 0.7.5

* enhancements
Expand Down
51 changes: 21 additions & 30 deletions README.rdoc
Expand Up @@ -7,22 +7,25 @@ Devise is a flexible authentication solution for Rails based on Warden. It:
* Allows you to have multiple roles (or models/scopes) signed in at the same time;
* Is based on a modularity concept: use just what you really need.

Right now it's composed of seven mainly modules:
Right now it's composed of six modules included by default when you invoke "devise :all" in your models:

* Authenticatable: responsible for encrypting password and validating authenticity of a user while signing in.
* Confirmable: responsible for verifying whether an account is already confirmed to sign in, and to send emails with confirmation instructions.
* Recoverable: takes care of reseting the user password and send reset instructions.
* Rememberable: manages generating and clearing token for remember the user from a saved cookie.
* Activatable: if you need to activate accounts by other means, which are not through confirmation, use this module.
* Timeoutable: expires sessions without activity in a certain period of time.
* Trackable: tracks sign in count, timestamps and ip.
* Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.

And it also includes the optional modules:

* Activatable: if you need to activate accounts by other means, which are not through confirmation, use this module.
* Timeoutable: expires sessions without activity in a certain period of time.

There's an example application using Devise at http://github.com/plataformatec/devise_example .

== Dependencies

Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Please ensure you have it installed in order to use devise (see instalation below).
Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Please ensure you have it installed in order to use devise (see installation below).

== Installation

Expand Down Expand Up @@ -53,7 +56,7 @@ And you're ready to go. The generator will install an initializer which describe

This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration. You can also check out the *Generators* section below to help you start.

Devise must be set up within the model (or models) you want to use, and devise routes must be created inside your routes.rb file.
Devise must be set up within the model (or models) you want to use, and devise routes must be created inside your config/routes.rb file.

We're assuming here you want a User model. First of all you have to setup a migration with the following fields:

Expand All @@ -62,6 +65,7 @@ We're assuming here you want a User model. First of all you have to setup a migr
t.confirmable
t.recoverable
t.rememberable
t.trackable
t.timestamps
end

Expand All @@ -71,35 +75,21 @@ You may also want to add some indexes to improve performance:
add_index :your_table, :confirmation_token # for confirmable
add_index :your_table, :reset_password_token # for recoverable

Now let's setup a User model adding the devise line to have your authentication working:
Now let's setup a User model adding the devise line:

class User < ActiveRecord::Base
devise :authenticatable
devise :all
end

This line adds devise authenticatable inside your User class. Devise don't rely on _attr_accessible_ or _attr_protected_ inside its modules, so be sure to setup what attributes are accessible or protected in your model.

You could also include the other devise modules as below:

# Include only authenticatable stuff
devise :authenticatable

# Include authenticatable + confirmable
devise :authenticatable, :confirmable

# Include authenticatable + recoverable + rememberable
devise :authenticatable, :recoverable, :rememberable

# Include authenticatable + timeoutable
devise :authenticatable, :timeoutable
This will include the six default modules outlined at the beginning. You can exclude and remove any module at will:

# Include all of them
devise :all
# Include timeout configuration
devise :all, :timeoutable

# Include all except recoverable
devise :all, :except => :recoverable
# Remove validations
devise :all, :except => :validatable

Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable.
Remember that Devise don't rely on _attr_accessible_ or _attr_protected_ inside its modules, so be sure to setup what attributes are accessible or protected in your model.

== Model configuration

Expand Down Expand Up @@ -151,19 +141,20 @@ Finally, if you are using confirmable or recoverable, you also need to setup def

== Views

By default devise will use the same views for all scopes/roles you have. But what if you need so different views to each of them? Devise also has an easy way to accomplish it: just setup :scoped_views to true inside your devise config file, and you will be able to have views based on scope like 'sessions/users/new' and 'sessions/admin/new'. If no view is found within the scope, Devise will fallback to the default view.
By default devise will use the same views for all scopes/roles you have. But what if you need so different views to each of them? Devise also has an easy way to accomplish it: just setup config,scoped_views to true inside your devise config file, and you will be able to have views based on scope like 'sessions/users/new' and 'sessions/admin/new'. If no view is found within the scope, Devise will fallback to the default view.

== Tidying up

Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps:
Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with just authentication, trackable and timeoutable stuff and none of confirmation or password recovery. Just follow the same steps:

# Create a migration with the required fields
create_table :admins do |t|
t.authenticatable
t.trackable
end

# Inside your Admin model
devise :authenticatable, :validatable
devise :authenticatable, :trackable, :timeoutable

# Inside your routes
map.devise_for :admin
Expand Down
13 changes: 1 addition & 12 deletions app/models/devise_mailer.rb
@@ -1,16 +1,5 @@
class DeviseMailer < ::ActionMailer::Base

# Sets who is sending the e-mail
def self.sender=(value)
@@sender = value
end

# Reads who is sending the e-mail
def self.sender
@@sender
end
self.sender = nil

# Deliver confirmation instructions when the user is created or its email is
# updated, and also when confirmation is manually requested
def confirmation_instructions(record)
Expand All @@ -30,7 +19,7 @@ def setup_mail(record, key)
raise "Invalid devise resource #{record}" unless mapping

subject translate(mapping, key)
from self.class.sender
from Devise.mailer_sender
recipients record.email
sent_on Time.now
content_type 'text/html'
Expand Down
22 changes: 11 additions & 11 deletions generators/devise/templates/README
@@ -1,22 +1,22 @@

================================================================================
===============================================================================

Some setup you must do manually if you haven't yet:

1. Setup defaut url options for your specific environment. Here is an example of development environment:
1. Run devise install generator:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ruby script/generate devise_install

It's a Rails required configuration. In production it must be the actual host your application is deployed to.
2. Setup defaut url options for your specific environment. Here is an
example of development environment:

2. Setup default sender for mails. In config/environment.rb:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

DeviseMailer.sender = "test@example.com"
This is a required Rails configuration. In production is must be the
actual host of your application

You can also configure this value by running script/generate devise_install and setting config.mailer_sender,
3. Ensure you have defined root_url to *something* in your config/routes.rb:

3. Ensure you have defined root_url to *something* in your config/routes.rb:
map.root :controller => 'home'

map.root :controller => 'home'

================================================================================
===============================================================================
10 changes: 4 additions & 6 deletions lib/devise.rb
Expand Up @@ -107,19 +107,17 @@ module Orm
mattr_accessor :default_scope
@@default_scope = nil

# Address which sends Devise e-mails
mattr_accessor :mailer_sender
@@mailer_sender

class << self
# Default way to setup Devise. Run script/generate devise_install to create
# a fresh initializer with all configuration values.
def setup
yield self
end

# Sets the sender in DeviseMailer.
def mailer_sender=(value)
DeviseMailer.sender = value
end
alias :sender= :mailer_sender=

# Sets warden configuration using a block that will be invoked on warden
# initialization.
#
Expand Down
4 changes: 3 additions & 1 deletion lib/devise/models/timeoutable.rb
Expand Up @@ -8,11 +8,13 @@ module Models
# will be asked for credentials again, it means, he/she will be redirected
# to the sign in page.
#
# In order to use timeoutable, you need to use trackable. So don't forget
# to invoke trackable in your migrations.
#
# Configuration:
#
# timeout: the time you want to timeout the user session without activity.
module Timeoutable

def self.included(base)
base.extend ClassMethods
end
Expand Down
9 changes: 0 additions & 9 deletions test/devise_test.rb
Expand Up @@ -7,15 +7,6 @@ def self.clean_warden_config!
end

class DeviseTest < ActiveSupport::TestCase

test 'DeviseMailer.sender can be configured through Devise' do
swap DeviseMailer, :sender => "foo@bar" do
assert_equal "foo@bar", DeviseMailer.sender
Devise.mailer_sender = "bar@foo"
assert_equal "bar@foo", DeviseMailer.sender
end
end

test 'model options can be configured through Devise' do
swap Devise, :confirm_within => 113, :pepper => "foo" do
assert_equal 113, Devise.confirm_within
Expand Down
2 changes: 1 addition & 1 deletion test/mailers/confirmation_instructions_test.rb
Expand Up @@ -4,7 +4,7 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase

def setup
setup_mailer
DeviseMailer.sender = 'test@example.com'
Devise.mailer_sender = 'test@example.com'
end

def user
Expand Down
2 changes: 1 addition & 1 deletion test/mailers/reset_password_instructions_test.rb
Expand Up @@ -4,7 +4,7 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase

def setup
setup_mailer
DeviseMailer.sender = 'test@example.com'
Devise.mailer_sender = 'test@example.com'
end

def user
Expand Down

0 comments on commit a789f08

Please sign in to comment.