Making Rails 3 RC, MongoMapper, and Devise play nice…for now

Edit: There is also a MongoMapper Devise plugin 
which has even the model generators working. 
Check it out: http://github.com/kristianmandrup/mm-devise
Let me know what you guys think. 
You can always use my solution below too :)

So this will be a quick informative one. Goal here is simple: get Rails 3 RC, MongoMapper, and Devise working all together nicely without much fuss. I had trouble finding this information so thought others might benefit from my findings.

First, real quickly, Devise for a long while had MongoMapper support. Up only recently (since a couple of months ago or so) they have removed support for MongoMapper since MongoMapper is not ActionModel compliant yet. Don’t panic just yet, from all that I’ve heard and read from John Nunemaker (author of Mongomapper), MongoMapper will be ActionModel compliant once final release of Rails 3 is out. If you’re unsure or don’t believe what I’m saying check out this conversation from the MongoMapper Google Groups .

So what to do for now, well if you looked further on the Google Groups page, Aaron Ti was nice enough to write a very simple and working Devise adapter for MongoMapper. Here is his Gist for his adapter:

So take that adapter and throw it in your lib directory in a file such as /lib/devise_mongomapper_adapter.rb. Then install devise as you would normally:

# install the gem
gem install devise

# install devise to your rails app
rails generate devise:install

Now the usual devise instructions tell you to run the devise model generator. Well, that doesn’t work in the adapter. But DONT PANIC! We can just create our own User model like so:

class User
  include MongoMapper::Document
  
  key :email, String
  key :username, String
  key :first_name, String
  key :last_name, String
  key :encrypted_password, String
  key :password_salt, String
  key :reset_password_token, String
  key :remember_token, String
  key :remember_created_at, Time
  key :sign_in_count, Integer
  key :current_sign_in_at, Time
  key :current_sign_in_ip, String
  timestamps!
  
  attr_accessible :email, :password, :password_confirmation
  
  devise :database_authenticatable, :registerable, :validatable, :recoverable, :rememberable, :trackable

end

Now this last step is very important! In your config/devise.rb make sure you’ve required your Devise MongoMapper adapter like so:

require 'devise_mongo_mapper_adapter'

And that’s it! You’re ready to use MongoMapper with Rails 3 and Devise. Just to let you know, I’m using Devise 1.1.1 currently and haven’t run into any problems. This is again if you can’t wait till MongoMapper, Devise, and Rails 3 play nicely natively again. Once Rails 3 is released, these issues will be of the past.

  1. harisamin posted this
© Copyright 2012 Haris Amin
blog comments powered by Disqus