Sha256: 26d12b53ff0c791c4807633fa57544fa1d1e61a7d2f07dc377114c81c3888f29

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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]
          (fields & 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.inject([]) do |memo,f|
          original[f] ? memo | original[f].downcase.split : memo
        end
      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.2 lib/activesearch/mongoid/model.rb