gitlink-forgeplus/app/models/user_action.rb

74 lines
1.6 KiB
Ruby

# == 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
before_create :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 action_data
case action_type
when "DestroyUser" then build_mode("User")
when "DestroyProject" then build_mode("Project")
else nil
end
end
def user
action_user = User.find_by(id: self.user_id)
if action_user.blank?
action_user = self.action_data
end
action_user
end
def build_mode(model_name)
model = model_name.constantize.new
model_name.constantize.column_names.each do |col|
model[col] = self.data_bank[col]
end
model
end
private
def add_user_info
if user.present?
self.login = user.login
self.email = user.mail
self.phone = user.phone
end
end
end