ActsAsHashed
ActsAsHashed is helpful to set a hash_code column based on SecureRandom.hex(16).
Installation
Add this line to your application's Gemfile:
gem 'acts_as_hashed'
And then execute:
$ bundle
Or install it yourself as:
$ gem install acts_as_hashed
Usage
Create your migrations for the desired models
$ rails g migration AddHashedCodeToOrders hashed_code:string
or
class AddHashedCodeToOrders < ActiveRecord::Migration
def self.up
add_column :orders, :hashed_code, :string
end
def self.down
remove_column :orders, :hashed_code, :string
end
end
Usage
In your model:
class Order < ActiveRecord::Base
acts_as_hashed
def to_param
hashed_code
end
...
end
or you can overwrite the method that will generate the hash.
class Order < ActiveRecord::Base
acts_as_hashed
class << self
def friendly_token
SecureRandom.hex(5)
end
end
end
If you are adding acts_as_hashed after having some records on your database you need too run the code above to update the records:
Order.update_missing_hashed_code
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Credits
ActsAsHashed is maintained and funded by HE:labs. Thank you to all the contributors.
License
ActsAsHashed is Copyright © 2012-2014 HE:labs. It is free software, and may be redistributed under the terms specified in the LICENSE file.