Finder Extensions and MySQL Fulltext Indexes 29 Oct 2006
ActiveRecord::Extensions has been turned into a pluggable extension registry to support better finder methods across multiple database platforms more easily. Very little ActiveRecord::Base code actually gets touched now. ActiveRecord will now query an extension registry and if an extension can answer the request it will take the result otherwise it will default to ActiveRecord’s original behavior.

The API interface that developers use is still the same, it’s just the underlying architecture that changed.

Extension Example

Here’s an example which is pulled straight from the source. It adds Range support for ActiveRecord find methods.

# EX: Model.find :all, :conditions=>{ :number => (1..100) }

# EX: Model.find :all, :conditions=>{:created_on => ( Date.today – 30 .. Date.today ) } 
class RangeExt < AbstractExtension NOT\_IN\_RGX = /(.+)\_(ne|not|not\_in)/ def self.process( key, val, caller ) if val.is\_a?( Range ) match\_data = key.to\_s.match( NOT\_IN\_RGX ) key = match\_data.captures

if match\_data fieldname = caller.connection.quote\_column\_name( key ) min = caller.connection.quote( val.first, caller.columns\_hash[ key ] ) max = caller.connection.quote( val.last, caller.columns\_hash[ key ] ) str = "#{caller.table\_name}.#{fieldname} #{match_data ? 'NOT ' : '' } BETWEEN #{min} AND #{max}" return Result.new( str, nil ) end nil end  
end 

register RangeExt, :adapters=>[ :mysql, :postgres ]  

All an extension has to support is the process method which takes the key and val from the :conditions hash, and it gets the context of the caller passed in (the AR model).

Once you create an extension you can add it to the ActiveRecord::Extensions registry by calling the register function on ActiveRecordExtensions. This code is called in the context of ActiveRecord::Extensions so the register function is available. This takes an array of database of adapters which are supported or the symbol :all.

This should open up ActiveRecord to receiving better finder support without having to hack up ActiveRecord itself since it’s pluggable.

MySQL Fulltext Indexes

Fulltext index support has been added for MySQL.

Example

class Project < ActiveRecord::Base fulltext :title, :fields=>%( project\_name project\_description )
end 
Project.find :all, :conditions=>{ :match_title=>'query string' }

Boolean and Query Expansion based fulltext index searches will be supported _(they are not committed to trunk yet)_ . It is not concrete whether these will be supplied via the declaration, the :conditions hash or allow to be supported by both.

This Ain’t Released Yet

Right now these changes exist in trunk. You can download them by visiting the rubyforge project page

These should be out in a 0.0.6 release later this week.


blog comments powered by Disqus