Module: Mongoid::FTS::Mixin

Defined in:
lib/mongoid-fts.rb

Class Method Summary collapse

Class Method Details

.codeObject



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/mongoid-fts.rb', line 617

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



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/mongoid-fts.rb', line 653

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