Creates tokens for accessing ActiveRecord objects. Allows you to declaratively specify token-based access for your ActiveRecord objects.
Under the covers, has_tokens uses SecureRandom.hex so they'll be all "c07a2". The size is configurable, so if you need a token that is trillions of characters long, you can make that happen, man.
gem install has_tokens
- Create a Migration to add a string "public_token" to your model
- Index that public_token field
class YourSuperActiveRecordClass < ActiveRecord::Base
HasTokens.on self
has_tokens public: 5
before_validation :generate_tokens, on: :create
validates_uniqueness_of :public_token
def self.for_token(token)
find_by_public_token!(token)
end
def to_param
public_token
end
end
Now, your urls will be tokenized "/models/5245c"
Note, if you have a bunch of models, you'll want to generate tokens for each using generate_tokens (and then save the model).
So, you first include has_tokens in your class with HasTokens.on self
. Then, the has_tokens will define your public_token method.
class YourHipsterClass
HasTokens.on self
has_tokens public: 5
def initialize
generate_tokens
end
end
You know, or whatever.
Please see License.txt