Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Creates tokens for accessing ActiveRecord objects

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 License.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 has_tokens.gemspec
README.md

has_tokens

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.

Install

gem install has_tokens

Active Record Implementation

  1. Create a Migration to add a string "public_token" to your model
  2. 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.

PORO (ruby object) Implementation

class YourHipsterClass
  HasTokens.on self
  has_tokens public: 5
  def initialize
    generate_tokens
  end
end

You know, or whatever.

Authors

coreyhaines and josh cheek

License

Please see License.txt

Something went wrong with that request. Please try again.