From 0289053a703fd8a1d7a7ce56ea796056e11883bc Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 20 Jan 2014 15:49:43 -0500 Subject: [PATCH] Add the Lounge category, an exclusive area for trust level 3 users --- config/locales/server.en.yml | 3 ++ config/site_settings.yml | 3 ++ db/fixtures/500_lounge_category.rb | 37 +++++++++++++++++++ .../20140120155706_add_lounge_category.rb | 31 ++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 db/fixtures/500_lounge_category.rb create mode 100644 db/migrate/20140120155706_add_lounge_category.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 3f134043ce3..72329052dae 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -188,6 +188,9 @@ en: no_info_me: "
the About Me field of your profile is currently blank, would you like to fill it out?
" no_info_other: "
%{name} hasn't entered anything in the About Me field of their profile yet
" + 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.]" diff --git a/config/site_settings.yml b/config/site_settings.yml index 13beb4f17ea..b635049490a 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -423,5 +423,8 @@ uncategorized: suppress_uncategorized_badge: client: true default: true + lounge_category_id: + default: -1 + hidden: true diff --git a/db/fixtures/500_lounge_category.rb b/db/fixtures/500_lounge_category.rb new file mode 100644 index 00000000000..5cc9cd36341 --- /dev/null +++ b/db/fixtures/500_lounge_category.rb @@ -0,0 +1,37 @@ +unless Rails.env.test? + 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 +end diff --git a/db/migrate/20140120155706_add_lounge_category.rb b/db/migrate/20140120155706_add_lounge_category.rb new file mode 100644 index 00000000000..4d7127c0b15 --- /dev/null +++ b/db/migrate/20140120155706_add_lounge_category.rb @@ -0,0 +1,31 @@ +class AddLoungeCategory < ActiveRecord::Migration + def up + unless Rails.env.test? + 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 + 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