<commit_before>module Searchkick
  module Model

    def searchkick(options = {})
      @searchkick_options = options.dup
      @searchkick_env = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
      searchkick_env = @searchkick_env # for class_eval

      class_eval do
        extend Searchkick::Search
        extend Searchkick::Reindex
        include Searchkick::Similar
        include Tire::Model::Search
        tire do
          index_name options[:index_name] || [options[:index_prefix], klass.model_name.plural, searchkick_env].compact.join("_")
        end

        def reindex
          tire.update_index
        end

        unless options[:callbacks] == false
          after_save :reindex
          after_destroy :reindex
        end

        def search_data
          to_hash.reject{|k, v| k == "id" }
        end

        def to_indexed_json
          source = search_data

          # stringify fields
          source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}

          options = self.class.instance_variable_get("@searchkick_options")

          # conversions
          conversions_field = options[:conversions]
          if conversions_field and source[conversions_field]
            source[conversions_field] = source[conversions_field].map{|k, v| {query: k, count: v} }
          end<commit_msg>Added todo<commit_after>module Searchkick
  module Model

    def searchkick(options = {})
      @searchkick_options = options.dup
      @searchkick_env = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
      searchkick_env = @searchkick_env # for class_eval

      class_eval do
        extend Searchkick::Search
        extend Searchkick::Reindex
        include Searchkick::Similar
        include Tire::Model::Search
        tire do
          index_name options[:index_name] || [options[:index_prefix], klass.model_name.plural, searchkick_env].compact.join("_")
        end

        def reindex
          tire.update_index
        end

        unless options[:callbacks] == false
          # TODO ability to temporarily disable
          after_save :reindex
          after_destroy :reindex
        end

        def search_data
          to_hash.reject{|k, v| k == "id" }
        end

        def to_indexed_json
          source = search_data

          # stringify fields
          source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}

          options = self.class.instance_variable_get("@searchkick_options")

          # conversions
          conversions_field = options[:conversions]
          if conversions_field and source[conversions_field]
            source[conversions_field] = source[conversions_field].map{|k, v| {query: k, count: v} }
          end
{'fraction_non_alphanumeric': 0.09577981651376147, 'fraction_numerical': 0.0, 'mean_word_length': 2.348894348894349, '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': '5684422', 'n_tokens_mistral': 814, 'n_tokens_neox': 767, 'n_words': 167}