aemadrid-sequel_orderable

Update of Aman Gupta's Sequel Orderable Plugin


License
Other
Install
gem install aemadrid-sequel_orderable -v 0.0.4

Documentation

Sequel Orderable Plugin

Allows for model instances to be part of an ordered list, based on a 'position' field in the database.

Basic Usage

Load the plugin into the model:

plugin :orderable

Given:

DB.create_table :items do
  primary_key :id
  varchar :name
  int :position
end

class Item < Sequel::Model(:items)
  plugin :orderable
end

item = Item[1]

The plugin provides access to the previous and next item in the list

item.next
item.prev

And methods to change the position of an item (and update affected items accordingly)

item.move_to(new_position)
item.move_to_top
item.move_to_bottom
item.move_up
item.move_down

Scoping

You can scope the position field by another field.

For example, to allow each user to have their own a distinct orderable list:

DB.create_table :items do
  primary_key :id
  varchar :name
  int :user_id
  int :pos
end

class Item < Sequel::Model(:items)
  plugin :orderable, :field => :pos, :scope => :user_id
end

All the defined methods will operate within the 'user_id' field's scope.

Examples

# Input: irb
require 'sequel'
require 'sequel_orderable'
require 'sequel/extensions/pretty_table' # for pretty prints

DB = Sequel.sqlite

DB.create_table :items do
  primary_key :id
  varchar :name
  int :position
end

class Item < Sequel::Model(:items)
  plugin :orderable
end

Item.create :name => "alice",   :position => 2
Item.create :name => "bob",     :position => 1
Item.create :name => "charlie", :position => 4
Item.create :name => "darwin",  :position => 3

Item.print

Item[:name => "alice"].move_down
Item.print
Item[:name => "darwin"].move_to_top
Item.print
Item[:name => "alice"].next
Item.print
Item[:name => "bob"].prev
Item.print
Item[:name => "darwin"].move_to(3)
Item.print
Item[:name => "bob"].move_to_bottom
Item.print

# Output
>> Item.print
+--+-------+--------+
|id|name   |position|
+--+-------+--------+
| 2|bob    |       1|
| 1|alice  |       2|
| 4|darwin |       3|
| 3|charlie|       4|
+--+-------+--------+
=> nil

>> Item[:name => "alice"].move_down
=> #<Item @values={:name=>"alice", :position=>3, :id=>1}>

>> Item.print
+--+-------+--------+
|id|name   |position|
+--+-------+--------+
| 2|bob    |       1|
| 4|darwin |       2|
| 1|alice  |       3|
| 3|charlie|       4|
+--+-------+--------+
=> nil

>> Item[:name => "darwin"].move_to_top
=> #<Item @values={:name=>"darwin", :position=>1, :id=>4}>

>> Item.print
+--+-------+--------+
|id|name   |position|
+--+-------+--------+
| 4|darwin |       1|
| 2|bob    |       2|
| 1|alice  |       3|
| 3|charlie|       4|
+--+-------+--------+
=> nil

>> Item[:name => "alice"].next
=> #<Item @values={:name=>"charlie", :position=>4, :id=>3}>

>> Item[:name => "bob"].prev
=> #<Item @values={:name=>"darwin", :position=>1, :id=>4}>

>> Item.print
+--+-------+--------+
|id|name   |position|
+--+-------+--------+
| 4|darwin |       1|
| 2|bob    |       2|
| 1|alice  |       3|
| 3|charlie|       4|
+--+-------+--------+
=> nil

Copyright

Copyright © 2007 Sharon Rosner, Wayne E. Seguin, Aman Gupta, Adrian Madrid

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.