mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Merge branch 'master' of git@github.com:plataformatec/devise
This commit is contained in:
commit
b5256d9765
20 changed files with 257 additions and 15 deletions
|
@ -1,20 +1,22 @@
|
|||
== 0.1.2 (development)
|
||||
== 0.2.0
|
||||
|
||||
* bug fixes
|
||||
* Fixed requiring devise strategies
|
||||
* enhancements
|
||||
* [#4] Allow option :null => true in authenticable migration
|
||||
* [#3] Remove attr_accessible calls from devise modules
|
||||
* Customizable time frame for rememberable with :remember_for config
|
||||
* Customizable time frame for confirmable with :confirm_in config
|
||||
* Generators for creating a resource and copy views
|
||||
|
||||
* optimize
|
||||
* Do not load hooks or strategies if they are not used
|
||||
|
||||
* enhancements
|
||||
* Allow option :null => true in authenticable migration
|
||||
* Customizable time frame for rememberable with :remember_for config
|
||||
* Customizable time frame for confirmable with :confirm_in config
|
||||
* bug fixes
|
||||
* [#2] Fixed requiring devise strategies
|
||||
|
||||
== 0.1.1
|
||||
|
||||
* bug fixes
|
||||
* Fixed requiring devise mapping
|
||||
* [#1] Fixed requiring devise mapping
|
||||
|
||||
== 0.1.0
|
||||
|
||||
|
|
18
README.rdoc
18
README.rdoc
|
@ -44,6 +44,8 @@ And you're ready to go.
|
|||
|
||||
== Basic Usage
|
||||
|
||||
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 setted up within the model (or models) you want to use, and devise routes must be created inside your 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:
|
||||
|
@ -168,7 +170,7 @@ After signing in a user, confirming it's account or updating it's password, devi
|
|||
|
||||
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:
|
||||
|
||||
config.action_mailer.default_url_options = {:host => 'localhost:3000'}
|
||||
config.action_mailer.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:
|
||||
|
||||
|
@ -191,6 +193,20 @@ Devise let's you setup as many roles as you want, so let's say you already have
|
|||
current_admin
|
||||
admin_session
|
||||
|
||||
== Generators
|
||||
|
||||
Devise comes with some generators to help you start:
|
||||
|
||||
script/generate devise Model
|
||||
|
||||
Will generate a model, configured with all devise modules, and add attr_accessible for default fields, so you can setup more accessible attributes later. The generator will also create the migration and configure your route for devise.
|
||||
|
||||
You can also copy devise views to your application, being able to modify them based on your needs. To do it so, run the following command:
|
||||
|
||||
script/generate devise_views
|
||||
|
||||
This is gonna copy all session, password, confirmation and notifier views to your app/views folder.
|
||||
|
||||
== 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:
|
||||
|
|
2
Rakefile
2
Rakefile
|
@ -35,7 +35,7 @@ begin
|
|||
s.homepage = "http://github.com/plataformatec/devise"
|
||||
s.description = "Flexible authentication solution for Rails with Warden"
|
||||
s.authors = ['José Valim', 'Carlos Antônio']
|
||||
s.files = FileList["[A-Z]*", "{app,config,lib}/**/*", "init.rb"]
|
||||
s.files = FileList["[A-Z]*", "{app,config,generators,lib}/**/*", "init.rb"]
|
||||
s.add_dependency("warden", "~> 0.5.1")
|
||||
end
|
||||
|
||||
|
|
2
TODO
2
TODO
|
@ -1,8 +1,8 @@
|
|||
* Create generators
|
||||
* Devise::Timeoutable
|
||||
* Devise::TestHelper
|
||||
* Use request_ip in session cookies
|
||||
* Devise::BruteForceProtection
|
||||
* Devise::MagicColumns
|
||||
* Improve Generators to pass modules as arguments
|
||||
* Different cryptography providers
|
||||
* Devise::Invitable
|
||||
|
|
5
generators/devise/USAGE
Normal file
5
generators/devise/USAGE
Normal file
|
@ -0,0 +1,5 @@
|
|||
To create a devise resource user:
|
||||
|
||||
script/generate devise User
|
||||
|
||||
This will generate a model named User, a route map for devise called :users, and a migration file for table :users with all devise modules.
|
25
generators/devise/devise_generator.rb
Normal file
25
generators/devise/devise_generator.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + "/lib/route_devise.rb")
|
||||
|
||||
class DeviseGenerator < Rails::Generator::NamedBase
|
||||
|
||||
def manifest
|
||||
record do |m|
|
||||
# Check for class naming collisions.
|
||||
m.class_collisions(class_name)
|
||||
|
||||
# Model
|
||||
m.directory(File.join('app', 'models', class_path))
|
||||
m.template 'model.rb', File.join('app', 'models', "#{file_path}.rb")
|
||||
|
||||
# Migration
|
||||
m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "devise_create_#{table_name}"
|
||||
|
||||
# Routing
|
||||
m.route_devise table_name
|
||||
|
||||
# Readme
|
||||
m.readme "README"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
32
generators/devise/lib/route_devise.rb
Normal file
32
generators/devise/lib/route_devise.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Rails
|
||||
module Generator
|
||||
module Commands
|
||||
class Create < Base
|
||||
|
||||
# Create devise route. Based on route_resources
|
||||
def route_devise(*resources)
|
||||
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
|
||||
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
||||
|
||||
logger.route "map.devise_for #{resource_list}"
|
||||
unless options[:pretend]
|
||||
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
||||
"#{match}\n map.devise_for #{resource_list}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Destroy < RewindBase
|
||||
|
||||
# Destroy devise route. Based on route_resources
|
||||
def route_devise(*resources)
|
||||
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
|
||||
look_for = "\n map.devise_for #{resource_list}\n"
|
||||
logger.route "map.devise_for #{resource_list}"
|
||||
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
generators/devise/templates/README
Normal file
21
generators/devise/templates/README
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
================================================================================
|
||||
|
||||
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:
|
||||
|
||||
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
|
||||
|
||||
It's a Rails required configuration.
|
||||
In production it must be the actual host your application is deployed to.
|
||||
|
||||
2. Setup default sender for mails.In config/environment.rb:
|
||||
|
||||
Notifier.sender = "test@example.com"
|
||||
|
||||
3. Ensure you have defined root_url to *something* in your config/routes.rb:
|
||||
|
||||
map.root :controller => 'home'
|
||||
|
||||
================================================================================
|
20
generators/devise/templates/migration.rb
Normal file
20
generators/devise/templates/migration.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table(:<%= table_name %>) do |t|
|
||||
t.authenticable
|
||||
t.confirmable
|
||||
t.recoverable
|
||||
t.rememberable
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :<%= table_name %>, :email, :unique => true
|
||||
add_index :<%= table_name %>, :confirmation_token, :unique => true
|
||||
add_index :<%= table_name %>, :reset_password_token, :unique => true
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :<%= table_name %>
|
||||
end
|
||||
end
|
5
generators/devise/templates/model.rb
Normal file
5
generators/devise/templates/model.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class <%= class_name %> < ActiveRecord::Base
|
||||
devise :all
|
||||
# Setup accessible (or protected) attributes for your model
|
||||
attr_accessible :email, :password, :password_confirmation
|
||||
end
|
3
generators/devise_views/USAGE
Normal file
3
generators/devise_views/USAGE
Normal file
|
@ -0,0 +1,3 @@
|
|||
To copy all session, password and confirmation views from devise to your app just run the following command:
|
||||
|
||||
script/generate devise_views
|
24
generators/devise_views/devise_views_generator.rb
Normal file
24
generators/devise_views/devise_views_generator.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
class DeviseViewsGenerator < Rails::Generator::Base
|
||||
|
||||
def manifest
|
||||
record do |m|
|
||||
views_directory = File.join('app', 'views')
|
||||
m.directory views_directory
|
||||
|
||||
{
|
||||
:sessions => [:new],
|
||||
:passwords => [:new, :edit],
|
||||
:confirmations => [:new],
|
||||
:notifier => [:confirmation_instructions, :reset_password_instructions]
|
||||
}.each do |dir, templates|
|
||||
m.directory File.join(views_directory, dir.to_s)
|
||||
|
||||
templates.each do |template|
|
||||
template_path = "#{dir}/#{template}.html.erb"
|
||||
m.file "#{template_path}", "#{views_directory}/#{template_path}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
16
generators/devise_views/templates/confirmations/new.html.erb
Normal file
16
generators/devise_views/templates/confirmations/new.html.erb
Normal file
|
@ -0,0 +1,16 @@
|
|||
<h2>Resend confirmation instructions</h2>
|
||||
|
||||
<% form_for resource_name, :url => confirmation_path(resource_name) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Resend confirmation instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to "Sign in", new_session_path(resource_name) %><br />
|
||||
|
||||
<%- if devise_mapping.recoverable? %>
|
||||
<%= link_to "Forgot password?", new_password_path(resource_name) %><br />
|
||||
<% end -%>
|
|
@ -0,0 +1,5 @@
|
|||
Welcome <%= @resource.email %>!
|
||||
|
||||
You can confirm your account through the link below:
|
||||
|
||||
<%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>
|
|
@ -0,0 +1,8 @@
|
|||
Hello <%= @resource.email %>!
|
||||
|
||||
Someone has requested a link to change your password, and you can do this through the link below.
|
||||
|
||||
<%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>
|
||||
|
||||
If you didn't request this, please ignore this email.
|
||||
Your password won't change until you access the link above and create a new one.
|
20
generators/devise_views/templates/passwords/edit.html.erb
Normal file
20
generators/devise_views/templates/passwords/edit.html.erb
Normal file
|
@ -0,0 +1,20 @@
|
|||
<h2>Change your password</h2>
|
||||
|
||||
<% form_for resource_name, :url => password_path(resource_name), :html => { :method => :put } do |f| %>
|
||||
<%= f.error_messages %>
|
||||
<%= f.hidden_field :reset_password_token %>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.submit "Change my password" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to "Sign in", new_session_path(resource_name) %><br />
|
||||
|
||||
<%- if devise_mapping.confirmable? %>
|
||||
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
|
||||
<% end -%>
|
16
generators/devise_views/templates/passwords/new.html.erb
Normal file
16
generators/devise_views/templates/passwords/new.html.erb
Normal file
|
@ -0,0 +1,16 @@
|
|||
<h2>Forgot your password?</h2>
|
||||
|
||||
<% form_for resource_name, :url => password_path(resource_name) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Send me reset password instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= link_to "Sign in", new_session_path(resource_name) %><br />
|
||||
|
||||
<%- if devise_mapping.confirmable? %>
|
||||
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
|
||||
<% end -%>
|
23
generators/devise_views/templates/sessions/new.html.erb
Normal file
23
generators/devise_views/templates/sessions/new.html.erb
Normal file
|
@ -0,0 +1,23 @@
|
|||
<h2>Sign in</h2>
|
||||
|
||||
<% form_for resource_name, :url => session_path(resource_name) do |f| -%>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<% if devise_mapping.rememberable? -%>
|
||||
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
|
||||
<% end -%>
|
||||
|
||||
<p><%= f.submit "Sign in" %></p>
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.recoverable? %>
|
||||
<%= link_to "Forgot password?", new_password_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.confirmable? %>
|
||||
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
|
||||
<% end -%>
|
|
@ -56,9 +56,10 @@ end
|
|||
#
|
||||
# 1) Include Devise::ActiveRecord and Devise::Migrations
|
||||
# 2) Load and config warden
|
||||
# 3) Add routes extensions
|
||||
# 4) Load routes definitions
|
||||
# 5) Include filters and helpers in controllers and views
|
||||
# 3) Load devise mapping structure
|
||||
# 4) Add routes extensions
|
||||
# 5) Load routes definitions
|
||||
# 6) Include filters and helpers in controllers and views
|
||||
#
|
||||
Rails.configuration.after_initialize do
|
||||
ActiveRecord::Base.extend Devise::ActiveRecord
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module Devise
|
||||
VERSION = "0.1.1".freeze
|
||||
VERSION = "0.2.0".freeze
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue