ploymorphic_wuid

Say you want you set an uniq id to an object (whatever it is), and get the object through the id, for example get an object’s versions:

/wuid/:id/versions

return the object through ploymorphic:

obj = Wuid.find(params[:id]).wuidable

Migration

class CreateWuids < ActiveRecord::Migration

def self.up
  create_table :wuids do |t|
    t.integer :wuidable_id
    t.string  :wuidable_type
    t.string  :rand_token
    t.integer :reference_to #optional
    t.timestamps
  end

  add_index :wuids, :rand_token, :unique => true, :name => 'token_index'
end

def self.down
  drop_table :wuids
end

end

Settings

in the environment.rb
  config.gem "ploymorphic_wuid"

in your model

  class Book < ActiveRecord::Base
    acts_as_wuid 
  end

 that's all

Usage

return the object:

  obj = Wuid.find(params[:wuid]).wuidable

now you can both pass in an id or an rand_token to get the corresponding object

get an object's wuid:

  obj.wid

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 tim.teng. See LICENSE for details.