Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make ActionMailer and ActionController more consistent by always usin…

…g Devise mapping names and updated README.
  • Loading branch information...
commit 475da069abafa2033d24db0ad16b447e45b98d9c 1 parent 2cc07d0
@josevalim josevalim authored
Showing with 22 additions and 16 deletions.
  1. +15 −6 README.rdoc
  2. +7 −10 app/models/notifier.rb
View
21 README.rdoc
@@ -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:
@@ -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
@@ -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:
@@ -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:
@@ -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:
View
17 app/models/notifier.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.