# File lib/xapian_db/adapters/active_record_adapter.rb 26 def primary_key_for(klass) 27 klass.primary_key 28 end
class XapianDb::Adapters::ActiveRecordAdapter
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
Public Class Methods
Source
# File lib/xapian_db/adapters/active_record_adapter.rb 32 def add_class_helper_methods_to(klass) 33 34 # Add the helpers from the base class 35 super klass 36 37 klass.instance_eval do 38 # define the method to retrieve a unique key 39 define_method(:xapian_id) do 40 "#{self.class}-#{self.id}" 41 end 42 43 def order_condition(primary_key) 44 '%s.%s' % [self.table_name, primary_key] 45 end 46 end 47 48 klass.class_eval do 49 # add the after commit logic, unless the blueprint has autoindexing turned off 50 if XapianDb::DocumentBlueprint.blueprint_for(klass.name).autoindex? 51 after_commit on: [:create, :update] do 52 changed_attrs = self.previous_changes.keys 53 next if changed_attrs.empty? 54 55 XapianDb.reindex(self, true, changed_attrs:) 56 XapianDb::DocumentBlueprint.dependencies_for(klass.name, changed_attrs).each do |dependency| 57 dependency.block.call(self).each{ |model| XapianDb.reindex(model, true, changed_attrs:) } 58 end 59 end 60 61 after_commit on: :destroy do 62 XapianDb.delete_doc_with(self.xapian_id) 63 end 64 end 65 66 # Add a method to reindex all models of this class 67 define_singleton_method(:rebuild_xapian_index) do |options={}| 68 options[:primary_key] = klass.primary_key 69 XapianDb.reindex_class(klass, options) 70 end 71 end 72 end
Implement the class helper methods @param [Class] klass The class to add the helper methods to
Source
# File lib/xapian_db/adapters/active_record_adapter.rb 76 def add_doc_helper_methods_to(a_module) 77 a_module.instance_eval do 78 79 include XapianDb::Utilities 80 81 # Implement access to the model id 82 define_method :id do 83 return @id unless @id.nil? 84 # retrieve the class and id from data 85 klass_name, id = data.split("-") 86 @id = id.to_i 87 end 88 89 # Implement access to the indexed object 90 define_method :indexed_object do 91 return @indexed_object unless @indexed_object.nil? 92 # retrieve the class and id from data 93 klass_name, id = data.split("-") 94 klass = constantize klass_name 95 @indexed_object = klass.find(id.to_i) 96 end 97 end 98 end
Implement the document helper methods on a module @param [Module] a_module The module to add the helper methods to
Source
# File lib/xapian_db/adapters/active_record_adapter.rb 43 def order_condition(primary_key) 44 '%s.%s' % [self.table_name, primary_key] 45 end
Source
return the name of the primary key column of a class @param [Class] klass the class @return [Symbol] the name of the primary key column