Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Functional February badge #3403

Merged
merged 7 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/commands/solution/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def call
end

award_reputation!
award_badge!
record_activity!
log_metric!
update_num_published_solutions_on_exercise!
Expand All @@ -32,6 +33,10 @@ def award_reputation!
)
end

def award_badge!
AwardBadgeJob.perform_later(solution.user, :functional_february)
end

def record_activity!
User::Activity::Create.(
:published_exercise,
Expand Down
1 change: 1 addition & 0 deletions app/images/icons/functional.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions app/models/badges/functional_february_badge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Badges
class FunctionalFebruaryBadge < Badge
TRACK_SLUGS = %w[
clojure elixir erlang fsharp haskell ocaml scala sml gleam
].freeze

seed "Functional February",
:ultimate,
'functional',
'Completed and published five exercises in a functional language in February'

def self.worth_queuing?(exercise:)
TRACK_SLUGS.include?(exercise.track.slug)
end

def award_to?(user)
ErikSchierboom marked this conversation as resolved.
Show resolved Hide resolved
user.solutions.published.joins(exercise: :track).
where('tracks.slug': TRACK_SLUGS).
where('MONTH(published_at) = 2').
count >= 5
end

def send_email_on_acquisition? = true
end
end
26 changes: 26 additions & 0 deletions test/commands/solution/publish_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ class Solution::PublishTest < ActiveSupport::TestCase
assert_includes user.reload.badges.map(&:class), Badges::AnybodyThereBadge
end

test "awards functional february badge when published five or more exercises in track after participating in 12in23 challenge" do
travel_to Time.utc(2022, 2, 24)

track = create :track, slug: 'fsharp'
user = create :user
user_track = create :user_track, user: user, track: track

create :user_challenge, user: user, challenge_id: '12in23'

4.times do
exercise = create :practice_exercise, :random_slug, track: track
create :practice_solution, :published, user: user, track: track, exercise: exercise
refute user.badges.present?
end

exercise = create :practice_exercise, :random_slug, track: track
solution = create :practice_solution, user: user, exercise: exercise
create :iteration, solution: solution, idx: 1
refute user.badges.present?

Solution::Publish.(solution, user_track, 1)

perform_enqueued_jobs
assert_includes user.reload.badges.map(&:class), Badges::FunctionalFebruaryBadge
end

test "solution snippet updated to published iteration's snippet when single iteration is published" do
solution = create :practice_solution, snippet: 'my snippet'
create :user_track, user: solution.user, track: solution.track
Expand Down
1 change: 1 addition & 0 deletions test/factories/badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
mentor researcher v3_pioneer tooling_pioneer
moss begetter bard architect troubleshooter
completer conceptual supermentor
functional_february
].each do |type|
factory "#{type}_badge", class: "Badges::#{type.to_s.camelize}Badge" do
end
Expand Down
56 changes: 56 additions & 0 deletions test/models/badges/functional_february_badge_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "test_helper"

class Badges::FunctionalFebruaryBadgeTest < ActiveSupport::TestCase
test "attributes" do
badge = create :functional_february_badge
assert_equal "Functional February", badge.name
assert_equal :ultimate, badge.rarity
ErikSchierboom marked this conversation as resolved.
Show resolved Hide resolved
assert_equal :functional, badge.icon
assert_equal 'Completed and published five exercises in a functional language in February', badge.description
assert badge.send_email_on_acquisition?
assert_nil badge.notification_key
end

test "award_to?" do
user = create :user
badge = create :functional_february_badge
fsharp = create :track, slug: 'fsharp'
csharp = create :track, slug: 'csharp'

# No solutions
refute badge.award_to?(user.reload)

# 4 bob's is not enough
4.times do |idx|
exercise = create :practice_exercise, slug: 'bob', track: fsharp
create :practice_solution, :published, user:, track: fsharp, exercise:, completed_at: Time.utc(2022, 2, idx + 5)
end
refute badge.award_to?(user.reload)

# Doesn't care if we get a 5th exercise in csharp
another_exercise = create :practice_exercise, slug: 'leap', track: csharp
create :practice_solution, :published, user: user, track: csharp, exercise: another_exercise
refute badge.award_to?(user.reload)

# Iterate a 5th bob, but in march
exercise = create :practice_exercise, slug: 'bob', track: fsharp
solution = create :practice_solution, :iterated, user: user, track: fsharp, exercise: exercise,
completed_at: Time.utc(2022, 3, 1)
refute badge.award_to?(user.reload)

# Complete it
solution.update(completed_at: Time.utc(2022, 2, 27))
refute badge.award_to?(user.reload)

# Publish it
solution.update(published_at: Time.utc(2022, 2, 28))
assert badge.award_to?(user.reload)
end

test "worth_queuing?" do
fsharp = create :track, slug: 'fsharp'
csharp = create :track, slug: 'csharp'
assert Badges::FunctionalFebruaryBadge.worth_queuing?(exercise: create(:practice_exercise, track: fsharp))
refute Badges::FunctionalFebruaryBadge.worth_queuing?(exercise: create(:practice_exercise, track: csharp))
end
end
2 changes: 1 addition & 1 deletion test/models/badges/lackadaisical_badge_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "test_helper"

class Badge::LackadaisicalBadgeTest < ActiveSupport::TestCase
class Badges::LackadaisicalBadgeTest < ActiveSupport::TestCase
test "attributes" do
badge = create :lackadaisical_badge
assert_equal "Lackadaisical", badge.name
Expand Down