2008/06/18

Plugin released - records_sequence

In my last post, I wanted to have some flexible next or previous functions to find a object's neighbors. To fulfill that I just finished a plugin called "records_sequence" which could be found here.

After installed, records_sequence could mixin two methods "next/previous" to every ActiveRecord objects.

INSTALL
This plugin requires Rails 2.1+ currently. Install it by below command:
ruby script/plugin install git://github.com/Aaron2Ti/records_sequence.git

USAGE
After installed, your models could:
foo = User.first
bar = foo.next
foo_neighbour = foo.previous # returns nil if foo has no previous neighbour

#The sequence's sorted column is defined by the new option :sorted_by,
# default is the 'id' column.
foo = User.last
pre_foo_sorted_by_id = foo.previous
pre_foo_sorted_by_age = foo.previous(:sorted_by => 'age')

#Also works fine with most other ActiveRecord's find options:
foo = User.find 30
foo.next(:conditions => ['age < ?', 20]) foo.previous(:offset => 2)
foo.next(:sorted_by => 'address', :order => 'name DESC, age')
foo.previous( :sorted_by => 'age', :conditions => ['age < ?', 20],
:order => 'name DESC, address', :offset => 2 )