-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Lounge category, an exclusive area for trust level 3 users
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
lounge = Category.where(id: SiteSetting.lounge_category_id).first | ||
if lounge and !lounge.group_ids.include?(Group[:trust_level_3].id) | ||
|
||
# The category for users with trust level 3 has been created. | ||
# Add permissions and a description to it. | ||
|
||
lounge.group_names = ['trust_level_3'] | ||
unless lounge.save | ||
puts lounge.errors.full_messages | ||
raise "Failed to set permissions on trust level 3 lounge category!" | ||
end | ||
|
||
creator = PostCreator.new(Discourse.system_user, | ||
raw: I18n.t('vip_category_description'), | ||
title: I18n.t('category.topic_prefix', category: lounge.name), | ||
category: lounge.name, | ||
archetype: Archetype.default | ||
) | ||
post = creator.create | ||
|
||
unless post && post.id | ||
puts post.errors.full_messages if post | ||
puts creator.errors.inspect | ||
raise "Failed to create description for trust level 3 lounge!" | ||
end | ||
|
||
lounge.topic_id = post.topic.id | ||
unless lounge.save | ||
puts lounge.errors.full_messages | ||
puts "Failed to set the lounge description topic!" | ||
end | ||
|
||
# Reset topic count because we don't count the description topic | ||
Category.exec_sql "UPDATE categories SET topic_count = 0 WHERE id = #{lounge.id}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class AddLoungeCategory < ActiveRecord::Migration | ||
def up | ||
result = Category.exec_sql "SELECT 1 FROM site_settings where name = 'lounge_category_id'" | ||
if result.count == 0 | ||
description = I18n.t('vip_category_description') | ||
|
||
default_name = I18n.t('vip_category_name') | ||
name = if Category.exec_sql("SELECT 1 FROM categories where name = '#{default_name}'").count == 0 | ||
default_name | ||
else | ||
"CHANGE_ME" | ||
end | ||
|
||
result = execute "INSERT INTO categories | ||
(name, color, text_color, created_at, updated_at, user_id, slug, description, read_restricted) | ||
VALUES ('#{name}', 'EEEEEE', '652D90', now(), now(), -1, '#{Slug.for(name)}', '#{description}', true) | ||
RETURNING id" | ||
category_id = result[0]["id"].to_i | ||
|
||
execute "INSERT INTO site_settings(name, data_type, value, created_at, updated_at) | ||
VALUES ('lounge_category_id', 3, #{category_id}, now(), now())" | ||
end | ||
end | ||
|
||
def down | ||
# Don't reverse this change. There is so much logic around deleting a category that it's messy | ||
# to try to do in sql. The up method will just make sure never to create the category twice. | ||
end | ||
end |