From e127463ac8e7fcc01b7d62e09a7a7db42f798411 Mon Sep 17 00:00:00 2001 From: Jacques Crocker Date: Fri, 26 Mar 2010 12:25:12 -0700 Subject: [PATCH] Adding Mongoid 2.0 Support, Removing MongoMapper for now --- Gemfile | 6 +-- README.rdoc | 2 +- lib/devise.rb | 2 +- .../orm/{mongo_mapper.rb => mongoid.rb} | 40 ++++++++++--------- .../devise_install/templates/devise.rb | 2 +- test/models/rememberable_test.rb | 7 +--- test/models/validatable_test.rb | 2 +- test/orm/mongo_mapper.rb | 12 ------ test/orm/mongoid.rb | 17 ++++++++ test/orm/mongoid/locale/en.yml | 4 ++ test/rails_app/app/mongo_mapper/admin.rb | 10 ----- test/rails_app/app/mongoid/admin.rb | 14 +++++++ .../app/{mongo_mapper => mongoid}/user.rb | 9 ++++- test/rails_app/config/initializers/devise.rb | 2 +- 14 files changed, 73 insertions(+), 56 deletions(-) rename lib/devise/orm/{mongo_mapper.rb => mongoid.rb} (51%) delete mode 100644 test/orm/mongo_mapper.rb create mode 100644 test/orm/mongoid.rb create mode 100644 test/orm/mongoid/locale/en.yml delete mode 100644 test/rails_app/app/mongo_mapper/admin.rb create mode 100644 test/rails_app/app/mongoid/admin.rb rename test/rails_app/app/{mongo_mapper => mongoid}/user.rb (62%) diff --git a/Gemfile b/Gemfile index 0d6870fd..f96015b0 100644 --- a/Gemfile +++ b/Gemfile @@ -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 \ No newline at end of file diff --git a/README.rdoc b/README.rdoc index 2545d5a6..9ee8be11 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 diff --git a/lib/devise.rb b/lib/devise.rb index 9e4fb0f8..374406eb 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -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 diff --git a/lib/devise/orm/mongo_mapper.rb b/lib/devise/orm/mongoid.rb similarity index 51% rename from lib/devise/orm/mongo_mapper.rb rename to lib/devise/orm/mongoid.rb index a1420a71..472a8556 100644 --- a/lib/devise/orm/mongo_mapper.rb +++ b/lib/devise/orm/mongoid.rb @@ -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 \ No newline at end of file diff --git a/lib/generators/devise_install/templates/devise.rb b/lib/generators/devise_install/templates/devise.rb index f050b2ac..1575d1dc 100644 --- a/lib/generators/devise_install/templates/devise.rb +++ b/lib/generators/devise_install/templates/devise.rb @@ -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' diff --git a/test/models/rememberable_test.rb b/test/models/rememberable_test.rb index 18f47acd..b0834ad5 100644 --- a/test/models/rememberable_test.rb +++ b/test/models/rememberable_test.rb @@ -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 diff --git a/test/models/validatable_test.rb b/test/models/validatable_test.rb index 39e66b25..16a2b0f1 100644 --- a/test/models/validatable_test.rb +++ b/test/models/validatable_test.rb @@ -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) diff --git a/test/orm/mongo_mapper.rb b/test/orm/mongo_mapper.rb deleted file mode 100644 index e206c347..00000000 --- a/test/orm/mongo_mapper.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/test/orm/mongoid.rb b/test/orm/mongoid.rb new file mode 100644 index 00000000..affa33fe --- /dev/null +++ b/test/orm/mongoid.rb @@ -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 \ No newline at end of file diff --git a/test/orm/mongoid/locale/en.yml b/test/orm/mongoid/locale/en.yml new file mode 100644 index 00000000..e4501ceb --- /dev/null +++ b/test/orm/mongoid/locale/en.yml @@ -0,0 +1,4 @@ +en: + errors: + messages: + taken: "has already been taken" diff --git a/test/rails_app/app/mongo_mapper/admin.rb b/test/rails_app/app/mongo_mapper/admin.rb deleted file mode 100644 index e2a04c3a..00000000 --- a/test/rails_app/app/mongo_mapper/admin.rb +++ /dev/null @@ -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 diff --git a/test/rails_app/app/mongoid/admin.rb b/test/rails_app/app/mongoid/admin.rb new file mode 100644 index 00000000..539406b2 --- /dev/null +++ b/test/rails_app/app/mongoid/admin.rb @@ -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 diff --git a/test/rails_app/app/mongo_mapper/user.rb b/test/rails_app/app/mongoid/user.rb similarity index 62% rename from test/rails_app/app/mongo_mapper/user.rb rename to test/rails_app/app/mongoid/user.rb index 6e7bc616..e1f4c69e 100644 --- a/test/rails_app/app/mongo_mapper/user.rb +++ b/test/rails_app/app/mongoid/user.rb @@ -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 diff --git a/test/rails_app/config/initializers/devise.rb b/test/rails_app/config/initializers/devise.rb index f0f5b962..1680851b 100644 --- a/test/rails_app/config/initializers/devise.rb +++ b/test/rails_app/config/initializers/devise.rb @@ -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