From bf5269893db7bd341c3f8ba53797bf24d2a349d4 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 17 Jul 2017 09:39:32 +0200 Subject: [PATCH] Allow to configure devise modules In order to be able to configure additional devise modules for the `Alchemy::User` model we introduce `Alchemy.devise_modules`. --- README.md | 28 ++++++++++++++++++++++++++++ app/models/alchemy/user.rb | 15 ++------------- lib/alchemy/devise.rb | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b4dc15b..6466eb6 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,34 @@ $ bundle update alchemy-devise $ bin/rails g alchemy:devise:install ``` +## Devise modules + +Default Devise modules included in `Alchemy::User` model + +- `:database_authenticatable` +- `:trackable` +- `:validatable` +- `:timeoutable` +- `:recoverable` + +If you want to add additional modules into the Alchemy user class append them to `Alchemy.devise_modules` in an initializer in your app. + +### Register additional modules example + +```ruby +# config/initializers/alchemy.rb +Alchemy.devise_modules << :registerable +``` + +### Using old encryption + +If your app uses an old encryption that needs the +devise-encryptable+ gem you also need to load the devise module. + +```ruby +# config/initializers/alchemy.rb +Alchemy.devise_modules << :encryptable +``` + ## Testing If you want to contribute (and you should ^_^), you need to run the tests locally on your machine. diff --git a/app/models/alchemy/user.rb b/app/models/alchemy/user.rb index 2488b40..793e0fc 100644 --- a/app/models/alchemy/user.rb +++ b/app/models/alchemy/user.rb @@ -16,19 +16,8 @@ class User < ActiveRecord::Base :send_credentials, :tag_list ] - DEVISE_MODULES = [ - :database_authenticatable, - :trackable, - :validatable, - :timeoutable, - :recoverable - ] - # If the app uses an old encryption it uses the devise-encryptable gem - # therefore we have to load the devise module - if (::Devise::Models::Encryptable rescue false) - DEVISE_MODULES.push(:encryptable) - end - devise *DEVISE_MODULES + + devise *Alchemy.devise_modules acts_as_taggable acts_as_tagger diff --git a/lib/alchemy/devise.rb b/lib/alchemy/devise.rb index 72ff912..65eb8a2 100644 --- a/lib/alchemy/devise.rb +++ b/lib/alchemy/devise.rb @@ -1,6 +1,41 @@ require "alchemy/devise/engine" module Alchemy + # Devise modules included in +Alchemy::User+ model + # + # === Default modules + # + # [ + #. :database_authenticatable, + # :trackable, + # :validatable, + # :timeoutable, + # :recoverable + #. ] + # + # If you want to add additional modules into the Alchemy user class append + # them to this collection in an initializer in your app. + # + # === Example + # + # # config/initializers/alchemy.rb + # Alchemy.devise_modules << :registerable + # + # If your app uses an old encryption that needs the +devise-encryptable+ gem + # you also need to load the devise module. + # + # Alchemy.devise_modules << :encryptable + # + def self.devise_modules + @devise_modules ||= [ + :database_authenticatable, + :trackable, + :validatable, + :timeoutable, + :recoverable + ] + end + module Devise end end