Module: ActsAsXapian::ActsMethods

Defined in:
lib/acts_as_xapian/base.rb

Overview

Main entry point, add acts_as_xapian to your model.

Instance Method Summary collapse

Instance Method Details

#acts_as_xapian(options) ⇒ Object

See top of this file for docs



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/acts_as_xapian/base.rb', line 42

def acts_as_xapian(options)
  # Give error only on queries if bindings not available
  return unless ActsAsXapian.bindings_available

  include InstanceMethods
  extend ClassMethods

  class_eval('def xapian_boost(term_type, term); 1; end') unless self.instance_methods.include?('xapian_boost')

  # extend has_many && has_many_and_belongs_to associations with our ProxyFinder to get scoped results
  # I've written a small report in the discussion group why this is the proper way of doing this.
  # see here: XXX - write it you lazy douche bag!
  self.reflections.each do |association_name, r|
    # skip if the associated model isn't indexed by acts_as_xapian
    next unless r.klass.respond_to?(:xapian?) && r.klass.xapian?
    # skip all associations except ham and habtm
    next unless [:has_many, :has_many_and_belongs_to_many].include?(r.macro)

    # XXX todo:
    # extend the associated model xapian options with this term:
    # [proxy_reflection.primary_key_name.to_sym, <magically find a free capital letter>, proxy_reflection.primary_key_name]
    # otherways this assumes that the associated & indexed model indexes this kind of term

    # but before you do the above, rewrite the options syntax... wich imho is actually very ugly

    # XXX test this nifty feature on habtm!

    if r.options[:extend].nil?
      r.options[:extend] = [ProxyFinder]
    elsif !r.options[:extend].include?(ProxyFinder)
      r.options[:extend] << ProxyFinder
    end
  end

  cattr_accessor :xapian_options
  self.xapian_options = options

  ActsAsXapian::Index.init(self.class.to_s, options)

  after_save :xapian_mark_needs_index
  after_destroy :xapian_mark_needs_destroy
end