This gem provides a has_ helper to populate a field with an obscured version of a model id.
Why Obscure an ID?
Add to your Gemfile:
Install the gem:
Generate the default initializer:
rails generate obscured_id:initializer
Generate and run a migation for the model you wish to have an obscured id:
rails generate obscured_id:migration MODEL_NAME rake db:migrate
Add the obscured_id declaration to your model:
Populate your database with the obscured id:
The following options can be configured with a Rails initializer:
obscured_id_secret_token: This is the secret token used when generating the obscured id. It is strongly recommended you run the obscured_id:initializer generator to set this, otherwise the default is nil.
obscured_id_field: This is the field in which you wish to store the obscured id. Default is "obscured_id"
record_obscured_ids: If set to false, the obscured_id will not automatically be set. Default is true.
This creates an initializer with a random secret key for obscuring the model's id. While it is possible to run this plugin without a secret key, it's not recommended.
obscured_id:migration MODEL_NAME (--field=FIELD_NAME)
This creates a migration for a model with the name. The field option lets you override the default field name "obscured_id". If you override the field name, be sure to set the field option in your configuration or
This will populate a model with an obscured id without recording timestamps. NOTE: that this will populate your model's obscured ID field regardless of your record_obscured_ids configuration or auto option in the
When added to your model definition,
has_obscured_id declaration configures your model to be obscure id capable, adding the methods described below.
The following options are available:
field: This overides
secret_token: This overides
auto: If set to false, the obscured_id will not automatically be set.
class ThingWithObscuredId < ActiveRecord::Base has_obscured_id end class ThingWithConfiguredObscuredId < ActiveRecord::Base has_obscured_id :field => :hidden_id, :secret_token => "ee71c598567e9d469738b7ec64213b8c", :auto => false end
set_obscured_id: Force sets the instance's obscured ID.
safe_set_obscured_id: Sets an instance's obscured ID only if updating is turned on.
set_obscured_ids: Force populates obscured ids for all records. Returns an array of integers, where the first is the count of succesful updates, and the second is the count of failed updates.
You can turn updating off:
ActiveRecord::Base.record_obscured_ids = false
And turn it back on:
ActiveRecord::Base.record_obscured_ids = true
$ bundle install $ rspec spec
Copyright (c) 2013 Larry Halff, released under the MIT license