<commit_before>class DynamicFieldCheck < ActiveRecord::Base
  CHECK_FORS = ['qualify','validate']
  CHECK_TYPES = ['array_true','array_false','custom_true','custom_false','email','length','min_length','numerical','max_length','split_true','split_false']
  CHECK_TYPES_THAT_REQUIRE_VALUE = ['array_true','array_false','custom_true','custom_false','length','min_length','max_length','split_true','split_false']

  belongs_to :dynamic_field

  named_scope :for_qualification, lambda {{ :conditions => "check_for = 'qualify'" }}
  named_scope :for_validation, lambda {{ :conditions => "check_for = 'validate'" }}

  validates_presence_of :dynamic_field
  validates_inclusion_of :check_for, :in => CHECK_FORS
  validates_inclusion_of :check_type, :in => CHECK_TYPES
  validates_presence_of :check_value, :if => :requires_value


  def check_field(field_value)
    case self.check_type
      when 'array_true'
        !field_value.nil? && !self.value_array.has_item_value?(field_value).nil?
      when 'array_false'
        field_value.nil? || self.value_array.has_item_value?(field_value).nil?
      when 'custom_true'
        !field_value.nil? && !field_value.match(self.check_value).nil?
      when 'custom_false'
        field_value.nil? || field_value.match(self.check_value).nil?
      when 'email'
        !field_value.nil? && !field_value.match(self.check_value || /\A[\w-]+(\.[\w-]+)*@([\w-]+(\.[\w-]+)*?\.[a-zA-Z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?\Z/).nil?
      when 'length'
        !field_value.nil? && field_value.length == self.check_value.to_i
      when 'min_length'
        !field_value.nil? && field_value.length >= self.check_value.to_i
      when 'max_length'
        !field_value.nil? && field_value.length <= self.check_value.to_i
      when 'numerical'
        !field_value.nil? && !field_value.match(self.check_value || /\A[+\-]?\d+\Z/).nil?
      when 'split_true'
        !field_value.nil? && self.value_split.include?(field_value)
      when 'split_false'
        field_value.nil? || !self.value_split.include?(field_value)
    end
  end

  def value_split
    @value_split ||= self.check_value.split('|') if ['split_true','split_false'].include?(self.check_type)
  end

  def value_array
    @value_array ||= DynamicArray.find_by_id(self.check_value) if ['array_true','array_false'].include?(self.check_type)
  end

  def check_message
    if self.custom_message
      return self.custom_message
    else
      msg = case self.check_type
        when 'array_true'   then self.message(:inclusion)
        when 'array_false'  then self.message(:exclusion)
        when 'custom_true'  then self.message(:invalid)
        when 'custom_false' then self.message(:invalid)
        when 'email'        then self.message(:invalid)
        when 'length'       then self.message(:wrong_length)
        when 'min_length'   then self.message(:too_short)
        when 'max_length'   then self.message(:too_long)
        when 'numerical'    then self.message(:not_a_number)
        when 'split_true'   then self.message(:inclusion)
        when 'split_false'  then self.message(:exclusion)<commit_msg>empty/blank custom messages were being presented<commit_after>class DynamicFieldCheck < ActiveRecord::Base
  CHECK_FORS = ['qualify','validate']
  CHECK_TYPES = ['array_true','array_false','custom_true','custom_false','email','length','min_length','numerical','max_length','split_true','split_false']
  CHECK_TYPES_THAT_REQUIRE_VALUE = ['array_true','array_false','custom_true','custom_false','length','min_length','max_length','split_true','split_false']

  belongs_to :dynamic_field

  named_scope :for_qualification, lambda {{ :conditions => "check_for = 'qualify'" }}
  named_scope :for_validation, lambda {{ :conditions => "check_for = 'validate'" }}

  validates_presence_of :dynamic_field
  validates_inclusion_of :check_for, :in => CHECK_FORS
  validates_inclusion_of :check_type, :in => CHECK_TYPES
  validates_presence_of :check_value, :if => :requires_value


  def check_field(field_value)
    case self.check_type
      when 'array_true'
        !field_value.nil? && !self.value_array.has_item_value?(field_value).nil?
      when 'array_false'
        field_value.nil? || self.value_array.has_item_value?(field_value).nil?
      when 'custom_true'
        !field_value.nil? && !field_value.match(self.check_value).nil?
      when 'custom_false'
        field_value.nil? || field_value.match(self.check_value).nil?
      when 'email'
        !field_value.nil? && !field_value.match(self.check_value || /\A[\w-]+(\.[\w-]+)*@([\w-]+(\.[\w-]+)*?\.[a-zA-Z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?\Z/).nil?
      when 'length'
        !field_value.nil? && field_value.length == self.check_value.to_i
      when 'min_length'
        !field_value.nil? && field_value.length >= self.check_value.to_i
      when 'max_length'
        !field_value.nil? && field_value.length <= self.check_value.to_i
      when 'numerical'
        !field_value.nil? && !field_value.match(self.check_value || /\A[+\-]?\d+\Z/).nil?
      when 'split_true'
        !field_value.nil? && self.value_split.include?(field_value)
      when 'split_false'
        field_value.nil? || !self.value_split.include?(field_value)
    end
  end

  def value_split
    @value_split ||= self.check_value.split('|') if ['split_true','split_false'].include?(self.check_type)
  end

  def value_array
    @value_array ||= DynamicArray.find_by_id(self.check_value) if ['array_true','array_false'].include?(self.check_type)
  end

  def check_message
    unless self.custom_message.blank?
      return self.custom_message
    else
      msg = case self.check_type
        when 'array_true'   then self.message(:inclusion)
        when 'array_false'  then self.message(:exclusion)
        when 'custom_true'  then self.message(:invalid)
        when 'custom_false' then self.message(:invalid)
        when 'email'        then self.message(:invalid)
        when 'length'       then self.message(:wrong_length)
        when 'min_length'   then self.message(:too_short)
        when 'max_length'   then self.message(:too_long)
        when 'numerical'    then self.message(:not_a_number)
        when 'split_true'   then self.message(:inclusion)
        when 'split_false'  then self.message(:exclusion)
{'fraction_non_alphanumeric': 0.14944178628389154, 'fraction_numerical': 0.002551834130781499, 'mean_word_length': 3.8915756630265212, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6323577', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2101, 'n_words': 324}