Sha256: 63219c27cd43d18caf14bc5591eefc7a7938491af53055a6af211de8605398e7

Contents?: true

Size: 825 Bytes

Versions: 4

Compression:

Stored size: 825 Bytes

Contents

require 'couch/generators/named_base'

module Couch::Generators
  class ValidationGenerator < NamedBase
    def create_validate_doc_update
      return if File.exists?(File.join(destination_root, "validate_doc_update.js"))
      template "validate_doc_update.js"
    end

    def inject_validations
      inject_into_file "validate_doc_update.js", model_validations, :after => "function (newDoc, oldDoc, userCtx) {\n"
    end

    protected

    def model_validations
      str = <<-STR
  if(newDoc.type == '#{model_name}') {
    // validations for #{model_name}
      STR

      attributes.each do |attribute|
        str << <<-STR
    if (typeof(newDoc['#{attribute}']) === 'undefined') {
      throw({ forbidden: '#{attribute} is required' });
    }
        STR
      end

      str << "  }\n"
      str
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
couch-0.2.0 lib/couch/generators/validation/validation_generator.rb
couch-0.1.2 lib/couch/generators/validation/validation_generator.rb
couch-0.1.1 lib/couch/generators/validation/validation_generator.rb
couch-0.1.0 lib/couch/generators/validation/validation_generator.rb