mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
When using database_authenticatable Devise will now only create an email field when appropriate.
(If using default authentication_keys or custom authentication_keys with email included) Test written for Mongoid, not sure how to test for active_record yet.
This commit is contained in:
parent
a48c815dc8
commit
0287d8cc80
2 changed files with 25 additions and 2 deletions
|
@ -3,7 +3,7 @@ module Devise
|
||||||
# and overwrite the apply_schema method.
|
# and overwrite the apply_schema method.
|
||||||
module Schema
|
module Schema
|
||||||
|
|
||||||
# Creates email, encrypted_password and password_salt.
|
# Creates email when enabled (on by default), encrypted_password and password_salt.
|
||||||
#
|
#
|
||||||
# == Options
|
# == Options
|
||||||
# * :null - When true, allow columns to be null.
|
# * :null - When true, allow columns to be null.
|
||||||
|
@ -15,8 +15,10 @@ module Devise
|
||||||
def database_authenticatable(options={})
|
def database_authenticatable(options={})
|
||||||
null = options[:null] || false
|
null = options[:null] || false
|
||||||
default = options.key?(:default) ? options[:default] : ("" if null == false)
|
default = options.key?(:default) ? options[:default] : ("" if null == false)
|
||||||
|
include_email = !self.respond_to?(:authentication_keys) \
|
||||||
|
|| (self.respond_to?(:authentication_keys) && self.authentication_keys.include?(:email))
|
||||||
|
|
||||||
apply_devise_schema :email, String, :null => null, :default => default
|
apply_devise_schema :email, String, :null => null, :default => default if include_email
|
||||||
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
|
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
21
test/schema_test.rb
Normal file
21
test/schema_test.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SchemaTest < ActiveSupport::TestCase
|
||||||
|
if DEVISE_ORM == :mongoid
|
||||||
|
test 'should create an email field if there are no custom authentication keys' do
|
||||||
|
eval "class User2; include Mongoid::Document; devise :database_authenticatable; end"
|
||||||
|
assert_not_equal User2.fields['email'], nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should create an email field if there are custom authentication keys and they include email' do
|
||||||
|
eval "class User3; include Mongoid::Document; devise :database_authenticatable, :authentication_keys => [:username, :email]; end"
|
||||||
|
assert_not_equal User3.fields['email'], nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'should not create an email field if there are custom authentication keys they don\'t include email' do
|
||||||
|
eval "class User4; include Mongoid::Document; devise :database_authenticatable, :authentication_keys => [:usernames]; end"
|
||||||
|
assert_equal User4.fields['email'], nil
|
||||||
|
assert_equal User4.fields['username'], nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue