Sha256: cfc676ee2571d476cff77aac08738ca63ed3650a0f39127c27814dc505a54a58

Contents?: true

Size: 821 Bytes

Versions: 7

Compression:

Stored size: 821 Bytes

Contents

require 'rid/generators/named_base'

module Rid::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

7 entries across 7 versions & 1 rubygems

Version Path
rid-0.5.2 lib/rid/generators/validation/validation_generator.rb
rid-0.5.1 lib/rid/generators/validation/validation_generator.rb
rid-0.5.0 lib/rid/generators/validation/validation_generator.rb
rid-0.4.1 lib/rid/generators/validation/validation_generator.rb
rid-0.4.0 lib/rid/generators/validation/validation_generator.rb
rid-0.3.1 lib/rid/generators/validation/validation_generator.rb
rid-0.3.0 lib/rid/generators/validation/validation_generator.rb