Sha256: 29569ab6bb706e4daab6159092bf2949fd18ce5a61f25537063a6a0eb523fd01

Contents?: true

Size: 1.2 KB

Versions: 18

Compression:

Stored size: 1.2 KB

Contents

module CouchPotato
  module Validation
    module WithValidatable
      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
    end
  end
end

# add [] method to Validatable's implementation of the Errors class
module Validatable
  class Errors
    def [](attribute)
      [on(attribute)].flatten.compact
    end
  end
end

Version data entries

18 entries across 18 versions & 3 rubygems

Version Path
couch_potato-0.6.0 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.7 lib/couch_potato/validation/with_validatable.rb
couch_potato-rails2-0.5.10 lib/couch_potato/validation/with_validatable.rb
couch_potato-rails2-0.5.9 lib/couch_potato/validation/with_validatable.rb
couch_potato-rails2-0.5.8 lib/couch_potato/validation/with_validatable.rb
couch_potato-rails2-0.5.7 lib/couch_potato/validation/with_validatable.rb
couch_potato-rails2-0.5.6 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.6 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.5 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.4 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.3 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.2 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.1 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.5.0 lib/couch_potato/validation/with_validatable.rb
davber_couch_potato-0.4.0 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.4.0 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.3.2 lib/couch_potato/validation/with_validatable.rb
couch_potato-0.3.1 lib/couch_potato/validation/with_validatable.rb