Skip to content

Commit

Permalink
Add Fab Five BadgeRewarder + Rake Task (forem#1048)
Browse files Browse the repository at this point in the history
* adds method and rake task

* Improve BadgeRewarder spac
  • Loading branch information
jessleenyc authored and benhalpern committed Nov 5, 2018
1 parent dd1d031 commit 295b30c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
6 changes: 5 additions & 1 deletion app/labor/badge_rewarder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.award_beloved_comment_badges
message = "You're DEV famous! [This is the comment](https://dev.to#{comment.path}) for which you are being recognized. 😄"
achievement = BadgeAchievement.create(
user_id: comment.user_id,
badge_id: 3,
badge_id: Badge.find_by_slug("beloved-comment")&.id || 3,
rewarding_context_message_markdown: message,
)
comment.user.save if achievement.valid?
Expand All @@ -29,6 +29,10 @@ def self.award_top_seven_badges(usernames, message_markdown = "Congrats!!!")
award_badges(usernames, "top-7", message_markdown)
end

def self.award_fab_five_badges(usernames, message_markdown = "Congrats!!!")
award_badges(usernames, "fab-5", message_markdown)
end

def self.award_contributor_badges(usernames, message_markdown = "Thank you so much for your contributions!")
award_badges(usernames, "dev-contributor", message_markdown)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/fetch.rake
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ task :award_contributor_badges, [:arg1] => :environment do |_t, args|
puts "Done!"
end

# rake award_fab_five_badges["ben jess peter mac liana andy"]
task :award_fab_five_badges, [:arg1] => :environment do |_t, args|
usernames = args[:arg1].split(" ")
puts "Awarding fab 5 badges to #{usernames}"
BadgeRewarder.award_fab_five_badges(usernames)
puts "Done!"
end

# this task is meant to be scheduled daily
task award_contributor_badges_from_github: :environment do
BadgeRewarder.award_contributor_badges_from_github
Expand Down
31 changes: 19 additions & 12 deletions spec/labor/badge_rewarder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
end

it "rewards beloved comment to folks who have a qualifying comment" do
user = create(:user)
user_other = create(:user)
article = create(:article)
create(:comment, user_id: user.id, positive_reactions_count: 30, commentable_id: article.id)
create(
:comment, user_id: user_other.id, positive_reactions_count: 3, commentable_id: article.id
)
create(:badge, title: "heweewweyhey")
create(:badge, title: "heweweewewewewyhey")
create(:badge, title: "heewwewewwwwwyhey")
create(:badge, title: "Beloved comment", slug: "beloved-comment")
comment = create(:comment, commentable: create(:article))
comment.update(positive_reactions_count: 30)
described_class.award_beloved_comment_badges
expect(user.badge_achievements.size).to eq(1)
expect(user_other.badge_achievements.size).to eq(0)
expect(BadgeAchievement.count).to eq(1)
end

it "does not reward beloved comment to non-qualifying comment" do
create(:badge, title: "Beloved comment", slug: "beloved-comment")
create(:comment, commentable: create(:article))
described_class.award_beloved_comment_badges
expect(BadgeAchievement.count).to eq(0)
end

it "rewards top seven badge to users" do
Expand All @@ -38,6 +37,14 @@
expect(BadgeAchievement.where(badge_id: badge.id).size).to eq(2)
end

it "rewards fab five badge to users" do
badge = create(:badge, title: "Fab 5")
user = create(:user)
user_other = create(:user)
described_class.award_fab_five_badges([user.username, user_other.username])
expect(BadgeAchievement.where(badge_id: badge.id).size).to eq(2)
end

it "rewards contributor badges" do
badge = create(:badge, title: "Dev Contributor")
user = create(:user)
Expand Down

0 comments on commit 295b30c

Please sign in to comment.