lib/activesearch/mongoid.rb in activesearch-0.0.3 vs lib/activesearch/mongoid.rb in activesearch-0.0.4

- old
+ new

@@ -2,28 +2,41 @@ module ActiveSearch # TODO: Wrap this so all engines behave consistently def self.search(text) - Mongoid::Model.where(:keywords.in => text.split + text.split.map { |word| "#{I18n.locale}:#{word}"}) + text = text.split(/\s+/) + Mongoid::Model.where(:_keywords.in => text + text.map { |word| "#{I18n.locale}:#{word}"}) end module Mongoid def self.included(base) base.extend ClassMethods end + protected + def reindex + ActiveSearch::Mongoid::Model.reindex(self, self.class.search_fields, self.class.search_options) + end + + def deindex + ActiveSearch::Mongoid::Model.deindex(self) + end + module ClassMethods - def search_on(*fields) - # TODO: Use inheritable class variables, so ActiveSearch::Mongoid::Model can get fields and options from there - self.class_eval <<-EOV - after_save do - fields = #{fields} - options = fields.pop if fields.last.is_a?(Hash) - return unless fields.any? { |f| self.send("\#{f}_changed?") } - ActiveSearch::Mongoid::Model.reindex(self, fields, options) - end - EOV + def search_options + @search_options + end + + def search_fields + @search_fields + end + + def search_by(*fields) + @search_options = fields.pop if fields.last.is_a?(Hash) + @search_fields = fields + self.after_save :reindex + self.after_destroy :deindex end end end end \ No newline at end of file