Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Devise config values within model

I have two models using Devise. Everything's properly scoped and working fine.

Now I want to set several config values differently for each model. For example, in one model I want to set allow_unconfirmed_access_for for a few days, and in the other I do not want to allow any such unconfirmed access.

This is possible per Devise's own documentation at the top of config/initializers/devise.rb...

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.

What's the syntax necessary to set variables within a model, as opposed to the initializer? I currently have this inside my model...

# Model Foo
Devise.setup do |config|
  config.allow_unconfirmed_access_for = 2.days
end

# Model Bar
Devise.setup do |config|
  config.allow_unconfirmed_access_for = 0.days
end

and it does not appear to be working...

like image 411
Frank Koehl Avatar asked Apr 07 '14 20:04

Frank Koehl


1 Answers

Following the advice from a related post, I learned that these values are placed inside of the model to which Devise is applied. So we're just dealing with normal class variables.

I was able to get the desired results by setting the variables like so...

class User < ActiveRecord::Base

  devise  :database_authenticatable, :registerable, :rememberable,
          :trackable, :validatable, :timeoutable,
          :confirmable, :recoverable, :lockable

  # override Devise default config settings
  self.allow_unconfirmed_access_for = 2.days

  # ...
like image 84
Frank Koehl Avatar answered Oct 04 '22 20:10

Frank Koehl