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