mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FEATURE: allow admins to choose a group as a primary group
FEATURE: allow admins to set a default title for a group
This commit is contained in:
parent
e143eb595f
commit
75890aed26
11 changed files with 220 additions and 19 deletions
|
@ -12,6 +12,8 @@ class Group < ActiveRecord::Base
|
|||
|
||||
after_save :destroy_deletions
|
||||
after_save :automatic_group_membership
|
||||
after_save :update_primary_group
|
||||
after_save :update_title
|
||||
|
||||
validate :name_format_validator
|
||||
validates_uniqueness_of :name, case_sensitive: false
|
||||
|
@ -301,6 +303,59 @@ class Group < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def update_title
|
||||
return if new_record? && !self.title.present?
|
||||
|
||||
if self.title_changed?
|
||||
sql = <<SQL
|
||||
UPDATE users SET title = :title
|
||||
WHERE (title = :title_was OR
|
||||
title = '' OR
|
||||
title IS NULL) AND
|
||||
COALESCE(title,'') <> COALESCE(:title,'') AND
|
||||
id IN (
|
||||
SELECT user_id
|
||||
FROM group_users
|
||||
WHERE group_id = :id
|
||||
)
|
||||
SQL
|
||||
|
||||
self.class.exec_sql(sql,
|
||||
title: title,
|
||||
title_was: title_was,
|
||||
id: id
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def update_primary_group
|
||||
return if new_record? && !self.primary_group?
|
||||
|
||||
if self.primary_group_changed?
|
||||
sql = <<SQL
|
||||
UPDATE users
|
||||
/*set*/
|
||||
/*where*/
|
||||
SQL
|
||||
|
||||
builder = SqlBuilder.new(sql)
|
||||
builder.where("
|
||||
id IN (
|
||||
SELECT user_id
|
||||
FROM group_users
|
||||
WHERE group_id = :id
|
||||
)", id: id)
|
||||
|
||||
if primary_group
|
||||
builder.set("primary_group_id = :id")
|
||||
else
|
||||
builder.set("primary_group_id = NULL")
|
||||
builder.where("primary_group_id = :id")
|
||||
end
|
||||
|
||||
builder.exec
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue