hashtrain / acts_as_random_id

Rails plugin for generating random id

This URL has Read+Write access

name age message
file .gitignore Thu May 07 04:40:52 -0700 2009 Change gitignore [shaliko]
file MIT-LICENSE Fri Apr 24 12:50:23 -0700 2009 Add options :step and :type. Write new type [shaliko]
file README.rdoc Fri May 08 11:53:44 -0700 2009 Change README [shaliko]
file Rakefile Fri May 08 05:42:58 -0700 2009 Change version [shaliko]
file acts_as_random_id.gemspec Fri May 08 05:42:58 -0700 2009 Change version [shaliko]
file init.rb Thu Apr 23 03:45:00 -0700 2009 Betta version 0.0.1 [shaliko]
file install.rb Thu Apr 23 03:45:00 -0700 2009 Betta version 0.0.1 [shaliko]
directory lib/ Fri May 08 04:39:49 -0700 2009 should_assign_custom_field [olkeene]
directory rdoc/ Thu May 07 04:35:14 -0700 2009 Add gem [shaliko]
directory tasks/ Sun Apr 26 03:12:05 -0700 2009 Version 0.1.2 [shaliko]
directory test/ Fri May 08 04:39:49 -0700 2009 should_assign_custom_field [olkeene]
file uninstall.rb Thu Apr 23 03:45:00 -0700 2009 Betta version 0.0.1 [shaliko]
README.rdoc

ActsAsRandomId

Generating unique random id for ActiveRecord models

Example

  class Comment < ActiveRecord::Base
    acts_as_random_id
  end

  class Group < ActiveRecord::Base
    acts_as_random_id do
      rand(3_14159265) + 1
    end
  end

  class Article < ActiveRecord::Base
    acts_as_random_id :field => :secure_identifier do
      Time.now.to_i
    end
  end

Install

  gem install hashtrain-acts_as_random_id --source http://gems.github.com

Usage

As you know rails generates field ID easy auto_incriment. Thus, there is an opportunity to check how many objects in the DB. Sometimes it’s secret information.

Using the plugin "acts_as_random_id" could generate a unique value id. Generation of ID will be until you will find a unique value ID. Of course you go to the full responsibility for the range.

By default, the plugin "acts_as_random_id" generates a unique id in the range of 1 to 2_147_483_647 (mysql and SQLite type integer)

  class Comment < ActiveRecord::Base
    acts_as_random_id
  end

You can specify a range of transfer method "acts_as_random_id" block where the generator is implemented

  class Group < ActiveRecord::Base
    acts_as_random_id do
      rand(3_14159265) + 1
    end
  end

License

Copyright © 2009 hashtrain.com and author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com), released under the MIT license.