PageRenderTime 134ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tidbits/active_record/validates_email.rb

https://github.com/jamesarosen/ruby-tidbits
Ruby | 30 lines | 25 code | 5 blank | 0 comment | 1 complexity | 10f42af15cff39a73f9c9663d906a7bf MD5 | raw file
  1. module Tidbits #:nodoc:
  2. module ActiveRecord #:nodoc:
  3. module ValidatesEmail
  4. def self.included(base)
  5. base.extend Tidbits::ActiveRecord::ValidatesEmail::ClassMethods
  6. end
  7. module ClassMethods
  8. def validates_email(*attr_names)
  9. options = attr_names.extract_options!.symbolize_keys!
  10. configuration = { :on => :save, :allow_blank => true, :message => ::ActiveRecord::Errors.default_error_messages[:invalid], :with => Tidbits::ActiveRecord::ValidatesEmail::SIMPLE_EMAIL_ADDRESS_REGEX }
  11. configuration.update(options)
  12. attr_names = [:email] if attr_names.nil? or attr_names.empty?
  13. raise ArgumentError.new(':with must be a regular expression') unless configuration[:with].is_a?(Regexp)
  14. validates_format_of(attr_names, configuration)
  15. end
  16. def valid_tld?(tld)
  17. end
  18. end
  19. unless const_defined?(:SIMPLE_EMAIL_ADDRESS_REGEX)
  20. SIMPLE_EMAIL_ADDRESS_REGEX = /\A[^@\s]+@(?:[[:alnum:]-]+\.)+[[:alpha:]]{2,}\Z/i
  21. end
  22. end
  23. end
  24. end
  25. ::ActiveRecord::Base.send :include, Tidbits::ActiveRecord::ValidatesEmail