<commit_before>module Concerns
  module Searchable
    extend ActiveSupport::Concern

    included do
      has_many :search_indices, as: :searchable, dependent: :destroy

      # TODO: What on earth am I going to do with FlexibleContent?
      #
      # A possible solution could have been to make ContentText searchable,
      # but because that model is not polymorphic, this is not possible.
      #
      # Another solution in the realms of the feasible, with Page as example:
      # In theory, if I could save the ContentText#content value as an index
      # linked to the parent Page object, there would be no problem. I would
      # let ContentText include Concerns::Searchable, and in this class
      # build in an exception to bubble up to the page instance. This is
      # possible through:
      # ContentColumn.find_by(content: ContentText.last).row.rowable
      #
      # The Udongo::Search::Base#indices method already groups by searchable
      # so in this case there wouldn't be duplicates, only additional matches
      # for the searchable because it takes ContentText#content into account.

      # Creates and saves indices for every indicated searchable field.
      # Takes translations into account.
      after_save do
        self.class.searchable_fields_list.each do |key|
          if translatable?
            next unless self.class.translatable_fields_list.include?(key)
            save_translatable_search_index!(key)
          else
            save_search_index!(key)
          end
        end
      end

      def save_search_index!(key)
        value = send(key)
        return if value.blank?
        index = search_indices.find_or_create_by!(locale: Udongo.config.i18n.app.default_locale, key: key)
        index.value = value<commit_msg>add respond_to? check for translatable because not all models (implementing searchable) might be translatable<commit_after>module Concerns
  module Searchable
    extend ActiveSupport::Concern

    included do
      has_many :search_indices, as: :searchable, dependent: :destroy

      # TODO: What on earth am I going to do with FlexibleContent?
      #
      # A possible solution could have been to make ContentText searchable,
      # but because that model is not polymorphic, this is not possible.
      #
      # Another solution in the realms of the feasible, with Page as example:
      # In theory, if I could save the ContentText#content value as an index
      # linked to the parent Page object, there would be no problem. I would
      # let ContentText include Concerns::Searchable, and in this class
      # build in an exception to bubble up to the page instance. This is
      # possible through:
      # ContentColumn.find_by(content: ContentText.last).row.rowable
      #
      # The Udongo::Search::Base#indices method already groups by searchable
      # so in this case there wouldn't be duplicates, only additional matches
      # for the searchable because it takes ContentText#content into account.

      # Creates and saves indices for every indicated searchable field.
      # Takes translations into account.
      after_save do
        self.class.searchable_fields_list.each do |key|
          if respond_to?(:translatable?) && translatable?
            next unless self.class.translatable_fields_list.include?(key)
            save_translatable_search_index!(key)
          else
            save_search_index!(key)
          end
        end
      end

      def save_search_index!(key)
        value = send(key)
        return if value.blank?
        index = search_indices.find_or_create_by!(locale: Udongo.config.i18n.app.default_locale, key: key)
        index.value = value
{'fraction_non_alphanumeric': 0.05906258466540233, 'fraction_numerical': 0.001083717149823896, 'mean_word_length': 2.775051124744376, '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': '343270', 'n_tokens_mistral': 983, 'n_tokens_neox': 951, 'n_words': 369}