Since my last post with email validations, I was looking for a solution to integrate it with devise
Sadly for me, devise implements the old way of validating email with regexp :
# From devise gem version 1.1.3 validates_format_of :email, :with => email_regexp, :allow_blank => true |
Well, as I don’t want to hack activemodel’s code, I prefer to skip the validates_format_of method in User class.
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable include SkipableMethod # Devise is great but uses the oldest way with regexp to validate email skip_and_restore_method(:validates_format_of) do devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end # Replace with a validation of my own validates :email, :email => true, :presence => true # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me end |
And then the library :
module SkipableMethod module ClassMethods protected def __skip_method(*args);end def skip_and_restore_method(*method_names,&block) method_names.each do |method_name| instance_eval <<-EVAL alias #{method_name}_skipped #{method_name} alias #{method_name} __skip_method EVAL end yield method_names.each do |method_name| instance_eval <<-EVAL alias #{method_name} #{method_name}_skipped undef #{method_name}_skipped EVAL end end end def self.included(receiver) receiver.extend ClassMethods end end |
TODO :
Some tests

3 comments
1 ping
phaedryx
December 1, 2010 at 18:56 (UTC 2) Link to this comment
Why not just remove the “:validatable” from your devise list and then write your own validations for the User model?
Hallelujah
December 1, 2010 at 23:54 (UTC 2) Link to this comment
Yes it can be a solution but I need to rewrite other validations …
I just want to use my own email validation
Naveed
April 25, 2011 at 15:10 (UTC 2) Link to this comment
thanks it got solution for my problem from :allow_blank => true
Email validation in Ruby On Rails 3 or Active model without regexp « La rolls des blogs
October 26, 2010 at 18:42 (UTC 2) Link to this comment
[...] use this with devise, see my blogpost here : Ruby Rails : How to bypass skip validation in Devise class User < ActiveRecord::Base validates :email, :presence => true, :email => true [...]