Class: XapianDb::Adapters::ActiveRecordAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/xapian_db/adapters/active_record_adapter.rb

Overview

Adapter for ActiveRecord. To use it, configure it like this:

XapianDb::Config.setup do |config|
  config.adapter :active_record
end

This adapter does the following:

  • adds the instance method xapian_id to an indexed class

  • adds the class method rebuild_xapian_index to an indexed class

  • adds an after commit block to an indexed class to update the index

  • adds an after destroy block to an indexed class to update the index

  • adds the instance method indexed_object to the module that will be included in every found xapian document

Author:

  • Gernot Kogler

Class Method Summary collapse

Class Method Details

.add_class_helper_methods_to(klass) ⇒ Object

Implement the class helper methods

Parameters:

  • klass (Class)

    The class to add the helper methods to



32
33
34
35
36
37
38
39
40
41
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
# File 'lib/xapian_db/adapters/active_record_adapter.rb', line 32

def add_class_helper_methods_to(klass)

  # Add the helpers from the base class
  super klass

  klass.instance_eval do
    # define the method to retrieve a unique key
    define_method(:xapian_id) do
      "#{self.class}-#{self.id}"
    end

    def order_condition(primary_key)
      '%s.%s' % [self.table_name, primary_key]
    end
  end

  klass.class_eval do
    # add the after commit logic, unless the blueprint has autoindexing turned off
    if XapianDb::DocumentBlueprint.blueprint_for(klass.name).autoindex?
      after_commit do
        unless self.destroyed?
          XapianDb.reindex(self, true, changed_attrs: self.previous_changes.keys)
          XapianDb::DocumentBlueprint.dependencies_for(klass.name, self.previous_changes.keys).each do |dependency|
            dependency.block.call(self).each{ |model| XapianDb.reindex model, true, changed_attrs: self.previous_changes.keys }
          end
        end
      end
    end

    # add the after destroy logic
    after_destroy do
      XapianDb.delete_doc_with(self.xapian_id)
    end

    # Add a method to reindex all models of this class
    define_singleton_method(:rebuild_xapian_index) do |options={}|
      options[:primary_key] = klass.primary_key
      XapianDb.reindex_class(klass, options)
    end
  end
end

.add_doc_helper_methods_to(a_module) ⇒ Object

Implement the document helper methods on a module

Parameters:

  • a_module (Module)

    The module to add the helper methods to



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xapian_db/adapters/active_record_adapter.rb', line 76

def add_doc_helper_methods_to(a_module)
  a_module.instance_eval do

    include XapianDb::Utilities

    # Implement access to the model id
    define_method :id do
      return @id unless @id.nil?
      # retrieve the class and id from data
      klass_name, id = data.split("-")
      @id = id.to_i
    end

    # Implement access to the indexed object
    define_method :indexed_object do
      return @indexed_object unless @indexed_object.nil?
      # retrieve the class and id from data
      klass_name, id = data.split("-")
      klass =  constantize klass_name
      @indexed_object = klass.find(id.to_i)
    end
  end
end

.primary_key_for(klass) ⇒ Symbol

return the name of the primary key column of a class

Parameters:

  • klass (Class)

    the class

Returns:

  • (Symbol)

    the name of the primary key column



26
27
28
# File 'lib/xapian_db/adapters/active_record_adapter.rb', line 26

def primary_key_for(klass)
 klass.primary_key
end