A platform for community discussion. Free, open, simple.
DEV: add upcoming change to enable topic voting badges by default (#41695)
David Battersbyee8c1bc3fa5a
diff --git a/plugins/discourse-topic-voting/app/services/discourse_topic_voting/enable_topic_voting_badges_toggled.rb b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/enable_topic_voting_badges_toggled.rbnew file mode 100644index 00000000000..f2ac768fd53--- /dev/null+++ b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/enable_topic_voting_badges_toggled.rb@@ -0,0 +1,22 @@+# frozen_string_literal: true++# Reacts to the `enable_topic_voting_badges` upcoming change being enabled or+# disabled (manually or via auto-promotion). See the on(:upcoming_change_enabled)+# / on(:upcoming_change_disabled) subscriptions in plugin.rb for the dispatch.+#+# Enabling turns on the four seeded Topic Voting badges; disabling turns them+# back off.+#+# New sites are handled separately by the badge seed fixture+# (db/fixtures/001_badges.rb): the promotion event does not fire on brand-new+# sites (UpcomingChanges.should_notify_admins? is false there), so the seed is+# what gives freshly provisioned sites their enabled-by-default badges.+module DiscourseTopicVoting+ class EnableTopicVotingBadgesToggled < Service::ActionBase+ option :enabled++ def call+ Badge.where(name: DiscourseTopicVoting::BADGE_NAMES).update_all(enabled:)+ end+ end+enddiff --git a/plugins/discourse-topic-voting/config/locales/server.en.yml b/plugins/discourse-topic-voting/config/locales/server.en.ymlindex 5fd90dc96f4..e202acd52d8 100755--- a/plugins/discourse-topic-voting/config/locales/server.en.yml+++ b/plugins/discourse-topic-voting/config/locales/server.en.yml@@ -7,6 +7,7 @@ en:site_settings:enable_ideas_category_type_setup: "Enables the Ideas category type option during category creation setup."+ enable_topic_voting_badges: "Enables the Topic Voting badges awarded to members when others vote for their ideas."topic_voting_enabled: 'Allow users to vote on topics?'topic_voting_enable_vote_limits: 'Limit the number of votes per user based on trust level'topic_voting_tl0_vote_limit: 'How many active votes are TL0 users allowed?'diff --git a/plugins/discourse-topic-voting/config/settings.yml b/plugins/discourse-topic-voting/config/settings.ymlindex b1b3ddef9c4..b5dfa1f6f18 100755--- a/plugins/discourse-topic-voting/config/settings.yml+++ b/plugins/discourse-topic-voting/config/settings.yml@@ -56,3 +56,11 @@ discourse_topic_voting:impact: "feature,staff"learn_more_url: "https://meta.discourse.org/t/-/399365"requires_plugin_enabled: false+ enable_topic_voting_badges:+ default: false+ hidden: true+ client: true+ upcoming_change:+ status: "alpha"+ impact: "site_setting_default,all_members"+ learn_more_url: "https://meta.discourse.org/t/-/401731"diff --git a/plugins/discourse-topic-voting/db/fixtures/001_badges.rb b/plugins/discourse-topic-voting/db/fixtures/001_badges.rbindex 2048200f552..59bada78dd7 100644--- a/plugins/discourse-topic-voting/db/fixtures/001_badges.rb+++ b/plugins/discourse-topic-voting/db/fixtures/001_badges.rb@@ -15,7 +15,7 @@badge.listable = truebadge.target_posts = truebadge.multiple_grant = true- badge.default_enabled = false+ badge.default_enabled = truebadge.default_allow_title = allow_titlebadge.trigger = Badge::Trigger::Nonebadge.auto_revoke = truediff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex 8cc735c782f..5dc1153a3f5 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -227,6 +227,18 @@ after_initialize doDiscourseTopicVoting::UserMerger.merge(source_user, target_user)end+ on(:upcoming_change_enabled) do |setting_name|+ if setting_name == :enable_topic_voting_badges+ DiscourseTopicVoting::EnableTopicVotingBadgesToggled.call(enabled: true)+ end+ end++ on(:upcoming_change_disabled) do |setting_name|+ if setting_name == :enable_topic_voting_badges+ DiscourseTopicVoting::EnableTopicVotingBadgesToggled.call(enabled: false)+ end+ end+Discourse::Application.routes.prepend doget "c/*category_slug_path_with_id/l/votes.rss" => "list#votes_feed", :format => :rssenddiff --git a/plugins/discourse-topic-voting/spec/integration/enable_topic_voting_badges_spec.rb b/plugins/discourse-topic-voting/spec/integration/enable_topic_voting_badges_spec.rbnew file mode 100644index 00000000000..6cfab3420f2--- /dev/null+++ b/plugins/discourse-topic-voting/spec/integration/enable_topic_voting_badges_spec.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++RSpec.describe "Enable Topic Voting badges by default upcoming change" do+ let(:badge_names) { DiscourseTopicVoting::BADGE_NAMES }++ def reseed_topic_voting_badges+ Badge.where(name: badge_names).delete_all+ load Rails.root.join("plugins/discourse-topic-voting/db/fixtures/001_badges.rb") # rubocop:disable Discourse/Plugins/UseRequireRelative+ end++ describe "seeding the Topic Voting badges on a new site" do+ it "enables the badges by default" do+ reseed_topic_voting_badges++ expect(Badge.where(name: badge_names).pluck(:enabled)).to all(eq(true))+ end+ end+enddiff --git a/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/enable_topic_voting_badges_toggled_spec.rb b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/enable_topic_voting_badges_toggled_spec.rbnew file mode 100644index 00000000000..baf76c36cc4--- /dev/null+++ b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/enable_topic_voting_badges_toggled_spec.rb@@ -0,0 +1,29 @@+# frozen_string_literal: true++RSpec.describe DiscourseTopicVoting::EnableTopicVotingBadgesToggled do+ let(:badge_names) { DiscourseTopicVoting::BADGE_NAMES }++ def topic_voting_badges+ Badge.where(name: badge_names)+ end++ describe "enabling" do+ before { topic_voting_badges.update_all(enabled: false) }++ it "enables all four Topic Voting badges" do+ described_class.call(enabled: true)++ expect(topic_voting_badges.where(enabled: true).count).to eq(4)+ end+ end++ describe "disabling" do+ before { topic_voting_badges.update_all(enabled: true) }++ it "disables all four Topic Voting badges" do+ described_class.call(enabled: false)++ expect(topic_voting_badges.where(enabled: false).count).to eq(4)+ end+ end+end