-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: http://thmadb.com/public_svn/plugins/acts_as_tokenizable@77 6dbbdfbd-06da-443d-8068-b1bdd22a71ef
- Loading branch information
smt
committed
Nov 6, 2007
0 parents
commit c8bfc39
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
ActsAsTokenizable | ||
================= | ||
|
||
Add a column named 'token' to your model, as a string: | ||
|
||
class AddIncompleteSignedUpUsers < ActiveRecord::Migration | ||
def self.up | ||
add_column :users, :token, :string | ||
end | ||
|
||
def self.down | ||
remove_column :users, :token | ||
end | ||
end | ||
|
||
Then open up your class, and add the following: | ||
|
||
class User < ActiveRecord::Base | ||
acts_as_tokenizable | ||
end | ||
|
||
Now when a user gets created, he will have a unique 16 character token: | ||
|
||
user = User.create! | ||
user.token # => "3737edeca0f85e76" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
ActiveRecord::Base.instance_eval do | ||
class << self | ||
include ActsAsTokenizable::ClassMethods | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ActsAsTokenizable | ||
module ActsAsTokenizable | ||
module ClassMethods | ||
def acts_as_tokenizable | ||
after_create do |record| | ||
random_string = "" | ||
16.times { |i| random_string << (Kernel.rand(93)+33) } | ||
record.token = value = Digest::MD5.hexdigest(random_string).to_s[0,16] | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require File.dirname(__FILE__) + "/spec_helper" | ||
|
||
# belongs_to :user | ||
# before_validation_on_create :generate_hash!, :set_date | ||
# validates_associated :user | ||
# validates_presence_of :value, :created_at | ||
# | ||
# def generate_hash! | ||
# random_string = String.new | ||
# 16.times { |i| random_string << (Kernel.rand(93)+33) } | ||
# self.value = Digest::MD5.hexdigest(random_string).to_s[0,16] | ||
# end | ||
# | ||
# def set_date | ||
# self.created_at = Time.now | ||
# end | ||
# | ||
# | ||
describe "A", TokenizableModel do | ||
before :each do | ||
@tokenizable_model = TokenizableModel.new | ||
end | ||
|
||
it "should have the token column" do | ||
@tokenizable_model.should respond_to(:token) | ||
end | ||
|
||
it "should not have a token when a new one is instantiated" do | ||
@tokenizable_model.token.should == nil | ||
end | ||
|
||
it "should create a new value when created" do | ||
@tokenizable_model.save! | ||
@tokenizable_model.token.should_not be_nil | ||
end | ||
|
||
it "should assign the value randomly, and return an MD5 hash" do | ||
Kernel.stub!(:rand).and_return 44 | ||
random_string = "" | ||
16.times { |i| random_string << (Kernel.rand(93)+33) } | ||
value = Digest::MD5.hexdigest(random_string).to_s[0,16] | ||
|
||
@tokenizable_model.save! | ||
@tokenizable_model.token.should == value | ||
end | ||
|
||
it "should only return a 16 character long value" do | ||
@tokenizable_model.save! | ||
@tokenizable_model.token.size.should == 16 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--diff |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rubygems' | ||
require 'active_record' | ||
require 'sqlite3' | ||
|
||
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:' | ||
|
||
ActiveRecord::Schema.define do | ||
|
||
create_table :tokenizable_models do |t| | ||
t.column :foo, :string | ||
t.column :token, :string | ||
end | ||
end | ||
|
||
require File.dirname(__FILE__) + "/../lib/acts_as_tokenizable" | ||
require File.dirname(__FILE__) + "/../init" | ||
|
||
|
||
class TokenizableModel < ActiveRecord::Base | ||
acts_as_tokenizable | ||
end | ||
|