Module: FulltextSearchable::ActiveRecord::ClassMethods

Defined in:
lib/fulltext_searchable/active_record.rb

Overview

ActiveRecord::Baseにextendされるモジュール

Instance Method Summary collapse

Instance Method Details

#fulltext_searchable(columns = [], options = {}, &block) ⇒ Object

全文検索機能を有効にする。

columns

全文検索の対象とするカラムを指定。

例:

fulltext_searchable :title, :body


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fulltext_searchable/active_record.rb', line 26

def fulltext_searchable(columns=[], options={}, &block)
  options = options.symbolize_keys
  cattr_accessor  :fulltext_columns,
    :fulltext_keyword_proc, :fulltext_referenced_columns

  referenced = options.delete(:referenced)
  self.fulltext_columns = Array.wrap(columns)
  self.fulltext_referenced_columns = Array.wrap(referenced) if referenced
  self.fulltext_keyword_proc = block

  class_eval "  has_one :fulltext_index, {\n    :as => :item,\n    :conditions => proc{ {:key => FulltextIndex.create_key(self) } }\n  }\n\n  include FulltextSearchable::ActiveRecord::Behaviors\n\n  after_commit       :save_fulltext_index\n  after_destroy      :destroy_fulltext_index\n  EOV\n  if self.fulltext_referenced_columns\n    class_eval <<-EOV\n      before_save       :check_fulltext_changes\n    EOV\n  end\nend\n"

#fulltext_searchable?Boolean

全文検索対応モデルかどうかを返す。

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/fulltext_searchable/active_record.rb', line 56

def fulltext_searchable?
  self.ancestors.include?(
    ::FulltextSearchable::ActiveRecord::Behaviors)
end