Adding Mongoid 2.0 Support, Removing MongoMapper for now

This commit is contained in:
Jacques Crocker 2010-03-26 12:25:12 -07:00
parent bd4b29c0fd
commit e127463ac8
14 changed files with 73 additions and 56 deletions

View File

@ -12,8 +12,8 @@ if RUBY_VERSION < '1.9'
gem "ruby-debug", ">= 0.10.3"
end
group :mongo_mapper do
group :mongoid do
gem "mongo", ">= 0.18.3"
gem "mongo_ext", ">= 0.18.3", :require => false
gem "mongo_mapper", :git => "git://github.com/jnunemaker/mongomapper.git"
end
gem "mongoid", :git => "git://github.com/durran/mongoid.git"
end

View File

@ -251,7 +251,7 @@ Devise implements encryption strategies for Clearance, Authlogic and Restful-Aut
== Other ORMs
Devise supports ActiveRecord (by default) and MongoMapper. We offer experimental Datamapper support (with the limitation that the Devise test suite does not run completely with Datamapper). To choose other ORM, you just need to configure it in the initializer file.
Devise supports ActiveRecord (by default) and Mongoid. We offer experimental Datamapper support (with the limitation that the Devise test suite does not run completely with Datamapper). To choose other ORM, you just need to configure it in the initializer file.
== TODO

View File

@ -79,7 +79,7 @@ module Devise
@@mappings = ActiveSupport::OrderedHash.new
# Tells if devise should apply the schema in ORMs where devise declaration
# and schema belongs to the same class (as Datamapper and MongoMapper).
# and schema belongs to the same class (as Datamapper and Mongoid).
mattr_accessor :apply_schema
@@apply_schema = true

View File

@ -1,49 +1,51 @@
module Devise
module Orm
module MongoMapper
module Mongoid
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. This automatically converts DateTime
# to Time, since MongoMapper does not recognize the former.
# Tell how to apply schema methods
def apply_schema(name, type, options={})
type = Time if type == DateTime
key name, type, options
field name, {:type => type}.merge(options)
end
end
module Compatibility
extend ActiveSupport::Concern
module ClassMethods
def find(*args)
case args.first
when :first, :all
send(args.shift, *args)
else
super
end
def lock!
self.reload
end
def save(validate = true)
if validate.is_a?(Hash) && validate.has_key?(:validate)
validate = validate[:validate]
end
super(validate)
end
def ==(other)
other.is_a?(self.class) && _id == other._id
end
end
end
end
end
[MongoMapper::Document, MongoMapper::EmbeddedDocument].each do |mod|
[Mongoid::Document].each do |mod|
mod::ClassMethods.class_eval do
include Devise::Models
include Devise::Orm::MongoMapper::Hook
include Devise::Orm::Mongoid::Hook
end
end

View File

@ -61,7 +61,7 @@ Devise.setup do |config|
# config.token_authentication_key = :auth_token
# ==> General configuration
# Load and configure the ORM. Supports :active_record (default), :mongo_mapper
# Load and configure the ORM. Supports :active_record (default), :mongoid
# (requires mongo_ext installed) and :data_mapper (experimental).
require 'devise/orm/active_record'

View File

@ -70,11 +70,8 @@ class RememberableTest < ActiveSupport::TestCase
assert_equal user, User.serialize_from_cookie("#{user.id}::#{user.remember_token}")
end
# MongoMapper cries if an invalid ID is given, so this does not need to be tested
unless DEVISE_ORM == :mongo_mapper
test 'serialize should return nil if no user is found' do
assert_nil User.serialize_from_cookie('0::123')
end
test 'serialize should return nil if no user is found' do
assert_nil User.serialize_from_cookie('0::123')
end
test 'remember me return nil if is a valid user with invalid token' do

View File

@ -1,7 +1,7 @@
require 'test_helper'
class ValidatableTest < ActiveSupport::TestCase
extend Devise::TestSilencer if [:mongo_mapper, :data_mapper].include?(DEVISE_ORM)
extend Devise::TestSilencer if [:mongoid, :data_mapper].include?(DEVISE_ORM)
test 'should require email to be set' do
user = new_user(:email => nil)

View File

@ -1,12 +0,0 @@
MongoMapper.database = "devise-test-suite"
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
require File.expand_path('../../rails_app/config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
setup do
User.delete_all
Admin.delete_all
end
end

17
test/orm/mongoid.rb Normal file
View File

@ -0,0 +1,17 @@
require File.expand_path('../../rails_app/config/environment', __FILE__)
require 'rails/test_help'
Mongoid.configure do |config|
config.master = Mongo::Connection.new('127.0.0.1', 27017).db("devise-test-suite")
end
I18n.load_path << File.join(
File.dirname(__FILE__), "mongoid", "locale", "en.yml"
)
class ActiveSupport::TestCase
setup do
User.delete_all
Admin.delete_all
end
end

View File

@ -0,0 +1,4 @@
en:
errors:
messages:
taken: "has already been taken"

View File

@ -1,10 +0,0 @@
class Admin
include MongoMapper::Document
include MongoMapper::Plugins::Callbacks
devise :authenticatable, :timeoutable, :registerable, :recoverable
def self.find_for_authentication(conditions)
last(:conditions => conditions, :order => "email")
end
end

View File

@ -0,0 +1,14 @@
class Admin
include Mongoid::Document
devise :authenticatable, :timeoutable, :registerable, :recoverable
def self.find_for_authentication(conditions)
last(:conditions => conditions, :sort => [[:email, :asc]])
end
def self.last(options={})
options.delete(:order) if options[:order] == "id"
super options
end
end

View File

@ -1,11 +1,16 @@
class User
include MongoMapper::Document
include Mongoid::Document
key :created_at, DateTime
field :created_at, :type => DateTime
devise :authenticatable, :http_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
end

View File

@ -36,7 +36,7 @@ Devise.setup do |config|
# Configure the e-mail address which will be shown in DeviseMailer.
config.mailer_sender = "please-change-me-omg@yourapp.com"
# Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper.
# Load and configure the ORM. Supports :active_record, :data_mapper and :mongoid.
require "devise/orm/#{DEVISE_ORM}"
# Turn scoped views on. Before rendering "sessions/new", it will first check for