2017-06-23 21:19:08 +08:00
|
|
|
class UserProject < ApplicationRecord
|
2016-02-12 23:52:43 +08:00
|
|
|
enum role: { owner: 0, normal_user: 1, technician: 2, viewer: 3 }
|
|
|
|
|
|
|
|
validates :role, presence: true
|
|
|
|
validates :user, presence: true
|
|
|
|
validates :project, presence: true
|
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :user, inverse_of: :user_projects, optional: true
|
|
|
|
belongs_to :assigned_by,
|
|
|
|
foreign_key: 'assigned_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :project, inverse_of: :user_projects, optional: true
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
before_destroy :destroy_associations
|
|
|
|
|
2018-04-18 22:47:52 +08:00
|
|
|
after_commit do
|
2018-04-23 18:26:21 +08:00
|
|
|
Views::Datatables::DatatablesReport.refresh_materialized_view
|
2018-04-18 22:47:52 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def role_str
|
|
|
|
I18n.t("user_projects.enums.role.#{role.to_s}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_associations
|
|
|
|
# Destroy the user from all project's modules
|
2016-07-22 21:36:48 +08:00
|
|
|
project.project_my_modules.each do |my_module|
|
2016-02-12 23:52:43 +08:00
|
|
|
um2 = (my_module.user_my_modules.select { |um| um.user == self.user }).first
|
|
|
|
if um2.present?
|
|
|
|
um2.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|