|
- # == Schema Information
- #
- # Table name: user_actions
- #
- # id :integer not null, primary key
- # user_id :integer
- # action_type :string(255)
- # action_id :integer
- # created_at :datetime not null
- # updated_at :datetime not null
- # ip :string(255)
- # data_bank :text(65535)
- # login :string(255)
- # phone :string(255)
- # email :string(255)
- # memo :text(65535)
- #
- # Indexes
- #
- # index_user_actions_on_action_id (action_id)
- # index_user_actions_on_action_type (action_type)
- # index_user_actions_on_ip (ip)
- # index_user_actions_on_user_id (user_id)
- #
-
- class UserAction < ApplicationRecord
-
- after_save :add_user_info
-
- serialize :data_bank, JSON
-
- def action_name
- case action_type
- when "DestroyUser" then "用户注销"
- when "DestroyProject" then "删除项目"
- when "Login" then "登录"
- when "Logout" then "退出登录"
- else self.action_type
- end
- end
-
- def opt_user_name
- user = User.find_by(id: self.user_id)
- if user.present?
- user&.login
- else
- del_user = UserAction.find_by(action_type: "DestroyUser", action_id: self.user_id)
- del_user.present? ? del_user.user.login : "不存在用户:#{user_id}"
- end
- end
-
- def action_info
- case action_type
- when "DestroyUser" then "账号:#{user&.login}<br/>邮箱:#{user&.mail}<br/>手机号:#{user&.phone}<br/>昵称:#{user&.nickname}<br/>"
- when "DestroyProject" then "项目名称:#{project&.name}<br/>项目标识:#{project&.identifier}<br/>"
- else "--"
- end
- end
-
- def user
- action_user = if action_type == "DestroyUser" && data_bank.present?
- build_mode("User")
- else
- User.find_by(id: self.user_id)
- end
- action_user
- end
-
- def project
- action_project = if action_type == "DestroyProject" && data_bank.present?
- build_mode("Project")
- else
- Project.find_by(id: self.action_id)
- end
- action_project
- end
- def build_mode(model_name)
- model = model_name.constantize.new
- model_name.constantize.column_names.each do |col|
- data = self.data_bank.class == String ? JSON.parse(self.data_bank) : self.data_bank
- model[col] = data[col]
- end
- model
- rescue =>err
- return nil
- end
-
- private
- def add_user_info
- if self.login.blank?
- if user.present?
- self.login = user.login
- self.email = user.mail
- self.phone = user.phone
- end
- end
- end
- end
|