discourse/plugins/discourse-narrative-bot/autoload/jobs/onceoff/grant_badges.rb
Blake Erickson 80a80ef2bd 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.
2020-04-09 20:46:51 -06:00

38 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Jobs
module DiscourseNarrativeBot
class GrantBadges < ::Jobs::Onceoff
def execute_onceoff(args)
new_user_track_badge = Badge.find_by(
name: ::DiscourseNarrativeBot::NewUserNarrative.badge_name
)
advanced_user_track_badge = Badge.find_by(
name: ::DiscourseNarrativeBot::AdvancedUserNarrative.badge_name
)
PluginStoreRow.where(
plugin_name: ::DiscourseNarrativeBot::PLUGIN_NAME,
type_name: 'JSON'
).find_each do |row|
value = JSON.parse(row.value)
completed = value["completed"]
user = User.find_by(id: row.key)
if user && completed
if completed.include?(::DiscourseNarrativeBot::NewUserNarrative.to_s)
BadgeGranter.grant(new_user_track_badge, user)
end
if completed.include?(::DiscourseNarrativeBot::AdvancedUserNarrative.to_s)
BadgeGranter.grant(advanced_user_track_badge, user)
end
end
end
end
end
end
end