After reading Jamis’ post on id partitioning, I
decided to learn how to write a rails plugin so here’s
my first attempt…
Brent
= partitioned_id rails plugin
This library adds a partitioned_id to ease the storage
partitioning of lots of files
in the file system. For an id of 1, a string
“0000/0001” will be returned and for
an id of 12345678, a string “1234/5678” will be
returned.
The default number of digits is 8, the default number
of partitions is 2, and
the default separator is File::SEPARATOR. You can
also set the following in your
rails environment.rb -
Partitioned::DEFAULT_DIGITS
Partitioned::DEFAULT_PARTITIONS
Partitioned::DEFAULT_SEPARATOR
You can also customize these defaults per model (see
example 2 below).
Finally, there is a partitioned_id helper for both
controllers and views which take
at least one parameter an Id and then optionally an
options hash specifying the number
of digits, partitions, and/or separator to be used.
Giving credit where credit is due, this plugin was
inspired by reading:
http://www.37signals.com/svn/archives2/id_partitioning.php
== Usage
=== Example 1
==== Folder.rb model
class Folder < ActiveRecord::Base
acts_as_partitioned_id
end
==== execute ruby script/console
f = Folder.new # =>
#Folder:......
f.save # => true
f.id # => 1
f.partitioned_id # => “0000/0001”
Folder.partitioned_id(123456) # => “0012/3456”
=== Example 2
==== Folder.rb model
class Folder < ActiveRecord::Base
acts_as_partitioned_id :digits => 9,
:partitions => 3,
:separator => ‘|’
end
==== execute ruby script/console
f = Folder.new # =>
#Folder:......
f.save # => true
f.id # => 1
f.partitioned_id # => “000|000|001”
Folder.partitioned_id(123456) # => “000|123|456”
=== Example 3
In your controller…
class MyController < ApplicationController
def index
@partitioned_id = partitioned_id(252525)
end
def other
@partitioned_id = partitioned_id(1234, :digits =>
9, :partitions => 3)
end
end
=== Example 4
In your view
<%= partitioned_id(65432) %>
== Resources
Install
- script/plugin install
svn://rubyforge.org//var/svn/actsaspartid/trunk/partitioned_id
Rubyforge project
RDocs
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around