Created
October 30, 2014 12:22
-
-
Save jeffkreeftmeijer/13759d1c49d82febc720 to your computer and use it in GitHub Desktop.
Set a random token after model initialization http://shorts.jeffkreeftmeijer.com/2014/set-a-random-token-after-model-initialization/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Concerns::Token | |
def self.included(base) | |
base.after_initialize(:set_token) | |
end | |
def set_token | |
self.token = SecureRandom.hex if new_record? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../../test_helper' | |
describe Concerns::Token do | |
[Session, Discussion, Comment, Notification].each do |model| | |
describe "for a #{model}" do | |
it "automatically generates a token" do | |
model.new.token.length.should == 32 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment