-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add completed #12in23 badge * Make completed 12in23 badge legendary
- Loading branch information
1 parent
9f09199
commit d4abc42
Showing
7 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Badges | ||
class Completed12In23Badge < Badge | ||
seed "Completed #12in23 Challenge", | ||
:legendary, | ||
'badge-completed-12-in-23', | ||
'Completed and published all featured exercises in the #12in23 challenge' | ||
|
||
def award_to?(user) | ||
return false unless participant?(user) | ||
|
||
User::Challenges::FeaturedExercisesProgress12In23.(user).all? do |exercise_progress| | ||
exercise_progress[:earned_for].present? | ||
end | ||
end | ||
|
||
def send_email_on_acquisition? = true | ||
|
||
def participant?(user) = user.challenges.where(challenge_id: User::Challenge::CHALLENGE_12_IN_23).exists? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "test_helper" | ||
|
||
class Badge::Completed12In23BadgeTest < ActiveSupport::TestCase | ||
test "attributes" do | ||
badge = create :completed_12_in_23_badge | ||
assert_equal "Completed #12in23 Challenge", badge.name | ||
assert_equal :legendary, badge.rarity | ||
assert_equal :'badge-completed-12-in-23', badge.icon # rubocop:disable Naming/VariableNumber | ||
assert_equal 'Completed and published all featured exercises in the #12in23 challenge', badge.description | ||
assert badge.send_email_on_acquisition? | ||
assert_nil badge.notification_key | ||
end | ||
|
||
test "award_to?" do | ||
user = create :user | ||
badge = create :completed_12_in_23_badge | ||
other_user = create :user | ||
|
||
refute badge.award_to?(user.reload) | ||
|
||
create :user_challenge, user:, challenge_id: '12in23' | ||
refute badge.award_to?(user.reload) | ||
|
||
User::Challenges::FeaturedExercisesProgress12In23.featured_exercises.values.flatten.uniq.each do |track_slug| | ||
create(:track, slug: track_slug) | ||
end | ||
|
||
User::Challenges::FeaturedExercisesProgress12In23.featured_exercises.each do |exercise_slug, track_slugs| | ||
track = Track.for!(track_slugs.sample) | ||
exercise = create(:practice_exercise, track:, slug: exercise_slug) | ||
create(:practice_solution, :published, user:, track:, exercise:, | ||
published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) | ||
end | ||
|
||
assert badge.award_to?(user.reload) | ||
|
||
last_solution = user.solutions.last | ||
|
||
# Sanity check: only published solutions count | ||
last_solution.update(published_iteration_id: nil, published_at: nil) | ||
refute badge.award_to?(user.reload) | ||
|
||
# Sanity check: other user's solutions don't count | ||
create(:practice_solution, :published, user: other_user, track: last_solution.track, exercise: last_solution.exercise, | ||
published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) | ||
refute badge.award_to?(user.reload) | ||
end | ||
end |