<commit_before>module Para
  module ModelHelper
    def attribute_field_mappings_for(component, relation)
      model = relation_klass_for(component, relation)
      model_field_mappings(model).fields
    end

    def model_field_mappings(model, attributes = nil)
      store_key = ['model', 'mappings', model.name.underscore].join(':')

      Para.store[store_key] ||= Para::AttributeFieldMappings.new(
        model, whitelist_attributes: attributes
      )
    end

    def relation_klass_for(component, relation)
      component.class.reflect_on_association(relation).klass
    end

    def field_value_for(object, field_name, type = nil)
      field = model_field_mappings(object.class).field_for(field_name, type)
      value = field.value_for(object)
      excerpt_value_for(value)
    end

    def excerpt_value_for(value)
      return value unless value.kind_of?(String)

      # Check wether the string is HTML or contains whitespace.
      # If so, use `truncate_html` helper, else truncate the string
      # so no-whitespace string (URLs for example) display the beginning of
      # their value, and not "..."
      if value.match(/\s|<|>/)
        truncate_html(value)
      else
        if (truncated = value[0..100]) != value
          "#{ truncated }..."
        else
          value
        end
      end
    end
  end
end<commit_msg>use sanitize instead of truncate in table content<commit_after>module Para
  module ModelHelper
    def attribute_field_mappings_for(component, relation)
      model = relation_klass_for(component, relation)
      model_field_mappings(model).fields
    end

    def model_field_mappings(model, attributes = nil)
      store_key = ['model', 'mappings', model.name.underscore].join(':')

      Para.store[store_key] ||= Para::AttributeFieldMappings.new(
        model, whitelist_attributes: attributes
      )
    end

    def relation_klass_for(component, relation)
      component.class.reflect_on_association(relation).klass
    end

    def field_value_for(object, field_name, type = nil)
      field = model_field_mappings(object.class).field_for(field_name, type)
      value = field.value_for(object)
      excerpt_value_for(value)
    end

    def excerpt_value_for(value)
      return value unless value.kind_of?(String)

      value = sanitize(value, tags: [])
      
      if (truncated = value[0..100]) != value
        "#{ truncated }..."
      else
        value
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09409368635437881, 'fraction_numerical': 0.003258655804480652, 'mean_word_length': 2.9676898222940227, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '3690996', 'n_tokens_mistral': 754, 'n_tokens_neox': 730, 'n_words': 166}