Skip to content

Commit

Permalink
Make ActionMailer and ActionController more consistent by always usin…
Browse files Browse the repository at this point in the history
…g Devise mapping names and updated README.
  • Loading branch information
josevalim committed Oct 29, 2009
1 parent 2cc07d0 commit 475da06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
21 changes: 15 additions & 6 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Now let's setup a User model adding the devise line to have your authentication
devise
end

This line adds devise authenticable automatically for you inside your User class. Devise don't rely on _attr_accessible_ or _attr_protected_ inside it's modules, so be sure to setup what attributes are accessible or protected in your model.
This line adds devise authenticable automatically for you 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:

Expand Down Expand Up @@ -107,6 +107,14 @@ In addition to :except, you can provide some options to devise call:

devise :all, :stretches => 20

* confirm_in: the time the user can access the site before being blocked because his account was not confirmed

devise :all, :confirm_in => 1.week

* remember_for: the time to store the remember me cookie in the user

devise :all, :remember_for => 2.weeks

The next step after setting up your model is to configure your routes for devise. You do this by opening up your config/routes.rb and adding:

map.devise_for :users
Expand Down Expand Up @@ -168,9 +176,10 @@ After signing in a user, confirming it's account or updating it's password, devi

map.root :controller => 'home'

You also need to setup default url options for the mailer, if you are using confirmable or recoverable. It's a Rails required configuration, and you can do this inside your specific environments. Here is an example of development environment:
You also need to setup default url options for the mailer, if you are using confirmable or recoverable. Here's is the configuration for development:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Notifier.sender = "no-reply@yourapp.com"
ActionMailer::Base.default_url_options = { :host => 'localhost:3000' }

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:

Expand Down Expand Up @@ -209,14 +218,14 @@ This is gonna copy all session, password, confirmation and notifier views to you

== I18n

Devise check for flash messages using i18n, so you're able to customize them easily. For example, to change the sign in message you should setup your locale file this way:
Devise uses flash messages with I18n with the flash keys :success and :failure. To customize your app, you can setup your locale file this way:

en:
devise:
sessions:
signed_in: 'Signed in successfully.'

You can also create distinct messages based on the resource you've configured:
You can also create distinct messages based on the resource you've configured using the singular name given in routes:

en:
devise:
Expand All @@ -226,7 +235,7 @@ You can also create distinct messages based on the resource you've configured:
admin:
signed_in: 'Hello admin!'

Devise notifier uses the same pattern to create subject messages, but it is not able to know what scope you are, he just know the record (ie user instance) that was sent to it. So you need to customize messages based on the model class name (usually the same as the resource name, if you follow basic conventions):
Devise notifier uses the same pattern to create subject messages:

en:
devise:
Expand Down
17 changes: 7 additions & 10 deletions app/models/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ def reset_password_instructions(record)

# Configure default email options
def setup_mail(record, key)
subject translate(record, key)
mapping = Devise.mappings.values.find { |m| m.to == record.class }
raise "Invalid devise resource #{record}" unless mapping

subject translate(mapping, key)
from self.class.sender
recipients record.email
sent_on Time.now
content_type 'text/html'
body underscore_name(record) => record, :resource => record
body mapping.name => record, :resource => record
end

# Setup subject namespaced by model. It means you're able to setup your
Expand All @@ -35,13 +38,7 @@ def setup_mail(record, key)
# user:
# notifier:
# confirmation_instructions: '...'
def translate(record, key)
I18n.t(:"#{underscore_name(record)}.#{key}",
:scope => [:devise, :notifier],
:default => key)
end

def underscore_name(record)
@underscore_name ||= record.class.name.underscore.to_sym
def translate(mapping, key)
I18n.t(:"#{mapping.name}.#{key}", :scope => [:devise, :notifier], :default => key)
end
end

0 comments on commit 475da06

Please sign in to comment.