# 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
Implement the class helper methods @param [Class] klass The class to add the helper methods to
# 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 XapianDb.reindex(self, true, changed_attrs: self.previous_changes.keys) 53 XapianDb::DocumentBlueprint.dependencies_for(klass.name, self.previous_changes.keys).each do |dependency| 54 dependency.block.call(self).each{ |model| XapianDb.reindex model, true, changed_attrs: self.previous_changes.keys } 55 end 56 end 57 58 after_commit on: :destroy do 59 XapianDb.delete_doc_with(self.xapian_id) 60 end 61 end 62 63 # Add a method to reindex all models of this class 64 define_singleton_method(:rebuild_xapian_index) do |options={}| 65 options[:primary_key] = klass.primary_key 66 XapianDb.reindex_class(klass, options) 67 end 68 end 69 end
Implement the document helper methods on a module @param [Module] a_module The module to add the helper methods to
# File lib/xapian_db/adapters/active_record_adapter.rb 73 def add_doc_helper_methods_to(a_module) 74 a_module.instance_eval do 75 76 include XapianDb::Utilities 77 78 # Implement access to the model id 79 define_method :id do 80 return @id unless @id.nil? 81 # retrieve the class and id from data 82 klass_name, id = data.split("-") 83 @id = id.to_i 84 end 85 86 # Implement access to the indexed object 87 define_method :indexed_object do 88 return @indexed_object unless @indexed_object.nil? 89 # retrieve the class and id from data 90 klass_name, id = data.split("-") 91 klass = constantize klass_name 92 @indexed_object = klass.find(id.to_i) 93 end 94 end 95 end
# 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
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