<commit_before>class SlugValidator < ActiveModel::EachValidator
  # implement the method called during validation
  def validate_each(record, attribute, value)
    if value.to_s =~ /^done\/(.+)/
      last_part = $1
    elsif value.to_s =~ /\Aforeign-travel-advice\/(.+)/ and record.kind == 'travel-advice'
      last_part = $1
    elsif value.to_s =~ /\Agovernment\/(.+)/ and prefixed_inside_government_format_names.include?(record.kind)
      last_part = $1.gsub('/','-')
    else
      last_part = value
    end
    if record.respond_to?(:kind) and prefixed_inside_government_format_names.include?(record.kind)
      unless value.to_s =~ /\Agovernment\/(.+)/
        record.errors[attribute] << "Inside Government slugs must have a government/ prefix"
      end
    end
    unless ActiveSupport::Inflector.parameterize(last_part.to_s) == last_part.to_s
      record.errors[attribute] << "must be usable in a URL"
    end
  end

  private
    def prefixed_inside_government_format_names
      Artefact::FORMATS_BY_DEFAULT_OWNING_APP["whitehall"] - ["detailed_guide"]
    end
end<commit_msg>Push slug parts into an array and validate each item<commit_after>class SlugValidator < ActiveModel::EachValidator
  # implement the method called during validation
  def validate_each(record, attribute, value)
    if value.to_s =~ /^done\/(.+)/
     parts = [$1]
    elsif value.to_s =~ /\Aforeign-travel-advice\/(.+)/ and record.kind == 'travel-advice'
      parts = [$1]
    elsif value.to_s =~ /\Agovernment\/(.+)/ and prefixed_inside_government_format_names.include?(record.kind)
      parts = $1.split('/')
    else
      parts = [value]
    end
    if record.respond_to?(:kind) and prefixed_inside_government_format_names.include?(record.kind)
      unless value.to_s =~ /\Agovernment\/(.+)/
        record.errors[attribute] << "Inside Government slugs must have a government/ prefix"
      end
    end
    parts.each do |part|
      unless ActiveSupport::Inflector.parameterize(part.to_s) == part.to_s
        record.errors[attribute] << "must be usable in a URL"
      end
    end
  end

  private
    def prefixed_inside_government_format_names
      Artefact::FORMATS_BY_DEFAULT_OWNING_APP["whitehall"] - ["detailed_guide"]
    end
end
{'fraction_non_alphanumeric': 0.12198391420911528, 'fraction_numerical': 0.002680965147453083, 'mean_word_length': 3.3645224171539962, 'pattern_counts': {'":': 0, '<': 13, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6937413', 'n_tokens_mistral': 750, 'n_tokens_neox': 706, 'n_words': 160}