原创作者: hideto
阅读:789次
评论:0条
更新时间:2011-05-26
这个插件让我们给使用它的当前class添加一个使用MD5生成的token
源码也很简单,token_generator.rb:
class Invitation < ActiveRecord::Base include TokenGenerator before_create :set_token end class ImperialInvitation < ActiveRecord::Base include TokenGenerator before_create :set_token private def set_token self.token = generate_token { |token| complies_to_imperial_standards?(token) } end end
源码也很简单,token_generator.rb:
module TokenGenerator def generate_token(size = 12, &validity) begin token = Digest::MD5.hexdigest("#{inspect}#{Time.now}").first(size) end while !validity.call(token) if block_given? token end def set_token self.token = generate_token { |token| self.class.find_by_token(token).nil? } end end
评论 共 0 条 请登录后发表评论