<commit_before># Mixin for parsing govuk_date_fields parameters prior to handling by
# rails.
#
# The govuk_date_fields helper uses the rails default multiparameters
# for dates:
# ```
# my_date_attribute(3i), my_date_attribute(2i), my_date_attribute(1i).
# ````
# These are parsed by rails and raise an `ActiveRecord::MultiparameterAssignmentErrors`
# if the parameters are invalid for a date (e.g. 32-01-2021).
# This would then result in a 500 unless handled so we parse the date
# from the params first and clean invalid date part values before rails
# can raise this error.
#
module MultiparameterAttributeCleaner
  extend ActiveSupport::Concern

  def clean_multiparameter_dates
    return unless params
    find_and_clean_dates(params)
  end

  private

  def find_and_clean_dates(parameters)
    parameters.each_pair do |k, v|
      if v.respond_to?(:each_pair)
        find_and_clean_dates(v)
      elsif k.to_s.include?('(3i)')
        parameters[k] = '' unless (1..31).cover?(v.to_i)
      elsif k.to_s.include?('(2i)')
        parameters[k] = '' unless (1..12).cover?(v.to_i)
      end
    end
  end
end<commit_msg>Prevent unnessesary assignment of empty string<commit_after># Mixin for parsing govuk_date_fields parameters prior to handling by
# rails.
#
# The govuk_date_fields helper uses the rails default multiparameters
# for dates:
# ```
# my_date_attribute(3i), my_date_attribute(2i), my_date_attribute(1i).
# ````
# These are parsed by rails and raise an `ActiveRecord::MultiparameterAssignmentErrors`
# if the parameters are invalid for a date (e.g. 32-01-2021).
# This would then result in a 500 unless handled so we parse the date
# from the params first and clean invalid date part values before rails
# can raise this error.
#
module MultiparameterAttributeCleaner
  extend ActiveSupport::Concern

  def clean_multiparameter_dates
    return unless params
    find_and_clean_dates(params)
  end

  private

  def find_and_clean_dates(parameters)
    parameters.each_pair do |k, v|
      if v.respond_to?(:each_pair)
        find_and_clean_dates(v)
      elsif k.to_s.include?('(3i)')
        parameters[k] = '' unless valid_day_param?(v)
      elsif k.to_s.include?('(2i)')
        parameters[k] = '' unless valid_month_param?(v)
      end
    end
  end

  def valid_day_param?(val)
    val.blank? || (1..31).cover?(val.to_i)
  end

  def valid_month_param?(val)
    val.blank? || (1..12).cover?(val.to_i)
  end
end
{'fraction_non_alphanumeric': 0.09832036050798854, 'fraction_numerical': 0.018025399426464563, 'mean_word_length': 3.9634146341463414, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5449653', 'n_tokens_mistral': 838, 'n_tokens_neox': 811, 'n_words': 230}