Module: Mack::Notifier::Validatable

Defined in:
lib/mack-notifier/validations.rb

Overview

Includes the validatable gem into your Notifier. validatable.rubyforge.org

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mack-notifier/validations.rb', line 7

def self.included(base)
  base.instance_eval do
    include ::Validatable
    alias_method "unvalidated_deliver!", "deliver!"
  end
  
  base.class_eval do

    # Alias the Validatable methods to look like DataMapper methods,
    # if that's the kind of thing you're used to. :)          
    alias_class_method :validates_acceptance_of, :validates_is_accepted
    alias_class_method :validates_confirmation_of, :validates_is_confirmed
    alias_class_method :validates_format_of, :validates_format
    alias_class_method :validates_length_of, :validates_length
    alias_class_method :validates_numericality_of, :validates_is_number
    alias_class_method :validates_presence_of, :validates_present
    
    class << self
      
      # 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
      def common_notifier_validations
        validates_presence_of :to
        validates_presence_of :from
        validates_presence_of :subject
        validates_email_format_of :to
        validates_email_format_of :from
      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.
      def validates_email_format_of(column, options = {})
        options = {:logic => lambda{
          [send(column)].flatten.each_with_index do |addr, i|
            errors.add(column, "[#{addr}] is not valid") unless addr.to_s.downcase.match(self.class.email_validation_regex)
          end
        }}.merge(options)
        validates_each :to, options
      end
      
      def email_validation_regex
        regex = <<-EOF
        [a-z0-9!#$\%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$\%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
        EOF
        /#{regex.strip}/
      end
      
    end # class << self
  end # class_eval
end

Instance Method Details

#deliver(handler = deliver_with) ⇒ Object

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/mack-notifier/validations.rb', line 73

def deliver(handler = deliver_with)
  return false unless self.valid?
  begin
    "Mack::Notifier::DeliveryHandlers::#{handler.to_s.camelcase}".constantize.deliver(self)
  rescue Exception => e
    self.errors.add(:deliver, e.message)
    return false
  end
  return true
end

#deliver!(handler = deliver_with) ⇒ Object

Raises a RuntimeError if the email you are trying to deliver is not valid.



64
65
66
67
# File 'lib/mack-notifier/validations.rb', line 64

def deliver!(handler = deliver_with)
  raise 'Notification is Invalid!' unless self.valid?
  unvalidated_deliver!(handler)
end

#errors_for(name) ⇒ Object



84
85
86
# File 'lib/mack-notifier/validations.rb', line 84

def errors_for(name)
  self.errors.on(name.to_sym)
end