Aaron2Ti / records_sequence

This URL has Read+Write access

records_sequence / README
100644 49 lines (36 sloc) 1.407 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
== Author
  Aaron Tian
 
== Contact
  Aaron2Ti@gmail.com
 
RecordsSequence
=============
  This plugin adds 'next/previous' methods to every ActiveRecord object.
  Using it you could easily find next/previous object accroding your requirements.
 
 
INSTALL
============
This plugin requires Rails 2.1 currently. Install easily by below command:
  ruby script/plugin install git://github.com/Aaron2Ti/records_sequence.git
 
 
USAGE
============
After installed, this plugin would mixin two methods(next and previous) to
every ActiveRecord object. So it's cool to do:
 
  class User < ActiveRecord::Base
  end
 
  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,
  # which 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 )
 
Try Yourself!
 
Copyright (c) 2008 [Aaron Tian Email: Aaron2Ti@gmail.com],
released under the MIT license