kosen-venture/RocketAnswer

View on GitHub
app/views/ranking/index.html.erb

Summary

Maintainability
Test Coverage
<%= content_for :title do %>解答の投稿数ランキング<% end %>

<div class="page-header">
  <h1><i class="fa fa-trophy" aria-hidden="true"></i> 解答の投稿数ランキング</h1>
</div>

<div id="tab">
  <div class="tab_area clearfix">
    <ul>
      <li class="<%= active_tab() %>" ><%= link_to "全期間", ranking_path %></li>
      <li class="<%= active_tab(1.month.ago.to_date) %>" ><%= link_to "1ヶ月", ranking_path(begin_date: 1.month.ago.to_date.to_s) %></li>
      <li class="<%= active_tab(6.month.ago.to_date) %>" ><%= link_to "6ヶ月", ranking_path(begin_date: 6.months.ago.to_date.to_s) %></li>
      <li class="<%= active_tab(1.year.ago.to_date) %>" ><%= link_to "1年", ranking_path(begin_date: 1.year.ago.to_date.to_s) %></li>
    </ul>
  </div>
</div>

<% if @begin_date.present? %>
  <%= "#{@begin_date} から #{Date.today.to_s}" %>
<% else %>
  全期間
<% end %>
の解答投稿数ランキング

<table class="table table-hover table-bordered">
  <thead>
    <tr>
      <th>順位</th>
      <th>ユーザ名</th>
      <th>投稿数</th>
    </tr>
  </thead>

  <tbody>
    <% rank = 1 %>
    <% count_before = @ranks.first[1] if @ranks.present? %>
    <% @ranks.each_with_index do |(user, count), index| %>
      <% if count < count_before %>
      <%  rank += 1 %>
      <%  count_before = count %>
      <% end %>
      <tr>
        <td><%= trophy(rank) %><%= rank %>位</td>
        <td><%= user.nickname %></td>
        <td><%= count %></td>
      </tr>
    <% end %>
  </tbody>
</table>