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.
This commit is contained in:
Blake Erickson 2020-04-09 20:46:51 -06:00
parent 3cac60f07c
commit 80a80ef2bd
8 changed files with 16 additions and 8 deletions

View File

@ -5,11 +5,11 @@ module Jobs
class GrantBadges < ::Jobs::Onceoff class GrantBadges < ::Jobs::Onceoff
def execute_onceoff(args) def execute_onceoff(args)
new_user_track_badge = Badge.find_by( new_user_track_badge = Badge.find_by(
name: ::DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME name: ::DiscourseNarrativeBot::NewUserNarrative.badge_name
) )
advanced_user_track_badge = Badge.find_by( advanced_user_track_badge = Badge.find_by(
name: ::DiscourseNarrativeBot::AdvancedUserNarrative::BADGE_NAME name: ::DiscourseNarrativeBot::AdvancedUserNarrative.badge_name
) )
PluginStoreRow.where( PluginStoreRow.where(

View File

@ -98,6 +98,10 @@ module DiscourseNarrativeBot
} }
} }
def self.badge_name
BADGE_NAME
end
def self.reset_trigger def self.reset_trigger
I18n.t('discourse_narrative_bot.advanced_user_narrative.reset_trigger') I18n.t('discourse_narrative_bot.advanced_user_narrative.reset_trigger')
end end

View File

@ -74,7 +74,7 @@ module DiscourseNarrativeBot
cancel_timeout_job(user) cancel_timeout_job(user)
BadgeGranter.grant( BadgeGranter.grant(
Badge.find_by(name: self.class::BADGE_NAME), Badge.find_by(name: self.class.badge_name),
user user
) )

View File

@ -117,6 +117,10 @@ module DiscourseNarrativeBot
} }
} }
def self.badge_name
BADGE_NAME
end
def self.search_answer def self.search_answer
':herb:' ':herb:'
end end

View File

@ -725,7 +725,7 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
"topic_id" => topic.id, "topic_id" => topic.id,
"track" => described_class.to_s) "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) .to eq(true)
end end
end end

View File

@ -1007,7 +1007,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
) )
expect(user.badges.where( expect(user.badges.where(
name: DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME).exists? name: DiscourseNarrativeBot::NewUserNarrative.badge_name).exists?
).to eq(true) ).to eq(true)
end end
end end

View File

@ -319,7 +319,7 @@ describe DiscourseNarrativeBot::TrackSelector do
) )
BadgeGranter.grant( BadgeGranter.grant(
Badge.find_by(name: DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME), Badge.find_by(name: DiscourseNarrativeBot::NewUserNarrative.badge_name),
user user
) )

View File

@ -19,8 +19,8 @@ RSpec.describe Jobs::DiscourseNarrativeBot::GrantBadges do
expect(user.badges.count).to eq(2) expect(user.badges.count).to eq(2)
expect(user.badges.map(&:name)).to contain_exactly( expect(user.badges.map(&:name)).to contain_exactly(
DiscourseNarrativeBot::NewUserNarrative::BADGE_NAME, DiscourseNarrativeBot::NewUserNarrative.badge_name,
DiscourseNarrativeBot::AdvancedUserNarrative::BADGE_NAME, DiscourseNarrativeBot::AdvancedUserNarrative.badge_name,
) )
expect(other_user.badges.count).to eq(0) expect(other_user.badges.count).to eq(0)