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 # POST /password
# #
def create 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? 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.') 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 redirect_to new_session_path
@ -22,14 +22,14 @@ class PasswordsController < ApplicationController
# GET /password/edit?perishable_token=abcdef # GET /password/edit?perishable_token=abcdef
# #
def edit def edit
self.resource = User.new self.resource = resource_class.new
resource.perishable_token = params[:perishable_token] resource.perishable_token = params[:perishable_token]
end end
# PUT /password # PUT /password
# #
def update def update
self.resource = User.reset_password!(params[resource_name]) self.resource = resource_class.reset_password!(params[resource_name])
if resource.errors.empty? if resource.errors.empty?
flash[:notice] = I18n.t(:update, :scope => [:devise, :passwords], :default => 'Your password was changed successfully.') flash[:notice] = I18n.t(:update, :scope => [:devise, :passwords], :default => 'Your password was changed successfully.')
redirect_to new_session_path 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) I18n.t(route_name, :scope => [:devise, :routes], :default => route_name.to_s)
end end
# map.resource :session, :only => [:new, :create, :destroy], :as => t(:session) Devise.mappings.each do |resource, mapping|
# map.resource :password, :only => [:new, :create, :edit, :update], :as => t(:password) map.namespace mapping.resource, :namespace => nil, :path_prefix => mapping.as do |devise_map|
# 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_map.resource :session, :only => [:new, :create, :destroy], :as => t(:session) 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 :password, :only => [:new, :create, :edit, :update], :as => t(:password)
devise_map.resource :confirmation, :only => [:new, :create, :show], :as => t(:confirmation) devise_map.resource :confirmation, :only => [:new, :create, :show], :as => t(:confirmation)

View file

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

View file

@ -29,7 +29,7 @@ Warden::Strategies.add(:devise) do
# warden the authentication was failed. # warden the authentication was failed.
# #
def authenticate! 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) success!(user)
else else
fail!(I18n.t(:authentication_failed, :scope => [:devise, :sessions], :default => 'Invalid email or password')) 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] Devise.map :participants, :to => Participant, :for => [:authenticable]
mappings = Devise.mappings mappings = Devise.mappings
assert_not mappings.empty? 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 end
test 'require :for option' do test 'require :for option' do
@ -59,18 +61,25 @@ class MapTest < ActiveSupport::TestCase
test 'find right mapping to use for routing' do test 'find right mapping to use for routing' do
Devise.map :participants, :for => [:authenticable] Devise.map :participants, :for => [:authenticable]
assert_equal 'participant', Devise.find_mapping('participants') assert_equal :participant, Devise.find_mapping('participants').resource
end end
test 'find right mapping to Participant for routing with :as option' do test 'find right mapping to Participant for routing with :as option' do
Devise.map :participants, :for => [:authenticable], :as => 'usuarios' Devise.map :participants, :for => [:authenticable], :as => 'usuarios'
assert_equal 'participant', Devise.find_mapping('usuarios') assert_equal :participant, Devise.find_mapping('usuarios').resource
end end
test 'find mapping should return default map in no one is found or empty is given' do test 'find mapping should return default map in no one is found or empty is given' do
Devise.map :participants, :for => [:authenticable] Devise.map :participants, :for => [:authenticable]
assert_equal 'participant', Devise.find_mapping('test_drive') assert_equal :participant, Devise.find_mapping('test_drive').resource
assert_equal 'participant', Devise.find_mapping(nil) 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 end
test 'find resource name based on mapping' do test 'find resource name based on mapping' do