<commit_before>require_dependency 'issue'

module SerialNumberField
  module IssuePatch
    extend ActiveSupport::Concern

    def assign_serial_number!
      serial_number_fields.each do |cf|
        next if assigned_serial_number?(cf)

        target_custom_value = serial_number_custom_values(cf).first
        target_custom_value.update_attributes!(
          :value => cf.format.generate_value(cf, self))
      end
    end

    def assigned_serial_number?(cf)
      serial_number_custom_values(cf).where(
        'custom_values.value is not null').present?
    end

    def serial_number_custom_values(cf)
      CustomValue.where(:custom_field_id => cf.id,
        :customized_type => 'Issue',
        :customized_id => self.id)
    end

    def serial_number_fields
      editable_custom_fields.select do |value|
        value.field_format == SerialNumberField::Format::NAME<commit_msg>Fix not have a CustomValue of serial number Error<commit_after>require_dependency 'issue'

module SerialNumberField
  module IssuePatch
    extend ActiveSupport::Concern

    def assign_serial_number!
      serial_number_fields.each do |cf|
        next if assigned_serial_number?(cf)

        target_custom_value = serial_number_custom_values(cf).first
        new_serial_number = cf.format.generate_value(cf, self)

        # TODO find_or_create_by
        if target_custom_value.present?
          target_custom_value.update_attributes!(
            :value => new_serial_number)
        else
          CustomValue.create!(:custom_field_id => cf.id,
            :customized_type => 'Issue',
            :customized_id => self.id,
            :value => new_serial_number)
        end
      end
    end

    def assigned_serial_number?(cf)
      serial_number_custom_values(cf).where(
        'custom_values.value is not null').present?
    end

    def serial_number_custom_values(cf)
      CustomValue.where(:custom_field_id => cf.id,
        :customized_type => 'Issue',
        :customized_id => self.id)
    end

    def serial_number_fields
      editable_custom_fields.select do |value|
        value.field_format == SerialNumberField::Format::NAME
{'fraction_non_alphanumeric': 0.08057755006986493, 'fraction_numerical': 0.0, 'mean_word_length': 3.0528301886792453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6928543', 'n_tokens_mistral': 634, 'n_tokens_neox': 619, 'n_words': 119}