Skip to content

Commit

Permalink
Separate medals by activity type.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 10, 2024
1 parent 04f0207 commit 4c5de2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-11-10 21:20:44 UTC using RuboCop version 1.68.0.
# on 2024-11-10 21:33:16 UTC using RuboCop version 1.68.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -82,7 +82,7 @@ Naming/VariableNumber:
RSpec/AnyInstance:
Enabled: false

# Offense count: 192
# Offense count: 193
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Expand Down Expand Up @@ -151,7 +151,7 @@ RSpec/InstanceVariable:
- 'spec/slack-strava/service_spec.rb'
- 'spec/support/api/endpoints/it_behaves_like_a_cursor_api.rb'

# Offense count: 34
# Offense count: 35
RSpec/LetSetup:
Enabled: false

Expand Down Expand Up @@ -187,7 +187,7 @@ RSpec/NamedSubject:
- 'spec/api/endpoints/teams_endpoint_spec.rb'
- 'spec/slack-strava/app_spec.rb'

# Offense count: 102
# Offense count: 104
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 7
Expand Down
4 changes: 2 additions & 2 deletions slack-strava/models/team_leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def aggregate!
end
end

def find(user_id)
def find(user_id, activity_type)
position = aggregate!.find_index do |row|
row[:_id][:user_id] == user_id
row[:_id][:user_id] == user_id && row[:_id][:type] == activity_type
end
position && position >= 0 ? position + 1 : nil
end
Expand Down
4 changes: 2 additions & 2 deletions slack-strava/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def team_admin?
activated_user? || is_admin? || is_owner?
end

def medal_s
case team.leaderboard(metric: 'Distance').find(_id)
def medal_s(activity_type)
case team.leaderboard(metric: 'Distance').find(_id, activity_type)
when 1
'🥇'
when 2
Expand Down
2 changes: 1 addition & 1 deletion slack-strava/models/user_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def to_slack_attachment
if display_field?(ActivityFields::USER) || display_field?(ActivityFields::DATE)
[
if display_field?(ActivityFields::USER)
["<@#{user.user_name}>", display_field?(ActivityFields::MEDAL) ? user.medal_s : nil].compact.join(' ')
["<@#{user.user_name}>", display_field?(ActivityFields::MEDAL) ? user.medal_s(type) : nil].compact.join(' ')
end,
display_field?(ActivityFields::DATE) ? start_date_local_s : nil
].compact.join(' on ')
Expand Down
24 changes: 21 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@
let!(:user) { Fabricate(:user) }

it 'no activities' do
expect(user.medal_s).to be_nil
expect(user.medal_s('Run')).to be_nil
end

context 'with an activity' do
Expand All @@ -666,7 +666,7 @@
end

it 'returns a gold medal' do
expect(user.medal_s).to eq '🥇'
expect(user.medal_s('Run')).to eq '🥇'
end
end

Expand All @@ -682,11 +682,29 @@
end

it "returns #{medal}" do
expect(user.medal_s).to eq medal
expect(user.medal_s('Run')).to eq medal
end
end
end
end

context 'with an activity of a different type' do
let!(:activity) { Fabricate(:user_activity, user: user) }

context 'ranked first' do
before do
Fabricate(:swim_activity, user: user)
end

it 'returns a gold medal' do
expect(user.medal_s('Swim')).to eq '🥈'
end

it 'returns a silver medal' do
expect(user.medal_s('Run')).to eq '🥇'
end
end
end
end
end
end

0 comments on commit 4c5de2f

Please sign in to comment.