3
我有一個在多個模型中重複的方法。我是否應該在多個模型中重複使用該代碼,或者是否有方法將該方法包含在一個地方並使其可用於多個模型?如何重構發生在多個模型中的方法?
# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
嘗試使用顧慮。 http://api.rubyonrails.org/classes/ActiveSupport/Concern.html – Kkulikovskis