Sha256: 50cbda666d8ffc9eef041eb7615b5df18649d6acc9485b7c70153f5e0c62bace

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

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
        include Tire::Model::Callbacks
        tire do
          index_name options[:index_name] || [klass.model_name.plural, searchkick_env].join("_")
        end

        def reindex
          update_index
        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

          # hack to prevent generator field doesn't exist error
          (options[:suggest] || []).map(&:to_s).each do |field|
            source[field] = "a" if !source[field]
          end

          source.to_json
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
searchkick-0.2.7 lib/searchkick/model.rb
searchkick-0.2.6 lib/searchkick/model.rb
searchkick-0.2.5 lib/searchkick/model.rb