Skip to content

Commit

Permalink
adding acts_as_tokenizable plugin
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README
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"

6 changes: 6 additions & 0 deletions init.rb
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
13 changes: 13 additions & 0 deletions lib/acts_as_tokenizable.rb
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
51 changes: 51 additions & 0 deletions spec/acts_as_tokenizable_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--diff
22 changes: 22 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit c8bfc39

Please sign in to comment.