Skip to content

Commit

Permalink
Tidy: Don't prefix View Model method names w/ display_
Browse files Browse the repository at this point in the history
This is an easy trap for me to fall into, but it feels wrong from the
POV of the view template. The message we want to be sending to the
View Model is the simple one: e.g. `best_bbbv`, not `display_best_bbbv`.
Then, the View Model just has to handle its internal needs a little
differently. In this case, we can use the same name, just prefixed with
an underscore to differentiate private methods / intermediate steps.
  • Loading branch information
Paul DobbinSchmaltz committed Oct 24, 2024
1 parent 85b99d2 commit 2484120
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 80 deletions.
18 changes: 9 additions & 9 deletions app/views/games/_results.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,44 @@
<% if stats.show_game_score? %>
<tr title="<%= t("game.stats.score") %>">
<th class="text-left">Score</th>
<td><%= stats.display_game_score %></td>
<td><%= stats.game_score %></td>
</tr>
<tr title="<%= t("game.stats.bbbv") %>">
<th class="text-left">3BV</th>
<td><%= stats.display_bbbv %></td>
<td><%= stats.bbbv %></td>
</tr>
<tr title="<%= t("game.stats.bbbvps") %>">
<th class="text-left">3BV/s</th>
<td><%= stats.display_bbbvps %></td>
<td><%= stats.bbbvps %></td>
</tr>
<tr title="<%= t("game.stats.efficiency") %>">
<th class="text-left">Efficiency</th>
<td><%= stats.display_efficiency_percentage %></td>
<td><%= stats.efficiency_percentage %></td>
</tr>
<% end %>
<tr title="<%= t("game.stats.clicks") %>">
<th class="text-left">Clicks</th>
<td><%= stats.display_clicks_count %></td>
<td><%= stats.clicks_count %></td>
</tr>
<tr>
<td colspan="100%">
<table class="ml-3">
<tbody class="*:*:px-3 *:*:py-1">
<tr title="<%= t("game.stats.reveals") %>">
<th class="text-left">Reveals</th>
<td><%= stats.display_reveals_count %></td>
<td><%= stats.reveals_count %></td>
</tr>
<tr title="<%= t("game.stats.chords") %>">
<th class="text-left">Chords</th>
<td><%= stats.display_chords_count %></td>
<td><%= stats.chords_count %></td>
</tr>
<tr>
<th class="text-left">Flags Placed</th>
<td><%= stats.display_flags_count %></td>
<td><%= stats.flags_count %></td>
</tr>
<tr>
<th class="text-left">Flags Removed</th>
<td><%= stats.display_unflags_count %></td>
<td><%= stats.unflags_count %></td>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion app/views/games/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
text-gray-500 dark:text-neutral-400
group-hover:text-inherit
"
><%= listing.display_game_score %></abbr>
><%= listing.game_score %></abbr>
<% end %>
<span><%= listing.game_engagement_time_range(self) %></span>
<span class="tracking-emojis"><%= listing.game_status_mojis %></span>
Expand Down
6 changes: 3 additions & 3 deletions app/views/games/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ def type_indicator
type[0]
end

def display_game_score = game_score.round(0)
def show_game_score? = !!game_score
def game_score = _game_score.round(0)
def show_game_score? = !!_game_score

def game_engagement_time_range(template)
template.safe_join(
Expand All @@ -222,6 +222,6 @@ def game_url(router = RailsRouter.instance)

def to_model = @model

def game_score = to_model.score
def _game_score = to_model.score
end
end
32 changes: 16 additions & 16 deletions app/views/games/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,48 @@ def cache_name = [game, :stats]

def show_game_score? = game.ended_in_victory?

def display_game_score
score ? score.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
def game_score
_score ? _score.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
end

def display_bbbv = bbbv || NO_VALUE_INDICATOR
def bbbv = _bbbv || NO_VALUE_INDICATOR

def display_bbbvps
bbbvps ? bbbvps.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
def bbbvps
_bbbvps ? _bbbvps.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
end

def display_efficiency_percentage
efficiency ? percentage(efficiency * 100.0) : NO_VALUE_INDICATOR
def efficiency_percentage
_efficiency ? percentage(_efficiency * 100.0) : NO_VALUE_INDICATOR
end

def display_clicks_count
def clicks_count
delimit(game.cell_transactions.size)
end

def display_reveals_count
def reveals_count
delimit(game.cell_reveal_transactions.size)
end

def display_chords_count
def chords_count
delimit(game.cell_chord_transactions.size)
end

def display_flags_count
def flags_count
delimit(game.cell_flag_transactions.size)
end

def display_unflags_count
def unflags_count
delimit(game.cell_unflag_transactions.size)
end

private

attr_reader :game

def score = game.score
def bbbv = game.bbbv
def bbbvps = game.bbbvps
def efficiency = game.efficiency
def _score = game.score
def _bbbv = game.bbbv
def _bbbvps = game.bbbvps
def _efficiency = game.efficiency

def percentage(value, precision: DEFAULT_PRECISION)
helpers.number_to_percentage(value, precision:)
Expand Down
22 changes: 11 additions & 11 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,53 @@
<tbody class="*:*:px-3 *:*:py-1">
<tr>
<th class="text-left">Engagements</th>
<td><%= @view.display_games_count %></td>
<td><%= @view.games_count %></td>
</tr>
<tr>
<th class="text-left">Victories</th>
<td><%= @view.display_winning_games_count %></td>
<td><%= @view.winning_games_count %></td>
</tr>
<tr>
<th class="text-left">Defeats</th>
<td><%= @view.display_losing_games_count %></td>
<td><%= @view.losing_games_count %></td>
</tr>

<tr><td colspan="100%" class="select-none">&nbsp;</td></tr>

<tr>
<th class="text-left">Cells Revealed</th>
<td><%= @view.display_reveals_count %></td>
<td><%= @view.reveals_count %></td>
</tr>
<tr>
<th class="text-left">Chords</th>
<td><%= @view.display_chords_count %></td>
<td><%= @view.chords_count %></td>
</tr>
<tr>
<th class="text-left">Flags Placed</th>
<td><%= @view.display_flags_count %></td>
<td><%= @view.flags_count %></td>
</tr>
<tr>
<th class="text-left">Flags Removed</th>
<td><%= @view.display_unflags_count %></td>
<td><%= @view.unflags_count %></td>
</tr>
<tr>
<th class="text-left">Mines Detonated</th>
<td><%= @view.display_tripped_mines_count %></td>
<td><%= @view.tripped_mines_count %></td>
</tr>

<tr><td colspan="100%" class="select-none">&nbsp;</td></tr>

<tr>
<th class="text-left">Best Score</th>
<td><%= @view.display_best_score %></td>
<td><%= @view.best_score %></td>
</tr>
<tr>
<th class="text-left">Best 3BV/s</th>
<td><%= @view.display_best_bbbvps %></td>
<td><%= @view.best_bbbvps %></td>
</tr>
<tr>
<th class="text-left">Best Efficiency</th>
<td><%= @view.display_best_efficiency %></td>
<td><%= @view.best_efficiency %></td>
</tr>
</tbody>
</table>
Expand Down
84 changes: 44 additions & 40 deletions app/views/users/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ def initialize(user:)
end

def cache_name
[user, completed_games_count]
[user, _completed_games_count]
end

def display_name = user.display_name
def enlistment_date = I18n.l(user.created_at.to_date)

def display_games_count = delimit(completed_games_count)
def games_count = delimit(_completed_games_count)

def display_winning_games_count
return 0 if winning_games_count.zero?
def winning_games_count
return 0 if _winning_games_count.zero?

"#{winning_games_count} (#{winning_games_percentage})"
"#{_winning_games_count} (#{winning_games_percentage})"
end

def display_losing_games_count
return 0 if losing_games_count.zero?
def losing_games_count
return 0 if _losing_games_count.zero?

"#{losing_games_count} (#{losing_games_percentage})"
"#{_losing_games_count} (#{losing_games_percentage})"
end

def display_reveals_count = delimit(reveals_count)
def display_chords_count = delimit(chords_count)
def display_flags_count = delimit(flags_count)
def display_unflags_count = delimit(unflags_count)
def display_tripped_mines_count = delimit(tripped_mines_count)
def reveals_count = delimit(_reveals_count)
def chords_count = delimit(_chords_count)
def flags_count = delimit(_flags_count)
def unflags_count = delimit(_unflags_count)
def tripped_mines_count = delimit(_tripped_mines_count)

def display_best_score
score = best_score
def best_score
score = _best_score
score ? score.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
end

def display_best_bbbvps
bbbvps = best_bbbvps
def best_bbbvps
bbbvps = _best_bbbvps
bbbvps ? bbbvps.round(DEFAULT_PRECISION) : NO_VALUE_INDICATOR
end

def display_best_efficiency
efficiency = best_efficiency
def best_efficiency
efficiency = _best_efficiency
efficiency ? percentage(efficiency * 100.0) : NO_VALUE_INDICATOR
end

Expand All @@ -56,40 +56,44 @@ def display_best_efficiency
attr_reader :user

def games = user.games
def cell_reveal_transactions = user.cell_reveal_transactions
def cell_chord_transactions = user.cell_chord_transactions
def cell_flag_transactions = user.cell_flag_transactions
def cell_unflag_transactions = user.cell_unflag_transactions
def revealed_cells = user.revealed_cells

def completed_games_count
@completed_games_count ||= games.for_game_over_statuses.size
def _completed_games_count
@_completed_games_count ||= games.for_game_over_statuses.size
end

def winning_games_percentage = percentage(winning_games_percent)
def winning_games_percent = winning_games_ratio * 100.0
def winning_games_ratio = winning_games_count / completed_games_count.to_f
def winning_games_ratio = _winning_games_count / _completed_games_count.to_f

def winning_games_count
@winning_games_count ||= games.for_status_alliance_wins.size
def _winning_games_count
@_winning_games_count ||= games.for_status_alliance_wins.size
end

def losing_games_percentage = percentage(losing_games_percent)
def losing_games_percent = losing_games_ratio * 100.0
def losing_games_ratio = losing_games_count / completed_games_count.to_f
def losing_games_ratio = _losing_games_count / _completed_games_count.to_f

def losing_games_count
@losing_games_count ||= games.for_status_mines_win.size
def _losing_games_count
@_losing_games_count ||= games.for_status_mines_win.size
end

def reveals_count = cell_reveal_transactions.size
def chords_count = cell_chord_transactions.size
def flags_count = cell_flag_transactions.size
def unflags_count = cell_unflag_transactions.size
def tripped_mines_count = revealed_cells.is_mine.size
def best_score = user.games.by_score_asc.pick(:score)
def best_bbbvps = user.games.by_bbbvps_desc.pick(:bbbvps)
def best_efficiency = user.games.by_efficiency_desc.pick(:efficiency)
def _reveals_count = cell_reveal_transactions.size
def cell_reveal_transactions = user.cell_reveal_transactions

def _chords_count = cell_chord_transactions.size
def cell_chord_transactions = user.cell_chord_transactions

def _flags_count = cell_flag_transactions.size
def cell_flag_transactions = user.cell_flag_transactions

def _unflags_count = cell_unflag_transactions.size
def cell_unflag_transactions = user.cell_unflag_transactions

def _tripped_mines_count = user.revealed_cells.is_mine.size

def _best_score = user.games.by_score_asc.pick(:score)
def _best_bbbvps = user.games.by_bbbvps_desc.pick(:bbbvps)
def _best_efficiency = user.games.by_efficiency_desc.pick(:efficiency)

def percentage(value, precision: DEFAULT_PRECISION)
helpers.number_to_percentage(value, precision:)
Expand Down

0 comments on commit 2484120

Please sign in to comment.