Rename apply_schema to apply_devise_schema and refactor Mongoid part a bit.

This commit is contained in:
José Valim 2010-06-13 11:48:35 +02:00
parent 290cfd1f72
commit f16d01869a
8 changed files with 50 additions and 56 deletions

View File

@ -23,7 +23,7 @@ module Devise
include Devise::Schema
# Tell how to apply schema methods.
def apply_schema(name, type, options={})
def apply_devise_schema(name, type, options={})
column name, type.to_s.downcase.to_sym, options
end
end

View File

@ -21,7 +21,7 @@ module Devise
# Tell how to apply schema methods. This automatically maps :limit to
# :length and :null to :required.
def apply_schema(name, type, options={})
def apply_devise_schema(name, type, options={})
SCHEMA_OPTIONS.each do |old_key, new_key|
next unless options.key?(old_key)
options[new_key] = options.delete(old_key)

View File

@ -4,32 +4,21 @@ module Devise
module Hook
def devise_modules_hook!
extend Schema
include ::Mongoid::Timestamps
include Compatibility
yield
return unless Devise.apply_schema
devise_modules.each { |m| send(m) if respond_to?(m, true) }
end
end
module Schema
include Devise::Schema
# Tell how to apply schema methods
def apply_schema(name, type, options={})
def apply_devise_schema(name, type, options={})
type = Time if type == DateTime
field name, { :type => type }.merge(options)
end
end
module Compatibility
def save(validate = true)
if validate.is_a?(Hash) && validate.has_key?(:validate)
validate = validate[:validate]
end
super(validate)
end
end
end
end
end

View File

@ -21,42 +21,42 @@ module Devise
ActiveSupport::Deprecation.warn ":encryptor as option is deprecated, simply remove it."
end
apply_schema :email, String, :null => null, :default => default
apply_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
apply_schema :password_salt, String, :null => null, :default => default
apply_devise_schema :email, String, :null => null, :default => default
apply_devise_schema :encrypted_password, String, :null => null, :default => default, :limit => 128
apply_devise_schema :password_salt, String, :null => null, :default => default
end
# Creates authentication_token.
def token_authenticatable(options={})
apply_schema :authentication_token, String
apply_devise_schema :authentication_token, String
end
# Creates confirmation_token, confirmed_at and confirmation_sent_at.
def confirmable
apply_schema :confirmation_token, String
apply_schema :confirmed_at, DateTime
apply_schema :confirmation_sent_at, DateTime
apply_devise_schema :confirmation_token, String
apply_devise_schema :confirmed_at, DateTime
apply_devise_schema :confirmation_sent_at, DateTime
end
# Creates reset_password_token.
def recoverable
apply_schema :reset_password_token, String
apply_devise_schema :reset_password_token, String
end
# Creates remember_token and remember_created_at.
def rememberable
apply_schema :remember_token, String
apply_schema :remember_created_at, DateTime
apply_devise_schema :remember_token, String
apply_devise_schema :remember_created_at, DateTime
end
# Creates sign_in_count, current_sign_in_at, last_sign_in_at,
# current_sign_in_ip, last_sign_in_ip.
def trackable
apply_schema :sign_in_count, Integer, :default => 0
apply_schema :current_sign_in_at, DateTime
apply_schema :last_sign_in_at, DateTime
apply_schema :current_sign_in_ip, String
apply_schema :last_sign_in_ip, String
apply_devise_schema :sign_in_count, Integer, :default => 0
apply_devise_schema :current_sign_in_at, DateTime
apply_devise_schema :last_sign_in_at, DateTime
apply_devise_schema :current_sign_in_ip, String
apply_devise_schema :last_sign_in_ip, String
end
# Creates failed_attempts, unlock_token and locked_at depending on the options given.
@ -75,18 +75,18 @@ module Devise
lock_strategy ||= :failed_attempts
if lock_strategy == :failed_attempts
apply_schema :failed_attempts, Integer, :default => 0
apply_devise_schema :failed_attempts, Integer, :default => 0
end
if [:both, :email].include?(unlock_strategy)
apply_schema :unlock_token, String
apply_devise_schema :unlock_token, String
end
apply_schema :locked_at, DateTime
apply_devise_schema :locked_at, DateTime
end
# Overwrite with specific modification to create your own schema.
def apply_schema(name, type, options={})
def apply_devise_schema(name, type, options={})
raise NotImplementedError
end
end

View File

@ -309,4 +309,13 @@ class AuthenticationTest < ActionController::IntegrationTest
get '/sign_in'
end
end
# Invalid session
test 'invalid session does not cause errors' do
get "/"
session["warden.user.user.key"] = 170410
get users_path
assert_equal 302, response.status
end
end

View File

@ -1,15 +1,6 @@
class Admin
include Mongoid::Document
include Shim
devise :database_authenticatable, :timeoutable, :registerable, :recoverable
def self.last(options={})
options.delete(:order) if options[:order] == "id"
super options
end
# overwrite equality (because some devise tests use this for asserting model equality)
def ==(other)
other.is_a?(self.class) && _id == other._id
end
end

View File

@ -0,0 +1,16 @@
module Shim
extend ::ActiveSupport::Concern
include ::Mongoid::Timestamps
module ClassMethods
def last(options={})
options.delete(:order) if options[:order] == "id"
super(options)
end
end
# overwrite equality (because some devise tests use this for asserting model equality)
def ==(other)
other.is_a?(self.class) && _id == other._id
end
end

View File

@ -1,21 +1,10 @@
class User
include Mongoid::Document
include Shim
field :created_at, :type => DateTime
devise :database_authenticatable, :confirmable, :lockable, :recoverable,
:registerable, :rememberable, :timeoutable, :token_authenticatable,
:trackable, :validatable
# attr_accessible :username, :email, :password, :password_confirmation
def self.last(options={})
options.delete(:order) if options[:order] == "id"
super options
end
# overwrite equality (because some devise tests use this for asserting model equality)
def ==(other)
other.is_a?(self.class) && _id == other._id
end
end