Module | Mack::Notifier::Validatable |
In: |
lib/mack-notifier/validations.rb
|
Includes the validatable gem into your Notifier. validatable.rubyforge.org
validates_acceptance_of | -> | validates_is_accepted |
Alias the Validatable methods to look like DataMapper methods, if that‘s the kind of thing you‘re used to. :) | ||
validates_confirmation_of | -> | validates_is_confirmed |
validates_format_of | -> | validates_format |
validates_length_of | -> | validates_length |
validates_numericality_of | -> | validates_is_number |
validates_presence_of | -> | validates_present |
Adds common validations to your Mack::Notifier class. These include:
validates_presence_of :to validates_presence_of :from validates_presence_of :subject validates_email_format_of :to validates_email_format_of :from
# File lib/mack-notifier/validations.rb, line 33 33: def common_notifier_validations 34: validates_presence_of :to 35: validates_presence_of :from 36: validates_presence_of :subject 37: validates_email_format_of :to 38: validates_email_format_of :from 39: end
# File lib/mack-notifier/validations.rb, line 52 52: def email_validation_regex 53: regex = "[a-z0-9!\#$\\%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!\#$\\%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\n" 54: /#{regex.strip}/ 55: end
# File lib/mack-notifier/validations.rb, line 7 7: def self.included(base) 8: base.instance_eval do 9: include ::Validatable 10: alias_method "unvalidated_deliver!", "deliver!" 11: end 12: 13: base.class_eval do 14: 15: class << self 16: 17: # Alias the Validatable methods to look like DataMapper methods, 18: # if that's the kind of thing you're used to. :) 19: alias_method :validates_is_accepted, :validates_acceptance_of 20: alias_method :validates_is_confirmed, :validates_confirmation_of 21: alias_method :validates_format, :validates_format_of 22: alias_method :validates_length, :validates_length_of 23: alias_method :validates_is_number, :validates_numericality_of 24: alias_method :validates_present, :validates_presence_of 25: 26: # Adds common validations to your Mack::Notifier class. 27: # These include: 28: # validates_presence_of :to 29: # validates_presence_of :from 30: # validates_presence_of :subject 31: # validates_email_format_of :to 32: # validates_email_format_of :from 33: def common_notifier_validations 34: validates_presence_of :to 35: validates_presence_of :from 36: validates_presence_of :subject 37: validates_email_format_of :to 38: validates_email_format_of :from 39: end 40: 41: # Validates the email format of the column specified against the email_validation_regex method. 42: # This will drill into arrays as well, if that's what your column is. 43: def validates_email_format_of(column, options = {}) 44: options = {:logic => lambda{ 45: [send(column)].flatten.each_with_index do |addr, i| 46: errors.add(column, "[#{addr}] is not valid") unless addr.to_s.downcase.match(self.class.email_validation_regex) 47: end 48: }}.merge(options) 49: validates_each :to, options 50: end 51: 52: def email_validation_regex 53: regex = "[a-z0-9!\#$\\%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!\#$\\%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\n" 54: /#{regex.strip}/ 55: end 56: 57: end # class << self 58: end # class_eval 59: end
Validates the email format of the column specified against the email_validation_regex method. This will drill into arrays as well, if that‘s what your column is.
# File lib/mack-notifier/validations.rb, line 43 43: def validates_email_format_of(column, options = {}) 44: options = {:logic => lambda{ 45: [send(column)].flatten.each_with_index do |addr, i| 46: errors.add(column, "[#{addr}] is not valid") unless addr.to_s.downcase.match(self.class.email_validation_regex) 47: end 48: }}.merge(options) 49: validates_each :to, options 50: end
Returns false if the email is not valid. If the email is valid and an exception is raised when trying to deliver it false is returned and the exception is added to the errors array, with the key :deliver.
# File lib/mack-notifier/validations.rb, line 74 74: def deliver(handler = deliver_with) 75: return false unless self.valid? 76: begin 77: "Mack::Notifier::DeliveryHandlers::#{handler.to_s.camelcase}".constantize.deliver(self) 78: rescue Exception => e 79: self.errors.add(:deliver, e.message) 80: return false 81: end 82: return true 83: end