2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-23 22:48:32 -04:00
|
|
|
class GroupUser < ActiveRecord::Base
|
|
|
|
belongs_to :group, counter_cache: "user_count"
|
|
|
|
belongs_to :user
|
2015-04-09 22:17:28 -04:00
|
|
|
|
|
|
|
after_save :update_title
|
2018-09-20 22:06:08 -04:00
|
|
|
after_destroy :grant_other_available_title
|
2015-04-09 22:17:28 -04:00
|
|
|
|
|
|
|
after_save :set_primary_group
|
2017-11-23 12:58:46 -05:00
|
|
|
after_destroy :remove_primary_group, :recalculate_trust_level
|
2015-04-09 22:17:28 -04:00
|
|
|
|
2017-04-20 15:47:25 -04:00
|
|
|
before_create :set_notification_level
|
2015-09-01 16:52:05 -04:00
|
|
|
after_save :grant_trust_level
|
|
|
|
|
2016-07-06 15:56:40 -04:00
|
|
|
def self.notification_levels
|
|
|
|
NotificationLevels.all
|
|
|
|
end
|
|
|
|
|
2015-04-09 22:17:28 -04:00
|
|
|
protected
|
|
|
|
|
2017-04-20 15:47:25 -04:00
|
|
|
def set_notification_level
|
|
|
|
self.notification_level = group&.default_notification_level || 3
|
|
|
|
end
|
|
|
|
|
2015-04-09 22:17:28 -04:00
|
|
|
def set_primary_group
|
2018-11-13 19:28:41 -05:00
|
|
|
user.update!(primary_group: group) if group.primary_group
|
2015-04-09 22:17:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_primary_group
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec("
|
2015-04-09 22:17:28 -04:00
|
|
|
UPDATE users
|
|
|
|
SET primary_group_id = NULL
|
|
|
|
WHERE id = :user_id AND primary_group_id = :id",
|
|
|
|
id: group.id, user_id: user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-09-20 22:06:08 -04:00
|
|
|
def grant_other_available_title
|
|
|
|
if group.title.present? && group.title == user.title
|
2020-05-24 16:12:55 -04:00
|
|
|
user.update_attribute(:title, user.next_best_title)
|
2015-04-09 22:17:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_title
|
|
|
|
if group.title.present?
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec("
|
2015-04-09 22:17:28 -04:00
|
|
|
UPDATE users SET title = :title
|
|
|
|
WHERE (title IS NULL OR title = '') AND id = :id",
|
|
|
|
id: user_id, title: group.title
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-09-01 16:52:05 -04:00
|
|
|
|
|
|
|
def grant_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2017-11-28 19:01:13 -05:00
|
|
|
|
2017-03-02 13:16:01 -05:00
|
|
|
TrustLevelGranter.grant(group.grant_trust_level, user)
|
2015-09-01 16:52:05 -04:00
|
|
|
end
|
2017-11-23 12:58:46 -05:00
|
|
|
|
|
|
|
def recalculate_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
|
|
|
|
2018-04-10 02:19:08 -04:00
|
|
|
Promotion.recalculate(user)
|
2017-11-23 12:58:46 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 22:35:14 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_users
|
|
|
|
#
|
2016-01-11 01:30:56 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# group_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# owner :boolean default(FALSE), not null
|
2016-01-27 05:56:25 -05:00
|
|
|
# notification_level :integer default(2), not null
|
2013-05-23 22:35:14 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_group_users_on_group_id_and_user_id (group_id,user_id) UNIQUE
|
2015-09-17 20:43:53 -04:00
|
|
|
# index_group_users_on_user_id_and_group_id (user_id,group_id) UNIQUE
|
2013-05-23 22:35:14 -04:00
|
|
|
#
|