-
-
Notifications
You must be signed in to change notification settings - Fork 126
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
Trophy for viewing five approaches #6543
Open
ErikSchierboom
wants to merge
6
commits into
main
Choose a base branch
from
approaches-trophy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb0e797
Start work on reading five approaches trophy
ErikSchierboom e7ad2ca
Reseed five approaches trophy
ErikSchierboom 18eb805
Fix test file name
ErikSchierboom d6eb302
Add tests
ErikSchierboom 1e9e7be
Add icon
ErikSchierboom a1ad738
Trigger viewed approaches trophy
ErikSchierboom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ | ||
class Track::Trophies::ReadFiveApproachesTrophy < Track::Trophy | ||
def self.valid_track_slugs | ||
Track.active.where(id: | ||
Exercise::Approach.joins(:exercise). | ||
where('exercises.track_id = tracks.id'). | ||
having("count(*) >= ?", NUM_APPROACHES). | ||
select(:track_id)).pluck(:slug) | ||
end | ||
|
||
def name(_) = "Digging Deeper" | ||
def icon = 'trophy-read-five-approaches' | ||
def order = 7 | ||
|
||
# rubocop:disable Layout/LineLength | ||
def criteria(track) | ||
"Awarded for reading %<num_approaches>i %<track_title>s approaches" % { | ||
num_approaches: NUM_APPROACHES, | ||
track_title: track.title | ||
} | ||
end | ||
|
||
def success_message(track) | ||
"Congratulations on reading %<num_approaches>i approaches in %<track_title>s. Learning how to approach an exercise in different ways is a fantastic way to master a language!" % { | ||
num_approaches: NUM_APPROACHES, | ||
track_title: track.title | ||
} | ||
end | ||
# rubocop:enable Layout/LineLength | ||
|
||
def award?(user, track) | ||
UserTrack::ViewedExerciseApproach. | ||
where(user:, track:). | ||
count >= NUM_APPROACHES | ||
end | ||
|
||
NUM_APPROACHES = 5 | ||
private_constant :NUM_APPROACHES | ||
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
File renamed without changes.
66 changes: 66 additions & 0 deletions
66
test/models/track/trophies/read_five_approaches_trophy_test.rb
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,66 @@ | ||
require "test_helper" | ||
|
||
class Track::Trophies::ReadFiveApproachesTrophyTest < ActiveSupport::TestCase | ||
test "award?" do | ||
user = create :user | ||
other_user = create :user | ||
track = create :track | ||
other_track = create :track, slug: 'kotlin' | ||
trophy = create :read_five_approaches_trophy | ||
|
||
exercise = create(:practice_exercise, :random_slug, track:) | ||
other_exercise = create(:practice_exercise, :random_slug, track: other_track) | ||
|
||
# Don't award trophy if no approaches have been read | ||
refute trophy.award?(user, track) | ||
|
||
# Don't award trophy if less than five approaches | ||
create_list(:exercise_approach, 4, :random, exercise:) do |approach| | ||
create(:user_track_viewed_exercise_approach, user:, track:, approach:) | ||
end | ||
refute trophy.award?(user, track) | ||
|
||
# Don't award trophy if other user has read five approaches in the track | ||
create_list(:exercise_approach, 6, :random, exercise:) do |approach| | ||
create(:user_track_viewed_exercise_approach, user: other_user, track:, approach:) | ||
end | ||
refute trophy.award?(user, track) | ||
|
||
# Don't count viewed approaches for other track | ||
create_list(:exercise_approach, 7, :random, exercise: other_exercise) do |approach| | ||
create(:user_track_viewed_exercise_approach, user:, track: other_track, approach:) | ||
end | ||
refute trophy.award?(user, track) | ||
|
||
# Award trophy when five approaches solutions in the track have been read | ||
create(:exercise_approach, exercise:) do |approach| | ||
create(:user_track_viewed_exercise_approach, user:, track:, approach:) | ||
end | ||
assert trophy.award?(user, track) | ||
end | ||
|
||
test "reseed! sets valid_track_slugs to tracks with at least five approaches" do | ||
trophy = create :read_five_approaches_trophy | ||
|
||
# Track is not valid if it doesn't have any approaches | ||
track = create :track, :random_slug | ||
trophy.reseed! | ||
assert_empty trophy.valid_track_slugs | ||
|
||
# Four approaches is not enough | ||
create_list(:exercise_approach, 4, exercise: create(:practice_exercise, :random_slug, track:)) | ||
trophy.reseed! | ||
assert_empty trophy.valid_track_slugs | ||
|
||
# Five approaches is enough | ||
create(:exercise_approach, exercise: create(:practice_exercise, :random_slug, track:)) | ||
trophy.reseed! | ||
assert_equal [track.slug], trophy.valid_track_slugs | ||
|
||
# Include all tracks with five approaches | ||
other_track = create :track, :random_slug | ||
create_list(:exercise_approach, 6, exercise: create(:practice_exercise, :random_slug, track: other_track)) | ||
trophy.reseed! | ||
assert_equal [track.slug, other_track.slug].sort, trophy.valid_track_slugs.sort | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One for you @iHiD