Skip to content

Instantly share code, notes, and snippets.

View RyanNielson's full-sized avatar

Ryan Nielson RyanNielson

View GitHub Profile
@RyanNielson
RyanNielson / tokenable.rb
Last active December 29, 2015 16:19
Add automatic token generation to a Rails model using concerns. Required a string field called token on the model.
module Tokenable
extend ActiveSupport::Concern
included do
before_validation :generate_token, unless: :persisted?, if: Proc.new { |tokenable| tokenable.token.blank? }
end
protected
def generate_token