<commit_before>module Cenit
  class ApplicationId
    include Mongoid::Document
    include Mongoid::Timestamps
    include RailsAdmin::Models::Cenit::ApplicationIdAdmin

    field :tenant_id

    field :identifier, type: String
    field :oauth_name, type: String
    field :slug, type: String, default: ''
    field :trusted, type: Boolean

    validates_length_of :oauth_name, within: 6..20, allow_nil: true
    validates_length_of :slug, maximum: 255
    validates_format_of :slug, with: /\A([a-z][a-z0-9]*([_-][a-z0-9]+)?)?\Z/

    validate do
      [:oauth_name, :slug].each do |field|
        if self[field].blank?
          remove_attribute(field)
        else
          self[field] = self[field].strip
          if self.class.where(:id.ne => id).and(field => self[field]).exists?
            errors.add(field, 'is already taken')
          end
        end
      end
      errors.blank?
    end

    before_save do
      self.tenant ||= Cenit::MultiTenancy.tenant_model.current_tenant
      self.identifier ||= (id.to_s + Token.friendly(60))
      if @redirect_uris
        app.configuration['redirect_uris'] = @redirect_uris
        unless app.save
          app.errors.full_messages.each { |error| errors.add(:base, "Invalid configuration: #{error}") }
        end
      end
      errors.blank?
    end

    before_destroy do
      if app<commit_msg>Fix | Apps slugs validation<commit_after>module Cenit
  class ApplicationId
    include Mongoid::Document
    include Mongoid::Timestamps
    include RailsAdmin::Models::Cenit::ApplicationIdAdmin

    field :tenant_id

    field :identifier, type: String
    field :oauth_name, type: String
    field :slug, type: String, default: ''
    field :trusted, type: Boolean

    validates_length_of :oauth_name, within: 6..20, allow_nil: true
    validates_length_of :slug, maximum: 255

    validate do
      [:oauth_name, :slug].each do |field|
        if self[field].blank?
          remove_attribute(field)
        else
          self[field] = self[field].strip
          if self.class.where(:id.ne => id).and(field => self[field]).exists?
            errors.add(field, 'is already taken')
          end
        end
      end
      errors.add(:slug, 'is not valid') unless slug.to_s.underscore == slug
      errors.blank?
    end

    before_save do
      self.tenant ||= Cenit::MultiTenancy.tenant_model.current_tenant
      self.identifier ||= (id.to_s + Token.friendly(60))
      if @redirect_uris
        app.configuration['redirect_uris'] = @redirect_uris
        unless app.save
          app.errors.full_messages.each { |error| errors.add(:base, "Invalid configuration: #{error}") }
        end
      end
      errors.blank?
    end

    before_destroy do
      if app
{'fraction_non_alphanumeric': 0.11233077204537138, 'fraction_numerical': 0.007317965605561654, 'mean_word_length': 2.625994694960212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5981705', 'n_tokens_mistral': 863, 'n_tokens_neox': 848, 'n_words': 195}