DEV: Use method instead of constant for discobot badge names

DEV: Use method instead of constant for discobot badge names

This change refactors the code a bit so that a plugin could easily replace which badge is awarded when completing the discobot new user tutorial and advanced tutorial.

By adding a static method and putting the BADGE_NAME constant inside of that method we can simply call that method now instead of the constant. A plugin could then class_eval that method and replace it with whatever badge name they choose. This is way cleaner than having the plugin change the frozen constant! eeek.

diff --git a/plugins/discourse-narrative-bot/autoload/jobs/onceoff/grant_badges.rb b/plugins/discourse-narrative-bot/autoload/jobs/onceoff/grant_badges.rb
index 373b3b7..c723d17 100644
--- a/plugins/discourse-narrative-bot/autoload/jobs/onceoff/grant_badges.rb
+++ b/plugins/discourse-narrative-bot/autoload/jobs/onceoff/grant_badges.rb
@@ -5,11 +5,11 @@ module Jobs
     class GrantBadges < ::Jobs::Onceoff
       def execute_onceoff(args)
         new_user_track_badge = Badge.find_by(
-          name: ::DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME
+          name: ::DiscourseNarrativeBot::NewUserNarrative.badge_name
         )
 
         advanced_user_track_badge = Badge.find_by(
-          name: ::DiscourseNarrativeBot::AdvancedUserNarrative::BADGE_NAME
+          name: ::DiscourseNarrativeBot::AdvancedUserNarrative.badge_name
         )
 
         PluginStoreRow.where(
diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb
index 185d472..aa66cb1 100644
--- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb
+++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb
@@ -98,6 +98,10 @@ module DiscourseNarrativeBot
       }
     }
 
+    def self.badge_name
+      BADGE_NAME
+    end
+
     def self.reset_trigger
       I18n.t('discourse_narrative_bot.advanced_user_narrative.reset_trigger')
     end
diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb
index 2cccd93..404dd08 100644
--- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb
+++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/base.rb
@@ -74,7 +74,7 @@ module DiscourseNarrativeBot
               cancel_timeout_job(user)
 
               BadgeGranter.grant(
-                Badge.find_by(name: self.class::BADGE_NAME),
+                Badge.find_by(name: self.class.badge_name),
                 user
               )
 
diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb
index 0acd31d..89afd4e 100644
--- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb
+++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb
@@ -117,6 +117,10 @@ module DiscourseNarrativeBot
       }
     }
 
+    def self.badge_name
+      BADGE_NAME
+    end
+
     def self.search_answer
       ':herb:'
     end
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
index 28fec30..da97a7d 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb
@@ -725,7 +725,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
                                                "topic_id" => topic.id,
                                                "track" => described_class.to_s)
 
-        expect(user.badges.where(name: DiscourseNarrativeBot::AdvancedUserNarrative::BADGE_NAME).exists?)
+        expect(user.badges.where(name: DiscourseNarrativeBot::AdvancedUserNarrative.badge_name).exists?)
           .to eq(true)
       end
     end
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
index 6648337..43ff1ac 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb
@@ -1007,7 +1007,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
           )
 
           expect(user.badges.where(
-            name: DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME).exists?
+            name: DiscourseNarrativeBot::NewUserNarrative.badge_name).exists?
           ).to eq(true)
         end
       end
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
index c957cfe..4e24b9f 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
@@ -319,7 +319,7 @@ describe DiscourseNarrativeBot::TrackSelector do
                 )
 
                 BadgeGranter.grant(
-                  Badge.find_by(name: DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME),
+                  Badge.find_by(name: DiscourseNarrativeBot::NewUserNarrative.badge_name),
                   user
                 )
 
diff --git a/plugins/discourse-narrative-bot/spec/jobs/onceoff/grant_badges_spec.rb b/plugins/discourse-narrative-bot/spec/jobs/onceoff/grant_badges_spec.rb
index 5a0b3a9..35950b6 100644
--- a/plugins/discourse-narrative-bot/spec/jobs/onceoff/grant_badges_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/jobs/onceoff/grant_badges_spec.rb
@@ -19,8 +19,8 @@ RSpec.describe Jobs::DiscourseNarrativeBot::GrantBadges do
     expect(user.badges.count).to eq(2)
 
     expect(user.badges.map(&:name)).to contain_exactly(
-      DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME,
-      DiscourseNarrativeBot::AdvancedUserNarrative::BADGE_NAME,
+      DiscourseNarrativeBot::NewUserNarrative.badge_name,
+      DiscourseNarrativeBot::AdvancedUserNarrative.badge_name,
     )
 
     expect(other_user.badges.count).to eq(0)

GitHub sha: 80a80ef2

Nice change! I like it.

1 Like