devise 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

@@ -1,4 +1,16 @@
1
- == (development)
1
+ == 0.2.1
2
+
3
+ * refactor
4
+ * Clean devise_views generator to use devise existing views
5
+
6
+ * enhancements
7
+ * [#7] Create instance variables (like @user) for each devise controller
8
+ * Use Devise::Controller::Helpers only internally
9
+
10
+ * bug fix
11
+ * [#6] Fix a bug with Mongrel and Ruby 1.8.6
12
+
13
+ == 0.2.0
2
14
 
3
15
  * enhancements
4
16
  * [#4] Allow option :null => true in authenticable migration
@@ -1,8 +1,9 @@
1
1
  class ConfirmationsController < ApplicationController
2
- before_filter :is_devise_resource?
2
+ include Devise::Controllers::Helpers
3
3
 
4
4
  # GET /resource/confirmation/new
5
5
  def new
6
+ build_resource
6
7
  end
7
8
 
8
9
  # POST /resource/confirmation
@@ -1,8 +1,11 @@
1
1
  class PasswordsController < ApplicationController
2
- before_filter :is_devise_resource?, :require_no_authentication
2
+ include Devise::Controllers::Helpers
3
+
4
+ before_filter :require_no_authentication
3
5
 
4
6
  # GET /resource/password/new
5
7
  def new
8
+ build_resource
6
9
  end
7
10
 
8
11
  # POST /resource/password
@@ -1,11 +1,13 @@
1
1
  class SessionsController < ApplicationController
2
- before_filter :is_devise_resource?
2
+ include Devise::Controllers::Helpers
3
+
3
4
  before_filter :require_no_authentication, :only => [ :new, :create ]
4
5
 
5
6
  # GET /resource/sign_in
6
7
  def new
7
8
  unauthenticated! if params[:unauthenticated]
8
9
  unconfirmed! if params[:unconfirmed]
10
+ build_resource
9
11
  end
10
12
 
11
13
  # POST /resource/sign_in
@@ -15,6 +17,7 @@ class SessionsController < ApplicationController
15
17
  redirect_back_or_to home_or_root_path
16
18
  else
17
19
  unauthenticated!
20
+ build_resource
18
21
  render :new
19
22
  end
20
23
  end
@@ -1,6 +1,6 @@
1
1
  <h2>Resend confirmation instructions</h2>
2
2
 
3
- <% form_for resource_name, :url => confirmation_path(resource_name) do |f| %>
3
+ <% form_for resource, :url => confirmation_path(resource_name) do |f| %>
4
4
  <%= f.error_messages %>
5
5
 
6
6
  <p><%= f.label :email %></p>
@@ -1,6 +1,6 @@
1
1
  <h2>Change your password</h2>
2
2
 
3
- <% form_for resource_name, :url => password_path(resource_name), :html => { :method => :put } do |f| %>
3
+ <% form_for resource, :url => password_path(resource_name), :html => { :method => :put } do |f| %>
4
4
  <%= f.error_messages %>
5
5
  <%= f.hidden_field :reset_password_token %>
6
6
 
@@ -1,6 +1,6 @@
1
1
  <h2>Forgot your password?</h2>
2
2
 
3
- <% form_for resource_name, :url => password_path(resource_name) do |f| %>
3
+ <% form_for resource, :url => password_path(resource_name) do |f| %>
4
4
  <%= f.error_messages %>
5
5
 
6
6
  <p><%= f.label :email %></p>
@@ -1,6 +1,6 @@
1
1
  <h2>Sign in</h2>
2
2
 
3
- <% form_for resource_name, :url => session_path(resource_name) do |f| -%>
3
+ <% form_for resource, :url => session_path(resource_name) do |f| -%>
4
4
  <p><%= f.label :email %></p>
5
5
  <p><%= f.text_field :email %></p>
6
6
 
@@ -1,22 +1,20 @@
1
1
  class DeviseViewsGenerator < Rails::Generator::Base
2
2
 
3
+ def initialize(*args)
4
+ super
5
+ @source_root = options[:source] || File.join(spec.path, '..', '..', 'app', 'views')
6
+ end
7
+
3
8
  def manifest
4
9
  record do |m|
5
10
  views_directory = File.join('app', 'views')
6
11
  m.directory views_directory
7
12
 
8
- {
9
- :sessions => [:new],
10
- :passwords => [:new, :edit],
11
- :confirmations => [:new],
12
- :notifier => [:confirmation_instructions, :reset_password_instructions]
13
- }.each do |dir, templates|
14
- m.directory File.join(views_directory, dir.to_s)
13
+ Dir[File.join(@source_root, "**/*.erb")].each do |file|
14
+ file = file.gsub(@source_root, "")[1..-1]
15
15
 
16
- templates.each do |template|
17
- template_path = "#{dir}/#{template}.html.erb"
18
- m.file "#{template_path}", "#{views_directory}/#{template_path}"
19
- end
16
+ m.directory File.join(views_directory, File.dirname(file))
17
+ m.file file, File.join(views_directory, file)
20
18
  end
21
19
  end
22
20
  end
@@ -1,5 +1,7 @@
1
1
  module Devise
2
2
  module Controllers
3
+ # Those filters are convenience methods added to ApplicationController to
4
+ # deal with Warden.
3
5
  module Filters
4
6
 
5
7
  def self.included(base)
@@ -89,21 +91,6 @@ module Devise
89
91
  METHODS
90
92
  end
91
93
 
92
- protected
93
-
94
- # Helper for use in before_filters where no authentication is required.
95
- #
96
- # Example:
97
- # before_filter :require_no_authentication, :only => :new
98
- def require_no_authentication
99
- redirect_to root_path if warden.authenticated?(resource_name)
100
- end
101
-
102
- # Checks whether it's a devise mapped resource or not.
103
- def is_devise_resource? #:nodoc:
104
- raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
105
- end
106
-
107
94
  end
108
95
  end
109
96
  end
@@ -1,10 +1,16 @@
1
1
  module Devise
2
2
  module Controllers
3
+ # Those helpers are used only inside Devise controllers and should not be
4
+ # included in ApplicationController since they all depend on the url being
5
+ # accessed.
3
6
  module Helpers
4
7
 
5
8
  def self.included(base)
6
9
  base.class_eval do
7
10
  helper_method :resource, :resource_name, :resource_class, :devise_mapping
11
+ hide_action :resource, :resource_name, :resource_class, :devise_mapping
12
+
13
+ before_filter :is_devise_resource?
8
14
  end
9
15
  end
10
16
 
@@ -23,6 +29,11 @@ module Devise
23
29
  devise_mapping.to
24
30
  end
25
31
 
32
+ # Attempt to find the mapped route for devise based on request path
33
+ def devise_mapping
34
+ @devise_mapping ||= Devise.find_mapping_by_path(request.path)
35
+ end
36
+
26
37
  protected
27
38
 
28
39
  # Redirects to stored uri before signing in or the default path and clear
@@ -57,9 +68,9 @@ module Devise
57
68
  respond_to?(home_path, true) ? send(home_path) : root_path
58
69
  end
59
70
 
60
- # Attempt to find the mapped route for devise based on request path
61
- def devise_mapping
62
- @devise_mapping ||= Devise.find_mapping_by_path(request.path)
71
+ # Checks whether it's a devise mapped resource or not.
72
+ def is_devise_resource? #:nodoc:
73
+ raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
63
74
  end
64
75
 
65
76
  # Sets the resource creating an instance variable
@@ -67,6 +78,19 @@ module Devise
67
78
  instance_variable_set(:"@#{resource_name}", new_resource)
68
79
  end
69
80
 
81
+ # Build a devise resource
82
+ def build_resource
83
+ self.resource = resource_class.new(params[resource_name])
84
+ end
85
+
86
+ # Helper for use in before_filters where no authentication is required.
87
+ #
88
+ # Example:
89
+ # before_filter :require_no_authentication, :only => :new
90
+ def require_no_authentication
91
+ redirect_to root_path if warden.authenticated?(resource_name)
92
+ end
93
+
70
94
  # Sets the flash message with :key, using I18n. By default you are able
71
95
  # to setup your messages using specific resource scope, and if no one is
72
96
  # found we look to default scope.
@@ -15,12 +15,14 @@ module Devise
15
15
  #
16
16
  # new_confirmation_path(:user) => new_user_confirmation_path
17
17
  # confirmation_path(:user) => user_confirmation_path
18
+ #
19
+ # Those helpers are added to your ApplicationController.
18
20
  module UrlHelpers
19
21
 
20
22
  [:session, :password, :confirmation].each do |module_name|
21
23
  [:path, :url].each do |path_or_url|
22
24
  actions = [ nil, :new_ ]
23
- actions << :edit_ if module_name == :password
25
+ actions << :edit_ if module_name == :password
24
26
  actions << :destroy_ if module_name == :session
25
27
 
26
28
  actions.each do |action|
@@ -23,7 +23,7 @@ module Devise
23
23
  headers["Content-Type"] = 'text/plain'
24
24
 
25
25
  message = options[:message] || "You are being redirected to #{redirect_path}"
26
- [302, headers, message]
26
+ [302, headers, [message]]
27
27
  end
28
28
  end
29
29
  end
@@ -8,7 +8,6 @@ module ActionController::Routing
8
8
  load_routes_without_devise!
9
9
 
10
10
  ActionController::Base.send :include, Devise::Controllers::Filters
11
- ActionController::Base.send :include, Devise::Controllers::Helpers
12
11
  ActionController::Base.send :include, Devise::Controllers::UrlHelpers
13
12
 
14
13
  ActionView::Base.send :include, Devise::Controllers::UrlHelpers
@@ -1,3 +1,3 @@
1
1
  module Devise
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.1".freeze
3
3
  end
@@ -83,13 +83,6 @@ class ControllerAuthenticableTest < ActionController::TestCase
83
83
  @controller.admin_session
84
84
  end
85
85
 
86
- test 'require no authentication tests current mapping' do
87
- @controller.expects(:resource_name).returns(:user)
88
- @mock_warden.expects(:authenticated?).with(:user).returns(true)
89
- @controller.expects(:redirect_to).with(root_path)
90
- @controller.send :require_no_authentication
91
- end
92
-
93
86
  test 'sign in automatically proxy to set user on warden' do
94
87
  @mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
95
88
  @controller.sign_in(:user, user)
@@ -1,7 +1,11 @@
1
1
  require 'test/test_helper'
2
2
 
3
+ class MyController < ApplicationController
4
+ include Devise::Controllers::Helpers
5
+ end
6
+
3
7
  class HelpersTest < ActionController::TestCase
4
- tests ApplicationController
8
+ tests MyController
5
9
 
6
10
  test 'get resource name from request path' do
7
11
  @request.path = '/users/session'
@@ -37,4 +41,11 @@ class HelpersTest < ActionController::TestCase
37
41
  test 'resources methods are not controller actions' do
38
42
  assert @controller.class.action_methods.empty?
39
43
  end
44
+
45
+ test 'require no authentication tests current mapping' do
46
+ @controller.expects(:resource_name).returns(:user)
47
+ @mock_warden.expects(:authenticated?).with(:user).returns(true)
48
+ @controller.expects(:redirect_to).with(root_path)
49
+ @controller.send :require_no_authentication
50
+ end
40
51
  end
@@ -25,10 +25,10 @@ class FailureTest < ActiveSupport::TestCase
25
25
  end
26
26
 
27
27
  test 'setup a default message' do
28
- assert_equal 'You are being redirected to /users/sign_in', call_failure.last
28
+ assert_equal ['You are being redirected to /users/sign_in'], call_failure.last
29
29
  end
30
30
 
31
31
  test 'pass in a different message' do
32
- assert_equal 'Hello world', call_failure(:message => 'Hello world').last
32
+ assert_equal ['Hello world'], call_failure(:message => 'Hello world').last
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-10-24 00:00:00 -02:00
13
+ date: 2009-10-27 00:00:00 -02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -56,12 +56,6 @@ files:
56
56
  - generators/devise/templates/model.rb
57
57
  - generators/devise_views/USAGE
58
58
  - generators/devise_views/devise_views_generator.rb
59
- - generators/devise_views/templates/confirmations/new.html.erb
60
- - generators/devise_views/templates/notifier/confirmation_instructions.html.erb
61
- - generators/devise_views/templates/notifier/reset_password_instructions.html.erb
62
- - generators/devise_views/templates/passwords/edit.html.erb
63
- - generators/devise_views/templates/passwords/new.html.erb
64
- - generators/devise_views/templates/sessions/new.html.erb
65
59
  - init.rb
66
60
  - lib/devise.rb
67
61
  - lib/devise/active_record.rb
@@ -113,42 +107,42 @@ signing_key:
113
107
  specification_version: 3
114
108
  summary: Flexible authentication solution for Rails with Warden
115
109
  test_files:
116
- - test/controllers/filters_test.rb
117
- - test/controllers/helpers_test.rb
110
+ - test/rails_app/config/boot.rb
111
+ - test/rails_app/config/routes.rb
112
+ - test/rails_app/config/environments/development.rb
113
+ - test/rails_app/config/environments/production.rb
114
+ - test/rails_app/config/environments/test.rb
115
+ - test/rails_app/config/environment.rb
116
+ - test/rails_app/config/initializers/session_store.rb
117
+ - test/rails_app/config/initializers/new_rails_defaults.rb
118
+ - test/rails_app/app/controllers/users_controller.rb
119
+ - test/rails_app/app/controllers/application_controller.rb
120
+ - test/rails_app/app/controllers/admins_controller.rb
121
+ - test/rails_app/app/controllers/home_controller.rb
122
+ - test/rails_app/app/helpers/application_helper.rb
123
+ - test/rails_app/app/models/admin.rb
124
+ - test/rails_app/app/models/organizer.rb
125
+ - test/rails_app/app/models/account.rb
126
+ - test/rails_app/app/models/user.rb
118
127
  - test/controllers/url_helpers_test.rb
119
- - test/models/validatable_test.rb
120
- - test/models/rememberable_test.rb
121
- - test/models/confirmable_test.rb
122
- - test/models/authenticable_test.rb
123
- - test/models/recoverable_test.rb
124
- - test/integration/rememberable_test.rb
125
- - test/integration/confirmable_test.rb
128
+ - test/controllers/helpers_test.rb
129
+ - test/controllers/filters_test.rb
126
130
  - test/integration/authenticable_test.rb
131
+ - test/integration/rememberable_test.rb
127
132
  - test/integration/recoverable_test.rb
128
- - test/active_record_test.rb
129
- - test/test_helper.rb
130
- - test/mailers/reset_password_instructions_test.rb
133
+ - test/integration/confirmable_test.rb
131
134
  - test/mailers/confirmation_instructions_test.rb
135
+ - test/mailers/reset_password_instructions_test.rb
136
+ - test/models/authenticable_test.rb
137
+ - test/models/rememberable_test.rb
138
+ - test/models/recoverable_test.rb
139
+ - test/models/validatable_test.rb
140
+ - test/models/confirmable_test.rb
132
141
  - test/failure_test.rb
133
- - test/routes_test.rb
134
- - test/rails_app/app/controllers/admins_controller.rb
135
- - test/rails_app/app/controllers/home_controller.rb
136
- - test/rails_app/app/controllers/users_controller.rb
137
- - test/rails_app/app/controllers/application_controller.rb
138
- - test/rails_app/app/models/account.rb
139
- - test/rails_app/app/models/user.rb
140
- - test/rails_app/app/models/admin.rb
141
- - test/rails_app/app/models/organizer.rb
142
- - test/rails_app/app/helpers/application_helper.rb
143
- - test/rails_app/config/boot.rb
144
- - test/rails_app/config/environments/production.rb
145
- - test/rails_app/config/environments/development.rb
146
- - test/rails_app/config/environments/test.rb
147
- - test/rails_app/config/initializers/new_rails_defaults.rb
148
- - test/rails_app/config/initializers/session_store.rb
149
- - test/rails_app/config/routes.rb
150
- - test/rails_app/config/environment.rb
151
- - test/mapping_test.rb
152
142
  - test/support/model_tests_helper.rb
153
143
  - test/support/assertions_helper.rb
154
144
  - test/support/integration_tests_helper.rb
145
+ - test/routes_test.rb
146
+ - test/test_helper.rb
147
+ - test/mapping_test.rb
148
+ - test/active_record_test.rb
@@ -1,16 +0,0 @@
1
- <h2>Resend confirmation instructions</h2>
2
-
3
- <% form_for resource_name, :url => confirmation_path(resource_name) do |f| %>
4
- <%= f.error_messages %>
5
-
6
- <p><%= f.label :email %></p>
7
- <p><%= f.text_field :email %></p>
8
-
9
- <p><%= f.submit "Resend confirmation instructions" %></p>
10
- <% end %>
11
-
12
- <%= link_to "Sign in", new_session_path(resource_name) %><br />
13
-
14
- <%- if devise_mapping.recoverable? %>
15
- <%= link_to "Forgot password?", new_password_path(resource_name) %><br />
16
- <% end -%>
@@ -1,5 +0,0 @@
1
- Welcome <%= @resource.email %>!
2
-
3
- You can confirm your account through the link below:
4
-
5
- <%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>
@@ -1,8 +0,0 @@
1
- Hello <%= @resource.email %>!
2
-
3
- Someone has requested a link to change your password, and you can do this through the link below.
4
-
5
- <%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>
6
-
7
- If you didn't request this, please ignore this email.
8
- Your password won't change until you access the link above and create a new one.
@@ -1,20 +0,0 @@
1
- <h2>Change your password</h2>
2
-
3
- <% form_for resource_name, :url => password_path(resource_name), :html => { :method => :put } do |f| %>
4
- <%= f.error_messages %>
5
- <%= f.hidden_field :reset_password_token %>
6
-
7
- <p><%= f.label :password %></p>
8
- <p><%= f.password_field :password %></p>
9
-
10
- <p><%= f.label :password_confirmation %></p>
11
- <p><%= f.password_field :password_confirmation %></p>
12
-
13
- <p><%= f.submit "Change my password" %></p>
14
- <% end %>
15
-
16
- <%= link_to "Sign in", new_session_path(resource_name) %><br />
17
-
18
- <%- if devise_mapping.confirmable? %>
19
- <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
20
- <% end -%>
@@ -1,16 +0,0 @@
1
- <h2>Forgot your password?</h2>
2
-
3
- <% form_for resource_name, :url => password_path(resource_name) do |f| %>
4
- <%= f.error_messages %>
5
-
6
- <p><%= f.label :email %></p>
7
- <p><%= f.text_field :email %></p>
8
-
9
- <p><%= f.submit "Send me reset password instructions" %></p>
10
- <% end %>
11
-
12
- <%= link_to "Sign in", new_session_path(resource_name) %><br />
13
-
14
- <%- if devise_mapping.confirmable? %>
15
- <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
16
- <% end -%>
@@ -1,23 +0,0 @@
1
- <h2>Sign in</h2>
2
-
3
- <% form_for resource_name, :url => session_path(resource_name) do |f| -%>
4
- <p><%= f.label :email %></p>
5
- <p><%= f.text_field :email %></p>
6
-
7
- <p><%= f.label :password %></p>
8
- <p><%= f.password_field :password %></p>
9
-
10
- <% if devise_mapping.rememberable? -%>
11
- <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
12
- <% end -%>
13
-
14
- <p><%= f.submit "Sign in" %></p>
15
- <% end -%>
16
-
17
- <%- if devise_mapping.recoverable? %>
18
- <%= link_to "Forgot password?", new_password_path(resource_name) %><br />
19
- <% end -%>
20
-
21
- <%- if devise_mapping.confirmable? %>
22
- <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
23
- <% end -%>