1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Updating controllers to scope resource_class and changing mapping implementation to reload mapped class based on cache_classes configuration.

This commit is contained in:
Carlos A. da Silva 2009-10-10 11:05:56 -03:00
parent 2fbb56e8f7
commit aefa857ab2
5 changed files with 52 additions and 28 deletions

View file

@ -10,7 +10,7 @@ class PasswordsController < ApplicationController
# POST /password
#
def create
self.resource = User.send_reset_password_instructions(params[resource_name])
self.resource = resource_class.send_reset_password_instructions(params[resource_name])
if resource.errors.empty?
flash[:notice] = I18n.t(:send_instructions, :scope => [:devise, :passwords], :default => 'You will receive an email with instructions about how to reset your password in a few minutes.')
redirect_to new_session_path
@ -22,14 +22,14 @@ class PasswordsController < ApplicationController
# GET /password/edit?perishable_token=abcdef
#
def edit
self.resource = User.new
self.resource = resource_class.new
resource.perishable_token = params[:perishable_token]
end
# PUT /password
#
def update
self.resource = User.reset_password!(params[resource_name])
self.resource = resource_class.reset_password!(params[resource_name])
if resource.errors.empty?
flash[:notice] = I18n.t(:update, :scope => [:devise, :passwords], :default => 'Your password was changed successfully.')
redirect_to new_session_path

View file

@ -3,12 +3,8 @@ ActionController::Routing::Routes.draw do |map|
I18n.t(route_name, :scope => [:devise, :routes], :default => route_name.to_s)
end
# map.resource :session, :only => [:new, :create, :destroy], :as => t(:session)
# map.resource :password, :only => [:new, :create, :edit, :update], :as => t(:password)
# map.resource :confirmation, :only => [:new, :create, :show], :as => t(:confirmation)
Devise.mappings.each do |mapping, options|
map.namespace mapping, :namespace => nil, :path_prefix => options[:as] do |devise_map|
Devise.mappings.each do |resource, mapping|
map.namespace mapping.resource, :namespace => nil, :path_prefix => mapping.as do |devise_map|
devise_map.resource :session, :only => [:new, :create, :destroy], :as => t(:session)
devise_map.resource :password, :only => [:new, :create, :edit, :update], :as => t(:password)
devise_map.resource :confirmation, :only => [:new, :create, :show], :as => t(:confirmation)

View file

@ -15,6 +15,27 @@ end
require 'devise/initializers/warden'
module Devise
class Mapping
attr_accessor :resource, :as, :for
def initialize(options={})
@resource = options[:resource]
@to = options[:to]
@for = options[:for]
@as = options[:as] || resource.to_s.pluralize
end
def to
return @to if @to
to = resource.to_s.classify.constantize
@to = to if Rails.configuration.cache_classes
to
end
def [](key)
send(key)
end
end
mattr_accessor :mappings
self.mappings = {}.with_indifferent_access
@ -22,31 +43,29 @@ module Devise
def self.map(mapping, options={})
raise ArgumentError, "Need to provide :for option for Devise.map" unless options.key?(:for)
options.assert_valid_keys(:to, :for, :as)
mapping = mapping.to_s
options[:as] ||= mapping.pluralize
mapping = mapping.singularize
options[:to] ||= mapping.camelize.constantize
mapping = mapping.to_sym
mappings[mapping] = options
mapping = mapping.to_s.singularize.to_sym
mappings[mapping] = Mapping.new(options.merge(:resource => mapping))
mappings.default = mapping if mappings.default.nil?
end
def self.find_mapping(map)
if mappings.key?(map.try(:to_sym))
map
map = map.to_s.split('/').reject(&:blank?).first
map_sym = map.try(:to_sym)
if mappings.key?(map_sym)
mappings[map_sym]
elsif mapping = mappings.detect{|m, options| options[:as] == map}.try(:first)
mapping
mappings[mapping]
else
mappings.default
end.to_s
mappings[mappings.default]
end
end
def self.resource_name(map)
find_mapping(map)
find_mapping(map).try(:resource).to_s
end
def self.resource_class(map)
mappings[resource_name(map).to_sym].try(:fetch, :to, nil)
find_mapping(map).try(:to)
end
end

View file

@ -29,7 +29,7 @@ Warden::Strategies.add(:devise) do
# warden the authentication was failed.
#
def authenticate!
if user = User.authenticate(params[:session][:email], params[:session][:password])
if user = Devise.resource_class(request.path).authenticate(params[:session][:email], params[:session][:password])
success!(user)
else
fail!(I18n.t(:authentication_failed, :scope => [:devise, :sessions], :default => 'Invalid email or password'))

View file

@ -18,7 +18,9 @@ class MapTest < ActiveSupport::TestCase
Devise.map :participants, :to => Participant, :for => [:authenticable]
mappings = Devise.mappings
assert_not mappings.empty?
assert_equal({:to => Participant, :for => [:authenticable], :as => 'participants'}, mappings[:participant])
assert_equal Participant, mappings[:participant].to
assert_equal [:authenticable], mappings[:participant].for
assert_equal 'participants', mappings[:participant].as
end
test 'require :for option' do
@ -59,18 +61,25 @@ class MapTest < ActiveSupport::TestCase
test 'find right mapping to use for routing' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participant', Devise.find_mapping('participants')
assert_equal :participant, Devise.find_mapping('participants').resource
end
test 'find right mapping to Participant for routing with :as option' do
Devise.map :participants, :for => [:authenticable], :as => 'usuarios'
assert_equal 'participant', Devise.find_mapping('usuarios')
assert_equal :participant, Devise.find_mapping('usuarios').resource
end
test 'find mapping should return default map in no one is found or empty is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participant', Devise.find_mapping('test_drive')
assert_equal 'participant', Devise.find_mapping(nil)
assert_equal :participant, Devise.find_mapping('test_drive').resource
assert_equal :participant, Devise.find_mapping(nil).resource
end
test 'find mapping receiving a path should split it' do
Devise.map :participants, :for => [:authenticable]
Devise.map :organizer, :for => [:authenticable]
assert_equal :organizer, Devise.find_mapping('organizer').resource
assert_equal :organizer, Devise.find_mapping('/organizers/new').resource
end
test 'find resource name based on mapping' do