想要做一個(gè)操作日誌記錄執(zhí)行過的操作,
然後想在model裏邊獲取當(dāng)前操作用戶的ID,進(jìn)行操作的數(shù)據(jù)表名稱,還有操作類型
現(xiàn)在寫了一個(gè)moduel。
module RecordOperation
extend ActiveSupport::Concern
included do
after_update :record_operation
before_destroy :record_operation
after_create :record_operation
def record_operation
end
end
人生最曼妙的風(fēng)景,竟是內(nèi)心的淡定與從容!
devise 文件裡是這麼寫的
To verify if a user is signed in, use the following helper:
user_signed_in?
For the current signed-in user, this helper is available:
current_user
You can access the session for this scope:
user_session
================================
至於在model中,一種直接點(diǎn)的方式是直接在方法中傳入user的實(shí)例或是id
redmine的處理方法我比較喜歡
User.rb
def self.current=(user)
Thread.current[:current_user] = user
end
def self.current
Thread.current[:current_user] ||= User.anonymous
end
然後在登入方法中
User.current = find_current_user
if user && user.is_a?(User)
User.current = user
start_user_session(user)
else
User.current = User.anonymous
end
最終在任何地方都可以呼叫User.current來取得目前登入者的實(shí)例
devise其實(shí)我沒有深入用過,不太清楚他具體實(shí)現(xiàn)。
剛好好奇心起,晚上再回去試試