From 80a80ef2bd028c2292bed0ef491de6ccba93f8f2 Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Thu, 9 Apr 2020 20:46:51 -0600 Subject: [PATCH] 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. --- .../autoload/jobs/onceoff/grant_badges.rb | 4 ++-- .../lib/discourse_narrative_bot/advanced_user_narrative.rb | 4 ++++ .../lib/discourse_narrative_bot/base.rb | 2 +- .../lib/discourse_narrative_bot/new_user_narrative.rb | 4 ++++ .../discourse_narrative_bot/advanced_user_narrative_spec.rb | 2 +- .../spec/discourse_narrative_bot/new_user_narrative_spec.rb | 2 +- .../spec/discourse_narrative_bot/track_selector_spec.rb | 2 +- .../spec/jobs/onceoff/grant_badges_spec.rb | 4 ++-- 8 files changed, 16 insertions(+), 8 deletions(-) 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 373b3b7a4fb..c723d176e92 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 185d472ea62..aa66cb1802a 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 2cccd93ac8c..404dd084b40 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 0acd31d5bbb..89afd4eb870 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 28fec30417f..da97a7df58a 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 6648337caf9..43ff1ac52a5 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 c957cfe627d..4e24b9feaef 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 5a0b3a98c1a..35950b6d428 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)