Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a Devise extension (to use a custom datastore)

I'd like to write an extension for Devise that allows you to use parse_resource as the datastore (as opposed to ActiveRecord). parse_resource is a Ruby wrapper for Parse.com's REST api. It's interface is pretty much the same as ActiveRecord's and is ActiveModel complaint. Because of this, it seems possible that an extension for Devise may not require too much non-boilerplate.

However, I can't find any tutorials. All I have to rely on are the sources for other extensions. From the MongoMapper extension, I gather that there are two main parts:

  1. The generators (not quite required)

    Here you overwrite DeviseGenerator#(generate_model|inject_devise_content|replace_default_devise_orm) methods.

  2. The "guts" (very much required)

    I'm not quite as sure what's going on here. It seems that there's a lot of boilerplate, with a little bit of custom type-casting, and at the bottom there's a declaration that we'll be using this extension instead of the default ORM.

Is that all there is to it? What am I missing? Can someone explain what happens in "the guts" in a bit more detail?

Are there any simple lint tests to run to ensure full compatibility with Devise?

like image 782
user94154 Avatar asked Dec 02 '11 23:12

user94154


2 Answers

I think the best approach would be to write an orm_adapter adapter for parse resource. It is the real "guts" of devise's integration with various orms.

It is actually very straightforward and includes a test suite you can use. Considering parse_resource is activemodel compliant, the adapter should be as easy as cloning an existing adapter.

Next you will need to hook in the orm_adapter to devise, which is just a clone of one of these files.

like image 96
Tanzeeb Khalili Avatar answered Nov 16 '22 00:11

Tanzeeb Khalili


For a good start you can check out the couchDB extension and the Riak extension as well. You can see that in the Hook module you override anything you want/must for making the Devise to work.

Can you see the class_eval declaration at the bottom? There you extend the class (in your case the ParseResource::Base) with the Devise::Models module, which holds all the necessary methods (including the famous devise method as you can see), and next with the Hooks module if you want to override anything (the MongoMapper extension doen't need to override anything, that's why is not using this method). Thus you must write:

module Devise
  module Orm
    module ParseResource
      module Hooks
        # here you define any overrides
      end
    end
  end
end

ParseResource::Base.class_eval do
  extend Devise::Models
  extend Devise::Orm::ParseResource::Hooks
end

After that you must require 'devise/orm/parse_resource' (assuming you have named the file parse_resource.rb) in your #{Rails.root}/config/initializers/devise.rb initializer. Hope I helped a bit :)

like image 34
Gerry Avatar answered Nov 16 '22 00:11

Gerry