Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pjurewicz committed Oct 12, 2023
1 parent 42f452f commit 236e55b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/slack_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def givers
end

def leaderboard
render json: SlackMessages.leaderboards(Leaderboards.new(team))
render json: SlackMessages.leaderboards(Leaderboards::TextPresenter(Leaderboards.new(team)))
end

private
Expand Down
21 changes: 16 additions & 5 deletions app/models/leaderboards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ def aggreated_data(scope)
.limit(10)
.sum('points')
.group_by(&:second)
.then(&method(:format))
.map { |points, members| [points, members.map(&:first)] }.to_h
end

def format(hash)
hash
.map { |count, members| "#{count}: #{members.map(&:first).join(", ")}" }
.join("\n")
class TextPresenter
def initialize(leaderboards)
@leaderboards = leaderboards
end

def method_missing(m, *args, &block)
@leaderboards.respond_to?(m) ? format(@leaderboards.public_send(m)) : super
end

private
def format(hash)
hash
.map { |count, members| "#{count}: #{members.join(", ")}" }
.join("\n")
end
end
end
6 changes: 4 additions & 2 deletions test/models/leaderboards_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class LeaderboardsTest < ActiveSupport::TestCase
GiveUpvote.new.call('dudu', '+1 kaka', '+1', team)
GiveUpvote.new.call('kaka', '+1 dudu', '+1', team)

assert_equal <<~RESULT.strip, Leaderboards.new(team).top_for_this_month
assert_equal({ 2 => ["kaka"], 1 => ["dudu"] }, Leaderboards.new(team).top_for_this_month)
assert_equal <<~RESULT.strip, Leaderboards::TextPresenter.new(Leaderboards.new(team)).top_for_this_month
2: kaka
1: dudu
RESULT
Expand All @@ -29,7 +30,8 @@ class LeaderboardsTest < ActiveSupport::TestCase
GiveUpvote.new.call('dudu', '+1 kaka', '+1', team)
GiveUpvote.new.call('kaka', '+1 dudu', '+1', team)

assert_equal <<~RESULT.strip, Leaderboards.new(team).top_for_this_week
assert_equal({ 2 => ["kaka"], 1 => ["dudu"] }, Leaderboards.new(team).top_for_this_week)
assert_equal <<~RESULT.strip, Leaderboards::TextPresenter.new(Leaderboards.new(team)).top_for_this_week
2: kaka
1: dudu
RESULT
Expand Down

0 comments on commit 236e55b

Please sign in to comment.