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

Moving resource_name and resource_class to Devise, rewriting route tests, using resource_class in controllers.

This commit is contained in:
Carlos A. da Silva 2009-10-10 08:37:22 -03:00
parent f4de0f6c5a
commit d181975555
18 changed files with 160 additions and 84 deletions

View file

@ -10,7 +10,7 @@ class ConfirmationsController < ApplicationController
# POST /confirmation
#
def create
@confirmation = User.send_confirmation_instructions(params[:confirmation])
@confirmation = resource_class.send_confirmation_instructions(params[:confirmation])
if @confirmation.errors.empty?
flash[:notice] = I18n.t(:send_instructions, :scope => [:devise, :confirmations], :default => 'You will receive an email with instructions about how to confirm your account in a few minutes.')
redirect_to new_session_path
@ -22,7 +22,7 @@ class ConfirmationsController < ApplicationController
# GET /confirmation?perishable_token=abcdef
#
def show
@confirmation = User.confirm!(:perishable_token => params[:perishable_token])
@confirmation = resource_class.confirm!(:perishable_token => params[:perishable_token])
if @confirmation.errors.empty?
flash[:notice] = I18n.t(:confirm, :scope => [:devise, :confirmations], :default => 'Your account was successfully confirmed!')
redirect_to new_session_path

View file

@ -10,7 +10,7 @@ class PasswordsController < ApplicationController
# POST /password
#
def create
@password = User.send_reset_password_instructions(params[:password])
@password = resource_class.send_reset_password_instructions(params[:password])
if @password.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
@password = User.new
@password = resource_class.new
@password.perishable_token = params[:perishable_token]
end
# PUT /password
#
def update
@password = User.reset_password!(params[:password])
@password = resource_class.reset_password!(params[:password])
if @password.errors.empty?
flash[:notice] = I18n.t(:update, :scope => [:devise, :passwords], :default => 'Your password was changed successfully.')
redirect_to new_session_path

View file

@ -17,19 +17,22 @@ require 'devise/initializers/warden'
module Devise
mattr_accessor :mappings
self.mappings = {}
self.mappings = {}.with_indifferent_access
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)
options[:as] ||= mapping
options[:to] ||= mapping.to_s.singularize.camelize.constantize
mappings[mapping.to_sym] = options
mappings.default = mapping.to_sym if mappings.default.nil?
mapping = mapping.to_s
options[:as] ||= mapping.pluralize
mapping = mapping.singularize
options[:to] ||= mapping.camelize.constantize
mapping = mapping.to_sym
mappings[mapping] = options
mappings.default = mapping if mappings.default.nil?
end
def self.find_mapping(map)
if map.present? && mappings.key?(map.to_sym)
if mappings.key?(map.try(:to_sym))
map
elsif mapping = mappings.detect{|m, options| options[:as] == map}.try(:first)
mapping
@ -37,10 +40,16 @@ module Devise
mappings.default
end.to_s
end
def self.resource_name(map)
find_mapping(map)
end
def self.resource_class(map)
mappings[resource_name(map).to_sym].try(:fetch, :to, nil)
end
end
ActionView::Base.send :include, DeviseHelper
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
ActionController::Base.send :include, Devise::Controllers::Authenticable
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
ActiveRecord::Base.send :extend, Devise::ActiveRecord
ActionController::Base.send :include, Devise::ActionController
ActionView::Base.send :include, Devise::ActionView

View file

@ -0,0 +1,12 @@
module Devise
module ActionController
def self.included(base)
base.class_eval do
include Devise::Controllers::Authenticable
include Devise::Controllers::Resources
include Devise::Controllers::UrlHelpers
end
end
end
end

12
lib/devise/action_view.rb Normal file
View file

@ -0,0 +1,12 @@
module Devise
module ActionView
def self.included(base)
base.class_eval do
include DeviseHelper
include Devise::Controllers::Resources
include Devise::Controllers::UrlHelpers
end
end
end
end

View file

@ -2,17 +2,16 @@ module Devise
module Controllers
module Authenticable
def self.included(base)
base.class_eval do
# def self.included(base)
# base.class_eval do
# helper_method :session_path, :session_url,
# :new_session_path, :new_session_url,
# :password_path, :password_url,
# :new_password_path, :new_password_url,
# :confirmation_path, :confirmation_url,
# :new_confirmation_path, :new_confirmation_url
end
end
# end
# end
protected

View file

@ -0,0 +1,20 @@
module Devise
module Controllers
module Resources
def resource_name(object=nil)
@resource_name ||= Devise.resource_name(resource_name_or_request_path(object))
end
def resource_class
@resource_class ||= Devise.resource_class(resource_name_or_request_path)
end
private
def resource_name_or_request_path(object=nil)
object ? object.class.name : request.path.split('/').second
end
end
end
end

View file

@ -2,13 +2,17 @@ module Devise
module Controllers
module UrlHelpers
def resource_name(resource=nil)
@resource_name ||= Devise.find_mapping(resource ? resource.class.name : request.path.split('/').second)
end
def resource_class
@resource_class ||= resource_name.singularize.camelize.constantize
end
# def self.included(base)
# base.class_eval do
# helper_method :session_path, :session_url,
# :new_session_path, :new_session_url,
# :password_path, :password_url,
# :new_password_path, :new_password_url,
# :edit_password_path, :edit_password_url,
# :confirmation_path, :confirmation_url,
# :new_confirmation_path, :new_confirmation_url
# end
# end
# TODO: refactor url helpers generation
[:session, :password, :confirmation].each do |module_name|

View file

@ -5,7 +5,7 @@ class ResourcesTest < ActionController::TestCase
test 'should get resource name from request path' do
@request.path = '/users/session'
assert_equal 'users', @controller.resource_name
assert_equal 'user', @controller.resource_name
end
test 'should get translated resource name from request path' do

View file

@ -7,20 +7,20 @@ class RoutesTest < ActionController::TestCase
@request.path = '/users/session'
prepend_path = "#{prepend_path}_" if prepend_path
assert_equal @controller.send(:"#{prepend_path}#{name}_path"),
send(:"#{prepend_path}users_#{name}_path")
send(:"#{prepend_path}user_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url"),
send(:"#{prepend_path}users_#{name}_url")
send(:"#{prepend_path}user_#{name}_url")
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :param => 123),
send(:"#{prepend_path}users_#{name}_path", :param => 123)
send(:"#{prepend_path}user_#{name}_path", :param => 123)
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :param => 123),
send(:"#{prepend_path}users_#{name}_url", :param => 123)
send(:"#{prepend_path}user_#{name}_url", :param => 123)
# @request.path = nil
# assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
# send(:"#{prepend_path}users_#{name}_path")
# assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
# send(:"#{prepend_path}users_#{name}_url")
@request.path = nil
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
send(:"#{prepend_path}user_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
send(:"#{prepend_path}user_#{name}_url")
end

View file

@ -13,35 +13,35 @@ class DeviseHelperTest < ActionView::TestCase
end
test 'should generate a link to sign in' do
self.stubs(:new_session_path).returns(new_users_session_path)
assert_equal %[<a href="#{new_users_session_path}">Sign in</a>], link_to_sign_in
self.stubs(:new_session_path).returns(new_user_session_path)
assert_equal %[<a href="#{new_user_session_path}">Sign in</a>], link_to_sign_in
end
test 'should use i18n to translante sign in link' do
self.stubs(:new_session_path).returns(new_users_session_path)
self.stubs(:new_session_path).returns(new_user_session_path)
store_translations(:sign_in => 'Login')
assert_equal %[<a href="#{new_users_session_path}">Login</a>], link_to_sign_in
assert_equal %[<a href="#{new_user_session_path}">Login</a>], link_to_sign_in
end
test 'should generate a link to forgot password' do
self.stubs(:new_password_path).returns(new_users_password_path)
assert_equal %[<a href="#{new_users_password_path}">Forgot password?</a>], link_to_new_password
self.stubs(:new_password_path).returns(new_user_password_path)
assert_equal %[<a href="#{new_user_password_path}">Forgot password?</a>], link_to_new_password
end
test 'should use i18n to translante forgot password link' do
self.stubs(:new_password_path).returns(new_users_password_path)
self.stubs(:new_password_path).returns(new_user_password_path)
store_translations(:new_password => 'New password?')
assert_equal %[<a href="#{new_users_password_path}">New password?</a>], link_to_new_password
assert_equal %[<a href="#{new_user_password_path}">New password?</a>], link_to_new_password
end
test 'should generate a link to confirmation instructions' do
self.stubs(:new_confirmation_path).returns(new_users_confirmation_path)
assert_equal %[<a href="#{new_users_confirmation_path}">Didn't receive confirmation instructions?</a>], link_to_new_confirmation
self.stubs(:new_confirmation_path).returns(new_user_confirmation_path)
assert_equal %[<a href="#{new_user_confirmation_path}">Didn't receive confirmation instructions?</a>], link_to_new_confirmation
end
test 'should use i18n to translante confirmation link' do
self.stubs(:new_confirmation_path).returns(new_users_confirmation_path)
self.stubs(:new_confirmation_path).returns(new_user_confirmation_path)
store_translations(:new_confirmation => 'New confirmation?')
assert_equal %[<a href="#{new_users_confirmation_path}">New confirmation?</a>], link_to_new_confirmation
assert_equal %[<a href="#{new_user_confirmation_path}">New confirmation?</a>], link_to_new_confirmation
end
end

View file

@ -60,7 +60,7 @@ class AuthenticationTest < ActionController::IntegrationTest
delete 'users/session'
assert_response :redirect
assert_redirected_to new_users_session_path
assert_redirected_to new_user_session_path
assert !warden.authenticated?
end
end

View file

@ -5,7 +5,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
test 'authenticated user should not be able to visit confirmation page' do
sign_in
get new_users_confirmation_path
get new_user_confirmation_path
assert_response :redirect
assert_redirected_to root_path
@ -28,7 +28,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
end
test 'not authenticated user with invalid perishable token should not be able to confirm an account' do
visit users_confirmation_path(:perishable_token => 'invalid_perishable')
visit user_confirmation_path(:perishable_token => 'invalid_perishable')
assert_response :success
assert_template 'confirmations/new'
@ -40,7 +40,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
user = create_user(:confirm => false)
assert_not user.confirmed?
visit users_confirmation_path(:perishable_token => user.perishable_token)
visit user_confirmation_path(:perishable_token => user.perishable_token)
# assert_response :redirect
assert_template 'sessions/new'
@ -51,7 +51,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
test 'already confirmed user should not be able to confirm the account again' do
user = create_user
visit users_confirmation_path(:perishable_token => user.perishable_token)
visit user_confirmation_path(:perishable_token => user.perishable_token)
assert_template 'confirmations/new'
assert_have_selector '#errorExplanation'

View file

@ -18,7 +18,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'authenticated user should not be able to visit forgot password page' do
sign_in
get new_users_password_path
get new_user_password_path
assert_response :redirect
assert_redirected_to root_path
@ -58,7 +58,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'authenticated user should not be able to visit edit password page' do
sign_in
get edit_users_password_path
get edit_user_password_path
assert_response :redirect
assert_redirected_to root_path
@ -67,7 +67,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with invalid perishable token should not be able to change his password' do
create_user
visit edit_users_password_path(:perishable_token => 'invalid_perishable')
visit edit_user_password_path(:perishable_token => 'invalid_perishable')
assert_response :success
assert_template 'passwords/edit'
@ -84,7 +84,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with valid perisable token but invalid password should not be able to change his password' do
create_user
visit edit_users_password_path(:perishable_token => @user.perishable_token)
visit edit_user_password_path(:perishable_token => @user.perishable_token)
fill_in 'Password', :with => '987654321'
fill_in 'Password confirmation', :with => 'other_password'
@ -99,7 +99,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with valid data should be able to change his password' do
create_user
visit edit_users_password_path(:perishable_token => @user.perishable_token)
visit edit_user_password_path(:perishable_token => @user.perishable_token)
fill_in 'Password', :with => '987654321'
fill_in 'Password confirmation', :with => '987654321'

View file

@ -1,6 +1,7 @@
require 'test/test_helper'
class Participant < User; end
class Organizer < User; end
class MapTest < ActiveSupport::TestCase
@ -17,7 +18,7 @@ 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[:participants])
assert_equal({:to => Participant, :for => [:authenticable], :as => 'participants'}, mappings[:participant])
end
test 'require :for option' do
@ -36,32 +37,51 @@ class MapTest < ActiveSupport::TestCase
Devise.mappings.default = nil
assert_nil Devise.mappings.default
Devise.map :participants, :for => [:authenticable]
assert_equal :participants, Devise.mappings.default
assert_equal :participant, Devise.mappings.default
Devise.map :organizers, :for => [:authenticable]
assert_equal :participant, Devise.mappings.default
end
test 'singularize map' do
Devise.map :participants, :for => [:authenticable]
assert_not_nil Devise.mappings[:participant]
end
test 'use map name pluralized to :as option if none is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participants', Devise.mappings[:participant][:as]
end
test 'map should lookup for the mapping class if no one is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal Participant, Devise.mappings[:participants][:to]
end
test 'use mapping to :as option if none is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal :participants, Devise.mappings[:participants][:as]
assert_equal Participant, Devise.mappings[:participant][:to]
end
test 'find right mapping to use for routing' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participants', Devise.find_mapping('participants')
assert_equal 'participant', Devise.find_mapping('participants')
end
test 'find right mapping to Participant for routing with :as option' do
Devise.map :participants, :for => [:authenticable], :as => 'usuarios'
assert_equal 'participants', Devise.find_mapping('usuarios')
assert_equal 'participant', Devise.find_mapping('usuarios')
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 'participants', Devise.find_mapping('test_drive')
assert_equal 'participants', Devise.find_mapping(nil)
assert_equal 'participant', Devise.find_mapping('test_drive')
assert_equal 'participant', Devise.find_mapping(nil)
end
test 'find resource name based on mapping' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participant', Devise.resource_name('participants')
end
test 'find resource class based on mapping' do
Devise.map :participants, :for => [:authenticable]
assert_equal Participant, Devise.resource_class('participants')
Devise.map :organizers, :for => [:authenticable]
assert_equal Organizer, Devise.resource_class('organizers')
end
end

View file

@ -3,20 +3,20 @@ require 'test_helper'
class ConfirmationRoutingTest < ActionController::TestCase
test 'new session route' do
assert_routing('users/confirmation/new', :controller => 'confirmations', :action => 'new')
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'users/confirmation/new')
end
test 'create confirmation route' do
assert_routing({:path => 'users/confirmation', :method => :post}, {:controller => 'confirmations', :action => 'create'})
assert_recognizes({:controller => 'confirmations', :action => 'create'}, {:path => 'users/confirmation', :method => :post})
end
test 'show confirmation route' do
assert_routing('users/confirmation', :controller => 'confirmations', :action => 'show')
assert_recognizes({:controller => 'confirmations', :action => 'show'}, 'users/confirmation')
end
test 'translated confirmation route' do
translated_route(:confirmation => 'confirmacao') do
assert_routing('users/confirmacao/new', :controller => 'confirmations', :action => 'new')
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'users/confirmacao/new')
end
end
end

View file

@ -3,24 +3,24 @@ require 'test_helper'
class PasswordRoutingTest < ActionController::TestCase
test 'new password route' do
assert_routing('users/password/new', :controller => 'passwords', :action => 'new')
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'users/password/new')
end
test 'create password route' do
assert_routing({:path => 'users/password', :method => :post}, {:controller => 'passwords', :action => 'create'})
assert_recognizes({:controller => 'passwords', :action => 'create'}, {:path => 'users/password', :method => :post})
end
test 'edit password route' do
assert_routing('users/password/edit', :controller => 'passwords', :action => 'edit')
assert_recognizes({:controller => 'passwords', :action => 'edit'}, 'users/password/edit')
end
test 'update password route' do
assert_routing({:path => 'users/password', :method => :put}, {:controller => 'passwords', :action => 'update'})
assert_recognizes({:controller => 'passwords', :action => 'update'}, {:path => 'users/password', :method => :put})
end
test 'translated password route' do
translated_route(:password => 'senha') do
assert_routing('users/senha/new', :controller => 'passwords', :action => 'new')
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'users/senha/new')
end
end
end

View file

@ -3,20 +3,20 @@ require 'test_helper'
class SessionRoutingTest < ActionController::TestCase
test 'new session route' do
assert_routing('users/session/new', :controller => 'sessions', :action => 'new')
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'users/session/new')
end
test 'create session route' do
assert_routing({:path => 'users/session', :method => :post}, {:controller => 'sessions', :action => 'create'})
assert_recognizes({:controller => 'sessions', :action => 'create'}, {:path => 'users/session', :method => :post})
end
test 'destroy session route' do
assert_routing({:path => 'users/session', :method => :delete}, {:controller => 'sessions', :action => 'destroy'})
assert_recognizes({:controller => 'sessions', :action => 'destroy'}, {:path => 'users/session', :method => :delete})
end
test 'translate session route' do
translated_route(:session => 'sessao') do
assert_routing('users/sessao/new', :controller => 'sessions', :action => 'new')
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'users/sessao/new')
end
end
end