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
# 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?
      errors.clear
      run_callbacks :before_validation
      before_validation_errors = errors.errors.dup
      super
      before_validation_errors.each do |k, v|
        v.each {|message| errors.add(k, message)}
      end
      errors.empty?
    end
  end
end