forgeplus/app/models/user_action.rb

145 lines
4.6 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# == 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
belongs_to :user, optional: true
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 "退出登录"
when "DestroyOrganization" then "删除组织"
when "CompetitionInfoCreate" then "创建竞赛"
when "CompetitionInfoUpdate" then "修改竞赛"
when "CompetitionInfoUp" then "上架竞赛"
when "CompetitionInfoDown" then "下架竞赛"
when "Attachment" then "上传附件"
else self.action_type
end
end
def opt_user_name
user = User.find_by(id: self.user_id)
if user.present?
user&.real_name
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/>"
when "DestroyOrganization" then "组织名称:#{organization&.nickname}<br/>组织标识:#{organization&.login}<br/>"
when "CompetitionInfoCreate", "CompetitionInfoUpdate", "CompetitionInfoUp", "CompetitionInfoDown" then "竞赛名称:#{competition_info&.title}<br/>竞赛标识:#{competition_info&.identifier}<br/>"
when 'Attachment'
issue = Issue.find_by_id(attachment.container_id)
if attachment.container_type == "Issue" && issue&.issue_classify == "issue"
return "上传附件issue地址<a href=\"#{Rails.application.config_for(:configuration)['platform_url']}/#{issue&.project&.owner&.login}/#{issue&.project&.identifier}/issues/#{issue&.project_issues_index}\">#{issue&.subject}</a>"
else
return "上传附件地址:<a href=\"#{Rails.application.config_for(:configuration)['platform_url']}/attachments/#{attachment.id}\">#{attachment.title}</a>"
end
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 organization
action_organization = if action_type == "DestroyOrganization" && data_bank.present?
build_mode("Organization")
else
Organization.find_by(id: self.action_id)
end
action_organization
end
def attachment
action_attachment = if action_type == "Attachment" && data_bank.present?
build_mode("Attachment")
else
Attachment.find_by(id: self.action_id)
end
action_attachment
end
def competition_info
competition_info = if action_type.include? "CompetitionInfo"
CompetitionInfo.find_by(id: self.action_id)
else
nil
end
competition_info
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