<commit_before>class Application
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Slug
    
  #standard fields
  field :name, type: String
  field :description, type: String
  field :ci_identifier, type: String
  #associations
  has_many :application_instances, autosave: true, dependent: :destroy
  #slug
  slug :name do |doc|
    doc.name.downcase.gsub(/[^a-z0-9_-]/,"-")
            .gsub(/--+/, "-")
            .gsub(/^-|-$/,"")
  end

  validates_presence_of :name

  accepts_nested_attributes_for :application_instances, reject_if: lambda{|a| a[:name].blank? },
                                                        allow_destroy: true

  #TODO:
###  delegate :contacts, :contact_ids, :contact_ids=, :contact_relations,
###           :contacts_with_role, :contact_ids_with_role, :contact_ids_with_role=,
###           to: :configuration_item
  
  #THIS IS TEMPORARY
  def contacts_with_role(role=nil); []; end
  def contact_ids_with_role(role=nil); []; end

  def to_s
    name
  end

  def sorted_application_instances
    ary = application_instances
    prod = ary.select{|ai| ai.name == "prod"}
    preprod = ary.select{|ai| ai.name == "preprod"}
    ecole = ary.select{|ai| ai.name == "ecole"}
    others = ary.select{|ai| !ai.name.in? %w(prod preprod ecole) }
    [prod, ecole, preprod, others].flatten.compact
  end<commit_msg>Added some validations on Application#application_instances so that users see more clearly that an app instance with a server and no name cannot be valid<commit_after>class Application
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Slug
    
  #standard fields
  field :name, type: String
  field :description, type: String
  field :ci_identifier, type: String
  #associations
  has_many :application_instances, autosave: true, dependent: :destroy
  #slug
  slug :name do |doc|
    doc.name.downcase.gsub(/[^a-z0-9_-]/,"-")
            .gsub(/--+/, "-")
            .gsub(/^-|-$/,"")
  end

  validates_presence_of :name
  validates_associated :application_instances

  accepts_nested_attributes_for :application_instances, allow_destroy: true,
                                reject_if: lambda{|a| a[:name].blank? && !a[:server_ids].detect(&:present?) }

  #TODO:
###  delegate :contacts, :contact_ids, :contact_ids=, :contact_relations,
###           :contacts_with_role, :contact_ids_with_role, :contact_ids_with_role=,
###           to: :configuration_item
  
  #THIS IS TEMPORARY
  def contacts_with_role(role=nil); []; end
  def contact_ids_with_role(role=nil); []; end

  def to_s
    name
  end

  def sorted_application_instances
    ary = application_instances
    prod = ary.select{|ai| ai.name == "prod"}
    preprod = ary.select{|ai| ai.name == "preprod"}
    ecole = ary.select{|ai| ai.name == "ecole"}
    others = ary.select{|ai| !ai.name.in? %w(prod preprod ecole) }
    [prod, ecole, preprod, others].flatten.compact
  end
{'fraction_non_alphanumeric': 0.1306122448979592, 'fraction_numerical': 0.0013605442176870747, 'mean_word_length': 3.344165435745938, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2067320', 'n_tokens_mistral': 997, 'n_tokens_neox': 964, 'n_words': 222}