<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
        if snippet_ref.new_record?
          snippet_ref.namespace = namespace
          name = snippet_name
          i = 0
          while Setup::Snippet.where(tenant: Cenit::MultiTenancy.tenant_model.current, name: name).exists?
            name = snippet_name("(#{i += 1})")
          end
          snippet_ref.name = name
        elsif snippet_ref.changed_attributes.has_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(tenant: Cenit::MultiTenancy.tenant_model.current, name: name).exists?
            name = snippet_name("(#{i += 1})")
          end
          snippet_ref.name = name
        end
        unless snippet_ref.save
          snippet_ref.errors.full_messages.each do |msg|
            errors.add(:code, msg)
          end
        end
        errors.blank?
      end
    end

    def snippet_ref
      self.snippet ||= Setup::Snippet.new
    end

    def snippet_name(suffix = '')
      name = code_name
      if (ext = ".#{code_extension.to_s.strip}".squeeze('.')).length > 1
        if name.ends_with?(ext)
          name = name.to(name.rindex('.') - 1)
        end
      else
        ext = ''
      end
      name + suffix + ext
    end

    def code_name
      name.to_s.to_file_name
    end

    def code_extension
      nil
    end

    def code
      snippet_ref.code
    end

    def code=(code)
      snippet_ref.code = code
    end

    #TODO Only for legacy codes, remove after migration

    def read_attribute(name)
      if name.to_s == 'code'
        code
      elsif name.to_s == self.class.legacy_code_attribute.to_s
        if respond_to?(name)
          send(name)
        else
          code
        end
      else
        super
      end
    end

    module ClassMethods

      def instantiate(attrs = nil, selected_fields = nil)
        record = super
        if record.snippet.nil? && (legacy_code = record.attributes[legacy_code_attribute])
          record.code = legacy_code
        end
        record
      end

      def legacy_code_attribute(*args)
        if args.length == 0
          @legacy_code_attribute
        else
          @legacy_code_attribute = args[0].to_s
        end
      end
    end
  end
end<commit_msg>Tuning snippet legacy code for migration<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
        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.has_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(tenant: Cenit::MultiTenancy.tenant_model.current,
                                     namespace: namespace,
                                     name: name).exists?
            name = snippet_name("(#{i += 1})")
          end
          snippet_ref.name = name
        end
        unless snippet_ref.save
          snippet_ref.errors.full_messages.each do |msg|
            errors.add(:code, msg)
          end
        end
        errors.blank?
      end
    end

    def snippet_ref
      self.snippet ||= Setup::Snippet.new
    end

    def snippet_name(suffix = '')
      name = code_name
      if (ext = ".#{code_extension.to_s.strip}".squeeze('.')).length > 1
        if name.ends_with?(ext)
          name = name.to(name.rindex('.') - 1)
        end
      else
        ext = ''
      end
      name + suffix + ext
    end

    def code_name
      name.to_s.to_file_name
    end

    def code_extension
      nil
    end

    def code
      snippet_ref.code
    end

    def code=(code)
      snippet_ref.code = code
    end

    #TODO Only for legacy codes, remove after migration

    def read_attribute(name)
      if name.to_s == self.class.legacy_code_attribute.to_s
        if respond_to?(name)
          send(name)
        else
          code
        end
      elsif name.to_s == 'code'
        code
      else
        super
      end
    end

    module ClassMethods

      def instantiate(attrs = nil, selected_fields = nil)
        record = super
        if record.snippet.nil? && (legacy_code = record.attributes[legacy_code_attribute])
          if record.respond_to?(assign_legacy_code = "#{legacy_code_attribute}=")
            record.send(assign_legacy_code, legacy_code)
          else
            record.code = legacy_code
          end
        end
        record
      end

      def legacy_code_attribute(*args)
        if args.length == 0
          @legacy_code_attribute
        else
          @legacy_code_attribute = args[0].to_s
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08736157496923888, 'fraction_numerical': 0.0028124450694322375, 'mean_word_length': 1.783757338551859, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4531502', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1691, 'n_words': 367}