From 5848f12cc1742afb5e1807271b61368703047d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 12 Oct 2009 21:06:39 -0300 Subject: [PATCH] Added magic predicates to mapping. --- TODO | 2 ++ app/views/confirmations/new.html.erb | 2 +- app/views/passwords/edit.html.erb | 2 +- app/views/passwords/new.html.erb | 2 +- app/views/sessions/new.html.erb | 4 ++-- config/routes.rb | 6 +++--- lib/devise/active_record.rb | 15 +++++++++++---- lib/devise/mapping.rb | 8 ++++++++ test/active_record_test.rb | 6 +++++- test/mapping_test.rb | 8 ++++++++ 10 files changed, 42 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 090e61d7..d7f45604 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/app/views/confirmations/new.html.erb b/app/views/confirmations/new.html.erb index ff426a18..2d1ad0c6 100644 --- a/app/views/confirmations/new.html.erb +++ b/app/views/confirmations/new.html.erb @@ -11,6 +11,6 @@ <%= link_to "Sign in", new_session_path(resource_name) %>
-<%- if devise_mapping.allows?(:passwords) %> +<%- if devise_mapping.recoverable? %> <%= link_to "Forgot password?", new_password_path(resource_name) %>
<% end -%> diff --git a/app/views/passwords/edit.html.erb b/app/views/passwords/edit.html.erb index 248028c1..16057e4a 100644 --- a/app/views/passwords/edit.html.erb +++ b/app/views/passwords/edit.html.erb @@ -15,6 +15,6 @@ <%= link_to "Sign in", new_session_path(resource_name) %>
-<%- if devise_mapping.allows?(:confirmations) %> +<%- if devise_mapping.confirmable? %> <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<% end -%> diff --git a/app/views/passwords/new.html.erb b/app/views/passwords/new.html.erb index b0412820..7389e786 100644 --- a/app/views/passwords/new.html.erb +++ b/app/views/passwords/new.html.erb @@ -11,6 +11,6 @@ <%= link_to "Sign in", new_session_path(resource_name) %>
-<%- if devise_mapping.allows?(:confirmations) %> +<%- if devise_mapping.confirmable? %> <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<% end -%> diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 51466f18..85fae420 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -10,10 +10,10 @@

<%= f.submit "Sign in" %>

<% end -%> -<%- if devise_mapping.allows?(:passwords) %> +<%- if devise_mapping.recoverable? %> <%= link_to "Forgot password?", new_password_path(resource_name) %>
<% end -%> -<%- if devise_mapping.allows?(:confirmations) %> +<%- if devise_mapping.confirmable? %> <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<% end -%> diff --git a/config/routes.rb b/config/routes.rb index e7a6eb89..3debe2c8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/lib/devise/active_record.rb b/lib/devise/active_record.rb index bbfab865..922f50c7 100644 --- a/lib/devise/active_record.rb +++ b/lib/devise/active_record.rb @@ -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 diff --git a/lib/devise/mapping.rb b/lib/devise/mapping.rb index 6c8f985f..64be6d03 100644 --- a/lib/devise/mapping.rb +++ b/lib/devise/mapping.rb @@ -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 diff --git a/test/active_record_test.rb b/test/active_record_test.rb index be52d2ef..0e30178f 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -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) diff --git a/test/mapping_test.rb b/test/mapping_test.rb index e04d3d60..1dbc723c 100644 --- a/test/mapping_test.rb +++ b/test/mapping_test.rb @@ -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