mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Fixed N+1 on up_matches and added html cache.
This commit is contained in:
@@ -8,6 +8,8 @@ class Mat < ApplicationRecord
|
||||
|
||||
QUEUE_SLOTS = %w[queue1 queue2 queue3 queue4].freeze
|
||||
|
||||
after_save :clear_queue_matches_cache
|
||||
|
||||
def assign_next_match
|
||||
slot = first_empty_queue_slot
|
||||
return true unless slot
|
||||
@@ -86,8 +88,20 @@ class Mat < ApplicationRecord
|
||||
QUEUE_SLOTS.map { |slot| public_send(slot) }
|
||||
end
|
||||
|
||||
# used to prevent N+1 query on each mat
|
||||
def queue_matches
|
||||
queue_match_ids.map { |match_id| match_id ? Match.find_by(id: match_id) : nil }
|
||||
slot_ids = queue_match_ids
|
||||
if @queue_matches.nil? || @queue_match_slot_ids != slot_ids
|
||||
ids = slot_ids.compact
|
||||
@queue_matches = if ids.empty?
|
||||
[nil, nil, nil, nil]
|
||||
else
|
||||
matches_by_id = Match.where(id: ids).index_by(&:id)
|
||||
slot_ids.map { |match_id| match_id ? matches_by_id[match_id] : nil }
|
||||
end
|
||||
@queue_match_slot_ids = slot_ids
|
||||
end
|
||||
@queue_matches
|
||||
end
|
||||
|
||||
def queue1_match
|
||||
@@ -175,9 +189,13 @@ class Mat < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def clear_queue_matches_cache
|
||||
@queue_matches = nil
|
||||
@queue_match_slot_ids = nil
|
||||
end
|
||||
|
||||
def queue_match_at(position)
|
||||
match_id = public_send("queue#{position}")
|
||||
match_id ? Match.find_by(id: match_id) : nil
|
||||
queue_matches[position - 1]
|
||||
end
|
||||
|
||||
def first_empty_queue_slot
|
||||
|
||||
34
app/views/tournaments/_up_matches_mat_row.html.erb
Normal file
34
app/views/tournaments/_up_matches_mat_row.html.erb
Normal file
@@ -0,0 +1,34 @@
|
||||
<% queue1_match, queue2_match, queue3_match, queue4_match = mat.queue_matches %>
|
||||
<% cache ["up_matches_mat_row", mat, mat.queue1, mat.queue2, mat.queue3, mat.queue4] do %>
|
||||
<tr>
|
||||
<td><%= mat.name %></td>
|
||||
<td>
|
||||
<% if queue1_match %><strong><%= queue1_match.bout_number %></strong> (<%= queue1_match.bracket_position %>)<br>
|
||||
<%= queue1_match.weight_max %> lbs
|
||||
<br><%= queue1_match.w1_bracket_name %> vs. <br>
|
||||
<%= queue1_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if queue2_match %><strong><%= queue2_match.bout_number %></strong> (<%= queue2_match.bracket_position %>)<br>
|
||||
<%= queue2_match.weight_max %> lbs
|
||||
<br><%= queue2_match.w1_bracket_name %> vs. <br>
|
||||
<%= queue2_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if queue3_match %><strong><%= queue3_match.bout_number %></strong> (<%= queue3_match.bracket_position %>)<br>
|
||||
<%= queue3_match.weight_max %> lbs
|
||||
<br><%= queue3_match.w1_bracket_name %> vs. <br>
|
||||
<%= queue3_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if queue4_match %><strong><%= queue4_match.bout_number %></strong> (<%= queue4_match.bracket_position %>)<br>
|
||||
<%= queue4_match.weight_max %> lbs
|
||||
<br><%= queue4_match.w1_bracket_name %> vs. <br>
|
||||
<%= queue4_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
@@ -1,4 +1,3 @@
|
||||
<% cache ["#{@tournament.id}_up_matches", @tournament] do %>
|
||||
<script>
|
||||
// $(document).ready(function() {
|
||||
// $('#matchList').dataTable();
|
||||
@@ -42,37 +41,7 @@
|
||||
|
||||
<tbody>
|
||||
<% @mats.each.map do |m| %>
|
||||
<tr>
|
||||
<td><%= m.name %></td>
|
||||
<td>
|
||||
<% if m.queue1_match %><strong><%=m.queue1_match.bout_number%></strong> (<%= m.queue1_match.bracket_position %>)<br>
|
||||
<%= m.queue1_match.weight_max %> lbs
|
||||
<br><%= m.queue1_match.w1_bracket_name %> vs. <br>
|
||||
<%= m.queue1_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if m.queue2_match %><strong><%=m.queue2_match.bout_number%></strong> (<%= m.queue2_match.bracket_position %>)<br>
|
||||
<%= m.queue2_match.weight_max %> lbs
|
||||
<br><%= m.queue2_match.w1_bracket_name %> vs. <br>
|
||||
<%= m.queue2_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if m.queue3_match %><strong><%=m.queue3_match.bout_number%></strong> (<%= m.queue3_match.bracket_position %>)<br>
|
||||
<%= m.queue3_match.weight_max %> lbs
|
||||
<br><%= m.queue3_match.w1_bracket_name %> vs. <br>
|
||||
<%= m.queue3_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td>
|
||||
<% if m.queue4_match %><strong><%=m.queue4_match.bout_number%></strong> (<%= m.queue4_match.bracket_position %>)<br>
|
||||
<%= m.queue4_match.weight_max %> lbs
|
||||
<br><%= m.queue4_match.w1_bracket_name %> vs. <br>
|
||||
<%= m.queue4_match.w2_bracket_name %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<%= render "up_matches_mat_row", mat: m %>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -104,4 +73,3 @@
|
||||
</table>
|
||||
|
||||
<br>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user