Module: WingedCouch::Models::Validation

Included in:
WingedCouch::Model
Defined in:
lib/winged_couch/models/validation.rb

Overview

Module for defining attribute validations

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


7
8
9
# File 'lib/winged_couch/models/validation.rb', line 7

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#errorsObject

Returns list of errors

If WingedCouch runs in Rails context,

returns ActiveModel::Errors collection

If WingedCouch run not in Rails context

return hash of errors in format
{ attribute_as_a_symbol => ['error message 1'] }

One important thing about CouchDB-based validation: CouchDB always returns only the first found error, So if you have two different validations, and both of them should add errors on object, only the first one will display an error However, after fixing first broken field, the second will show an error

So, if you have Rails, prefer ActiveModel::Validations over native CouchDB validations

Returns:

  • ActiveModel::Errors

  • Hash


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/winged_couch/models/validation.rb', line 33

def errors
  if self.class.has_active_model?
    super.tap do |active_model_errors|
      parsed_native_errors.each do |attribute, message|
        unless active_model_errors[attribute].include?(message)
          active_model_errors.add(attribute, message)
        end
      end
    end
  else
    parsed_native_errors
  end
end