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

夙愿已清 提交于 2019-12-05 13:07:34

问题


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?


回答1:


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.




回答2:


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 :)



来源:https://stackoverflow.com/questions/8364053/how-to-write-a-devise-extension-to-use-a-custom-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!