Module: CouchPotato::Validation::WithValidatable

Defined in:
lib/couch_potato/validation/with_validatable.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/couch_potato/validation/with_validatable.rb', line 4

def self.included(base)
  begin
    require 'validatable'
  rescue LoadError
    puts "Please install the gem validatable using 'gem install validatable'"
    raise
  end
  base.send :include, ::Validatable
  base.class_eval do
    # Override the validate method to first run before_validation callback
    def valid_with_before_validation_callback?
      errors.clear
      run_callbacks :validation do
        before_validation_errors = errors.errors.dup
        valid_without_before_validation_callback?
        before_validation_errors.each do |k, v|
          v.each {|message| errors.add(k, message)}
        end
      end
      errors.empty?
    end
    
    alias_method :valid_without_before_validation_callback?, :valid?
    alias_method :valid?, :valid_with_before_validation_callback?
  end
end