Module: Mongoid::FTS::Able

Defined in:
lib/mongoid-fts/able.rb

Class Method Summary collapse

Class Method Details

.codeObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid-fts/able.rb', line 4

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

        options[:model] = self

        args.push(options)

        FTS.search(*args, &block)
      end
      alias :search :fts_search

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

        options[:model] = self

        args.push(options)

        FTS.search(*args, &block)
      end
      alias :_search :_fts_search
    end

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

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

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

.included(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mongoid-fts/able.rb', line 42

def Able.included(other)
  unless other.is_a?(Able)
    begin
      super
    ensure
      other.module_eval(&Able.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