<commit_before>module Setup
  module SnippetCode
    extend ActiveSupport::Concern

    include ShareWithBindings

    included do

      binding_belongs_to :snippet, class_name: Setup::Snippet.to_s, inverse_of: nil

      before_save do
        configure_snippet
        if snippet_ref.changed? && !snippet_ref.save
          snippet_ref.errors.full_messages.each do |msg|
            errors.add(:code, msg)
          end
        end
        errors.blank?
      end
    end

    def configure_snippet
      if snippet_ref.new_record?
        snippet_ref.namespace = namespace
        name = snippet_name
        i = 0
        while Setup::Snippet.where(tenant: Cenit::MultiTenancy.tenant_model.current,
                                   namespace: namespace,
                                   name: name).exists?
          name = snippet_name("(#{i += 1})")
        end
        snippet_ref.name = name
      elsif snippet_ref.changed_attributes.key?('code') &&
            snippet_ref.tenant != Cenit::MultiTenancy.tenant_model.current
        self.snippet = Setup::Snippet.new(snippet_ref.attributes.reject { |key, _| %w(_id origin).include?(key) })
        name = snippet_ref.name
        i = 0
        while Setup::Snippet.where(namespace: namespace,
                                   name: name).exists?
          name = snippet_name("(#{i += 1})")
        end
        snippet_ref.name = name
      end
    end

    def snippet_ref
      self.snippet ||= Setup::Snippet.new
      snippet.instance_variable_set(:@snippet_code_owner, self)
      snippet
    end

    def snippet_name(suffix = '')
      name = code_name
      if (ext = ".#{code_extension.to_s.strip}".squeeze('.')).length > 1
        name = name.to(name.rindex('.') - 1) if name.ends_with?(ext)
      else
        ext = ''
      end
      name + suffix + ext<commit_msg>Fixing snippet name conflict testing<commit_after>module Setup
  module SnippetCode
    extend ActiveSupport::Concern

    include ShareWithBindings

    included do

      binding_belongs_to :snippet, class_name: Setup::Snippet.to_s, inverse_of: nil

      before_save do
        configure_snippet
        if snippet_ref.changed? && !snippet_ref.save
          snippet_ref.errors.full_messages.each do |msg|
            errors.add(:code, msg)
          end
        end
        errors.blank?
      end
    end

    def configure_snippet
      if snippet_ref.new_record?
        snippet_ref.namespace = namespace
        name = snippet_name
        i = 0
        while Setup::Snippet.where(namespace: namespace, name: name).exists?
          name = snippet_name("(#{i += 1})")
        end
        snippet_ref.name = name
      elsif snippet_ref.changed_attributes.key?('code') &&
            snippet_ref.tenant != Cenit::MultiTenancy.tenant_model.current
        self.snippet = Setup::Snippet.new(snippet_ref.attributes.reject { |key, _| %w(_id origin).include?(key) })
        name = snippet_ref.name
        i = 0
        while Setup::Snippet.where(namespace: namespace, name: name).exists?
          name = snippet_name("(#{i += 1})")
        end
        snippet_ref.name = name
      end
    end

    def snippet_ref
      self.snippet ||= Setup::Snippet.new
      snippet.instance_variable_set(:@snippet_code_owner, self)
      snippet
    end

    def snippet_name(suffix = '')
      name = code_name
      if (ext = ".#{code_extension.to_s.strip}".squeeze('.')).length > 1
        name = name.to(name.rindex('.') - 1) if name.ends_with?(ext)
      else
        ext = ''
      end
      name + suffix + ext
{'fraction_non_alphanumeric': 0.10467079347214406, 'fraction_numerical': 0.0033764772087788407, 'mean_word_length': 2.1184210526315788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4531501', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1068, 'n_words': 217}