1
好奇如何從一個活動記錄類包含的模塊的實例方法中調用一個類方法。例如,我希望用戶和客戶端模型共享密碼加密的細節和螺栓。從模塊的實例方法中調用一個類方法mixin(rails)
# app/models
class User < ActiveRecord::Base
include Encrypt
end
class Client < ActiveRecord::Base
include Encrypt
end
# app/models/shared/encrypt.rb
module Encrypt
def authenticate
# I want to call the ClassMethods#encrypt_password method when @user.authenticate is run
self.password_crypted == self.encrypt_password(self.password)
end
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def encrypt_password(password)
Digest::SHA1.hexdigest(password)
end
end
end
但是,這失敗了。說實例方法調用它時找不到類方法。我可以調用 User.encrypt_password( '密碼') 但 User.authenticate( '密碼')未能查找方法用戶#ENCRYPT_PASSWORD
有什麼想法?