Add the Lounge category, an exclusive area for trust level 3 users

This commit is contained in:
Neil Lalonde 2014-01-20 15:03:05 -05:00
parent daa5d023d6
commit 2d98720cc8
4 changed files with 70 additions and 0 deletions

View file

@ -188,6 +188,9 @@ en:
no_info_me: "<div class='missing-profile'>the About Me field of your profile is currently blank, <a href='/users/%{username_lower}/preferences/about-me'>would you like to fill it out?</a></div>"
no_info_other: "<div class='missing-profile'>%{name} hasn't entered anything in the About Me field of their profile yet</div>"
vip_category_name: "Lounge"
vip_category_description: "A category exclusive to members with trust level 3 and higher."
category:
topic_prefix: "Category definition for %{category}"
replace_paragraph: "[Replace this first paragraph with a short description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this text or create topics, this category won't appear on the categories page.]"

View file

@ -423,5 +423,8 @@ uncategorized:
suppress_uncategorized_badge:
client: true
default: true
lounge_category_id:
default: -1
hidden: true

View file

@ -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

View file

@ -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