Module: Mongoid::FTS::Mixin

Defined in:
lib/mongoid-fts.rb

Class Method Summary collapse

Class Method Details

.codeObject



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/mongoid-fts.rb', line 596

def Mixin.code
  @code ||= proc do
    class << self
      def search(*args, &block)
        options = Map.options_for!(args)

        options[:model] = self

        args.push(options)

        FTS.search(*args, &block)
      end

      def _search(*args, &block)
        options = Map.options_for!(args)

        options[:model] = self

        args.push(options)

        FTS.search(*args, &block)
      end
    end

    after_save do |model|
      FTS::Index.add(model) rescue nil
    end

    after_destroy do |model|
      FTS::Index.remove(model) rescue nil
    end

    has_one(:search_index, :as => :context, :class_name => '::Mongoid::FTS::Index')
  end
end

.included(other) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/mongoid-fts.rb', line 632

def Mixin.included(other)
  unless other.is_a?(Mixin)
    begin
      super
    ensure
      other.module_eval(&Mixin.code)

      FTS.models.dup.each do |model|
        FTS.models.delete(model) if model.name == other.name
      end

      FTS.models.push(other)
      FTS.models.uniq!
    end
  end
end