Added magic predicates to mapping.

This commit is contained in:
José Valim 2009-10-12 21:06:39 -03:00
parent 0664359381
commit 5848f12cc1
10 changed files with 42 additions and 13 deletions

2
TODO
View File

@ -6,6 +6,8 @@
* Use path_names in routes
* Use sign_in and sign_out in SessionsController
* Create generators
* Allow stretches and pepper per model
* Mailer subjects namespaced by model

View File

@ -11,6 +11,6 @@
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- if devise_mapping.allows?(:passwords) %>
<%- if devise_mapping.recoverable? %>
<%= link_to "Forgot password?", new_password_path(resource_name) %><br />
<% end -%>

View File

@ -15,6 +15,6 @@
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- if devise_mapping.allows?(:confirmations) %>
<%- if devise_mapping.confirmable? %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View File

@ -11,6 +11,6 @@
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- if devise_mapping.allows?(:confirmations) %>
<%- if devise_mapping.confirmable? %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View File

@ -10,10 +10,10 @@
<p><%= f.submit "Sign in" %></p>
<% end -%>
<%- if devise_mapping.allows?(:passwords) %>
<%- if devise_mapping.recoverable? %>
<%= link_to "Forgot password?", new_password_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.allows?(:confirmations) %>
<%- if devise_mapping.confirmable? %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View File

@ -1,17 +1,17 @@
ActionController::Routing::Routes.draw do |map|
Devise.mappings.each_value do |mapping|
map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m|
if mapping.allows?(:sessions)
if mapping.authenticable?
m.resource :session,
:only => [:new, :create, :destroy]
end
if mapping.allows?(:passwords)
if mapping.recoverable?
m.resource :password,
:only => [:new, :create, :edit, :update]
end
if mapping.allows?(:confirmations)
if mapping.confirmable?
m.resource :confirmation,
:only => [:new, :create, :show]
end

View File

@ -23,10 +23,17 @@ module Devise
# devise :all
#
def devise(*options)
include Devise::Models::Authenticable
include Devise::Models::Confirmable unless ([:all, :confirmable] & options).empty?
include Devise::Models::Recoverable unless ([:all, :recoverable] & options).empty?
include Devise::Models::Validatable unless ([:all, :validatable] & options).empty?
options = [:confirmable, :recoverable, :validatable] if options.include?(:all)
options |= [:authenticable]
options.each do |m|
devise_modules << m.to_sym
include Devise::Models.const_get(m.to_s.classify)
end
end
def devise_modules
@devise_modules ||= []
end
end
end

View File

@ -24,6 +24,14 @@ module Devise
klass
end
CONTROLLERS.values.each do |m|
class_eval <<-METHOD, __FILE__, __LINE__
def #{m}?
@for.include?(:#{m})
end
METHOD
end
def allows?(controller)
@for.include?(CONTROLLERS[controller.to_sym])
end

View File

@ -23,22 +23,26 @@ end
class ActiveRecordTest < ActiveSupport::TestCase
def include_authenticable_module?(mod)
mod.devise_modules.include?(:authenticable) &&
mod.included_modules.include?(Devise::Models::Authenticable)
end
def include_confirmable_module?(mod)
mod.devise_modules.include?(:confirmable) &&
mod.included_modules.include?(Devise::Models::Confirmable)
end
def include_recoverable_module?(mod)
mod.devise_modules.include?(:recoverable) &&
mod.included_modules.include?(Devise::Models::Recoverable)
end
def include_validatable_module?(mod)
mod.devise_modules.include?(:validatable) &&
mod.included_modules.include?(Devise::Models::Validatable)
end
test 'acts as devisable should include by defaul authenticable only' do
test 'acts as devisable should include by default authenticable only' do
assert include_authenticable_module?(Authenticable)
assert_not include_confirmable_module?(Authenticable)
assert_not include_recoverable_module?(Authenticable)

View File

@ -60,4 +60,12 @@ class MapTest < ActiveSupport::TestCase
Devise.map :participant, :for => [:authenticable, :confirmable], :as => "participantes"
assert_equal Devise.mappings[:participant], Devise.find_mapping_by_path("/participantes/session")
end
test 'magic predicates' do
Devise.map :participant, :for => [:authenticable, :confirmable]
mapping = Devise.mappings[:participant]
assert mapping.authenticable?
assert mapping.confirmable?
assert !mapping.recoverable?
end
end