Sha256: 153af12884c23673182bc367fb0679b5f0e537f81d25a332766a081fe3f60d65

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module ActiveSearch
  module Mongoid
    class Model
      include ::Mongoid::Document
      
      field :type, type: String
      field :original_id, type: BSON::ObjectId
      field :keywords
      field :stored, type: Hash, default: {}
      
      index :keywords
      index [:type, :original_id], unique: true
      
      def store_fields(original, fields, options)
        if options && options[:store]
          options[:store].each do |f|
            self.stored[f] = original[f] if original.send("#{f}_changed?")
          end
        end
      end
      
      def refresh_keywords(original, fields)
        self.keywords = fields.select { |f| original.fields[f.to_s]  }.inject([]) do |memo,f|
          
          if original.fields[f.to_s].localized?
            memo += original.send("#{f}_translations").map do |locale,translation|
              translation.downcase.split.map { |word| "#{locale}:#{word}"}
            end.flatten
          else
            original[f] ? memo += original[f].downcase.split : memo
          end
        end
        self.keywords.uniq!
      end
      
      def self.reindex(original, fields, options)
        doc = self.find_or_initialize_by(type: original.class.to_s, original_id: original.id)
        doc.store_fields(original, fields, options)
        doc.refresh_keywords(original, fields)
        doc.save
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activesearch-0.0.3 lib/activesearch/mongoid/model.rb