Here is a Ruby class:
class User
devise :trackable, :confirmable
end
For most i开发者_如何学运维nstances I want :confirmable
to be present, but for some instances, I would like to remove :confirmable
before instantiation.
QUESTION: How to remove :confirmable
on-the-fly?
I would rather avoid creating a separate class.
devise :confirmable
adds a number of methods to your model, one of which is skip_confirmation!
:
If you don’t want confirmation to be sent on create, neither a code to be generated, call skip_confirmation!
Example:
user = User.new
user.skip_confirmation!
You will need the migrations for both :trackable and :confirmable in any case for your DB.
Wouldn't it be easier to just have :confirmable defined for both cases, but in the case you don't want it, you can automatically confirm the user account from within the controller, after the user is created?
see:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
lines 27..30 contain the before_create and after_create hooks
you'll need to do this modification:
you'll need to override :confirmation_required? , so that it returns true only in the cases where you want a confirmation token to be generated and a confirmation email to be sent. In the case you don't need the confirmation email, you can do a user.confirm! after creating the user account. You could put this in as an additional after_create action.
e.g.
module Devise
module Models
module Confirmable
after_create :confirm! , :if => :confirmation_not_required? # you'll need to define that method
private
def confirmation_required? # overriding the default behavior
your_special_conditions && !confirmed?
end
def confirmation_not_required?
! confirmation_required?
end
end
end
end
Note: Instead of user.confirm! you could also use user.skip_confirmation!
精彩评论