Skip to content

briandoll/acts_as_hashed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

acts_as_hashed
==============

Sometimes it's just not OK to expose content from your model into the URL that fetches it.  By default, ActiveRecord uses the model's :id parameter as its param.  There are lots of various SEO slug URL plugins, too.  This is not one of them.  If you can get away with overriding YourModel#to_param, you should.

acts_as_hashed uses a cryptographic hash function to hash one or more properties of your model, and stores that hash with the model.  You can then look up model instances using that crypto_hash property.

A Sample Use Case: (Scratching my own itch)
I have a Rails application that maintains data that is private to the user that created it.  I would also like to give my users the option of sharing a link to a specific page of their data.  Using IDs and slugs are out, but sharing a link that included an crypto hash, based on one or more properties of that model, would be a nice way to go.


Get it working:
  1) install this plugin
  2) create a migration that adds a "crypto_hash" text field to each model you'll be hashing
  3) rake db:migrate ;)
  4) add the acts_as_hashed class method to your model like so:
       acts_as_hashed :from => :date
         - or -
       acts_as_hashed :from => [:id, :date, :heading]
  5) save a new model and check out its cryptography hash!
  6) ModelName.find_by_crypto_hash("321654987") should do the trick

Notes:
  1) each property passed to the :from option must accept a to_s method call
  2) I'm currently using crc32 hashing, since they make for nice tidy URL elements, which in turn requires zlib

Copyright (c) 2008 Brian Doll, released under the MIT license

About

Provides a unique hash parameter on AR models to use in place of ids or params for private shared URLs

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
MIT-LICENSE

Stars

Watchers

Forks

Packages

No packages published

Languages