1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-03-24 17:04:43 +00:00

Fixed mat assignment rules to be db agnostic with comma delimited strings and upgraded test env db to mariadb 10.10 to match production.

This commit is contained in:
2025-01-25 20:02:22 -05:00
parent 54655a2ea9
commit 690e497654
8 changed files with 76 additions and 60 deletions

View File

@@ -5,8 +5,7 @@ class MatAssignmentRulesController < ApplicationController
def index def index
@mat_assignment_rules = @tournament.mat_assignment_rules @mat_assignment_rules = @tournament.mat_assignment_rules
# Create a hash mapping each Weight ID to its Weight object for quick lookups @weights_by_id = @tournament.weights.index_by(&:id) # For quick lookup
@weights_by_id = @tournament.weights.index_by(&:id)
end end
def new def new
@@ -14,11 +13,6 @@ class MatAssignmentRulesController < ApplicationController
load_form_data load_form_data
end end
def destroy
@mat_assignment_rule.destroy
redirect_to tournament_mat_assignment_rules_path(@tournament), notice: 'Mat assignment rule was successfully deleted.'
end
def create def create
@mat_assignment_rule = @tournament.mat_assignment_rules.build(mat_assignment_rule_params) @mat_assignment_rule = @tournament.mat_assignment_rules.build(mat_assignment_rule_params)
load_form_data load_form_data
@@ -44,6 +38,11 @@ class MatAssignmentRulesController < ApplicationController
end end
end end
def destroy
@mat_assignment_rule.destroy
redirect_to tournament_mat_assignment_rules_path(@tournament), notice: 'Mat assignment rule was successfully deleted.'
end
private private
def set_tournament def set_tournament
@@ -59,20 +58,17 @@ class MatAssignmentRulesController < ApplicationController
end end
def mat_assignment_rule_params def mat_assignment_rule_params
# Ensure defaults to empty arrays for checkboxes
params[:mat_assignment_rule][:weight_classes] ||= [] params[:mat_assignment_rule][:weight_classes] ||= []
params[:mat_assignment_rule][:bracket_positions] ||= [] params[:mat_assignment_rule][:bracket_positions] ||= []
params[:mat_assignment_rule][:rounds] ||= [] params[:mat_assignment_rule][:rounds] ||= []
# Permit parameters and normalize types
params.require(:mat_assignment_rule).permit(:mat_id, weight_classes: [], bracket_positions: [], rounds: []).tap do |whitelisted| params.require(:mat_assignment_rule).permit(:mat_id, weight_classes: [], bracket_positions: [], rounds: []).tap do |whitelisted|
whitelisted[:weight_classes] = whitelisted[:weight_classes].map(&:to_i) whitelisted[:weight_classes] = Array(whitelisted[:weight_classes]).map(&:to_i)
whitelisted[:rounds] = whitelisted[:rounds].map(&:to_i) whitelisted[:rounds] = Array(whitelisted[:rounds]).map(&:to_i)
whitelisted[:bracket_positions] = whitelisted[:bracket_positions] # Assume these are strings whitelisted[:bracket_positions] = Array(whitelisted[:bracket_positions])
end end
end end
# Load data needed for the form
def load_form_data def load_form_data
@available_mats = @tournament.mats @available_mats = @tournament.mats
@unique_bracket_positions = @tournament.matches.select(:bracket_position).distinct.pluck(:bracket_position) @unique_bracket_positions = @tournament.matches.select(:bracket_position).distinct.pluck(:bracket_position)

View File

@@ -48,36 +48,37 @@ class Mat < ApplicationRecord
def next_eligible_match def next_eligible_match
# Start with all matches that are either unfinished (nil or 0), have a bout number, and are ordered by bout_number # Start with all matches that are either unfinished (nil or 0), have a bout number, and are ordered by bout_number
filtered_matches = tournament.matches filtered_matches = tournament.matches
.where(finished: [nil, 0]) # finished is nil or 0 .where(finished: [nil, 0]) # finished is nil or 0
.where(mat_id: nil) # mat_id is nil .where(mat_id: nil) # mat_id is nil
.where.not(bout_number: nil) # bout_number is not nil .where.not(bout_number: nil) # bout_number is not nil
.order(:bout_number) .order(:bout_number)
# .where's are chained as ANDs # Filter out BYE matches
# cannot search for .where.not(loser1_name: "BYE") because it will not find any that are NULL
filtered_matches = filtered_matches filtered_matches = filtered_matches
.where("loser1_name != ? OR loser1_name IS NULL", "BYE") .where("loser1_name != ? OR loser1_name IS NULL", "BYE")
.where("loser2_name != ? OR loser2_name IS NULL", "BYE") .where("loser2_name != ? OR loser2_name IS NULL", "BYE")
# Sequentially apply each rule, narrowing down the matches # Apply mat assignment rules
mat_assignment_rules.each do |rule| mat_assignment_rules.each do |rule|
if rule.weight_classes.any? if rule.weight_classes.any?
filtered_matches = filtered_matches.where(weight_id: rule.weight_classes) # Ensure weight_classes is treated as an array
filtered_matches = filtered_matches.where(weight_id: Array(rule.weight_classes).map(&:to_i))
end end
if rule.bracket_positions.any? if rule.bracket_positions.any?
filtered_matches = filtered_matches.where(bracket_position: rule.bracket_positions) # Ensure bracket_positions is treated as an array
filtered_matches = filtered_matches.where(bracket_position: Array(rule.bracket_positions).map(&:to_s))
end end
if rule.rounds.any? if rule.rounds.any?
filtered_matches = filtered_matches.where(round: rule.rounds) # Ensure rounds is treated as an array
filtered_matches = filtered_matches.where(round: Array(rule.rounds).map(&:to_i))
end end
end end
# Return the first match in filtered results, or nil if none are left # Return the first match in filtered results, or nil if none are left
result = filtered_matches.first filtered_matches.first
result end
end
def unfinished_matches def unfinished_matches

View File

@@ -2,11 +2,28 @@ class MatAssignmentRule < ApplicationRecord
belongs_to :mat belongs_to :mat
belongs_to :tournament belongs_to :tournament
# Ensure default values for JSON fields # Convert comma-separated values to arrays
# because mysql doesn't allow this def weight_classes
after_initialize do (super || "").split(",").map(&:to_i)
self.weight_classes ||= [] end
self.bracket_positions ||= []
self.rounds ||= [] def weight_classes=(value)
super(value.is_a?(Array) ? value.join(",") : value)
end
def bracket_positions
(super || "").split(",")
end
def bracket_positions=(value)
super(value.is_a?(Array) ? value.join(",") : value)
end
def rounds
(super || "").split(",").map(&:to_i)
end
def rounds=(value)
super(value.is_a?(Array) ? value.join(",") : value)
end end
end end

View File

@@ -13,9 +13,9 @@
<div> <div>
<%= form.label :weight_classes, "Allowed Weight Classes" %><br> <%= form.label :weight_classes, "Allowed Weight Classes" %><br>
<% if @tournament.weights.any? %> <% if @tournament.weights.any? %>
<% @tournament.weights.each do |weight| %> <% @tournament.weights.sort_by{|w| w.max}.each do |weight| %>
<div> <div>
<%= check_box_tag "mat_assignment_rule[weight_classes][]", weight.id, @mat_assignment_rule.weight_classes.map(&:to_i).include?(weight.id) %> <%= check_box_tag "mat_assignment_rule[weight_classes][]", weight.id, Array(@mat_assignment_rule.weight_classes).map(&:to_i).include?(weight.id) %>
<%= label_tag "mat_assignment_rule_weight_classes_#{weight.id}", weight.max %> <%= label_tag "mat_assignment_rule_weight_classes_#{weight.id}", weight.max %>
</div> </div>
<% end %> <% end %>
@@ -30,7 +30,7 @@
<% if @unique_bracket_positions.present? %> <% if @unique_bracket_positions.present? %>
<% @unique_bracket_positions.each do |position| %> <% @unique_bracket_positions.each do |position| %>
<div> <div>
<%= check_box_tag "mat_assignment_rule[bracket_positions][]", position, @mat_assignment_rule.bracket_positions.include?(position) %> <%= check_box_tag "mat_assignment_rule[bracket_positions][]", position, Array(@mat_assignment_rule.bracket_positions).include?(position) %>
<%= label_tag "mat_assignment_rule_bracket_positions_#{position}", position %> <%= label_tag "mat_assignment_rule_bracket_positions_#{position}", position %>
</div> </div>
<% end %> <% end %>
@@ -45,7 +45,7 @@
<% if @unique_rounds.present? %> <% if @unique_rounds.present? %>
<% @unique_rounds.each do |round| %> <% @unique_rounds.each do |round| %>
<div> <div>
<%= check_box_tag "mat_assignment_rule[rounds][]", round, @mat_assignment_rule.rounds.map(&:to_i).include?(round) %> <%= check_box_tag "mat_assignment_rule[rounds][]", round, Array(@mat_assignment_rule.rounds).map(&:to_i).include?(round) %>
<%= label_tag "mat_assignment_rule_rounds_#{round}", round %> <%= label_tag "mat_assignment_rule_rounds_#{round}", round %>
</div> </div>
<% end %> <% end %>

View File

@@ -5,30 +5,25 @@
<th>Weight Classes (Max)</th> <th>Weight Classes (Max)</th>
<th>Bracket Positions</th> <th>Bracket Positions</th>
<th>Rounds</th> <th>Rounds</th>
<th><%= link_to ' New Mat Assignment Rule', new_tournament_mat_assignment_rule_path(@tournament), :class=>"fas fa-plus" %></th> <th><%= link_to ' New Mat Assignment Rule', new_tournament_mat_assignment_rule_path(@tournament), class: "fas fa-plus" %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @mat_assignment_rules.each do |rule| %> <% @mat_assignment_rules.each do |rule| %>
<tr> <tr>
<td><%= rule.mat.name %></td> <td><%= rule.mat.name %></td>
<!-- Display max values for each weight associated with the rule -->
<td> <td>
<% rule.weight_classes.each do |weight_id| %> <% Array(rule.weight_classes).each_with_index do |weight_id, index| %>
<% weight = @weights_by_id[weight_id] %> <% weight = @weights_by_id[weight_id.to_i] %>
<%= weight ? weight.max : "N/A" %><%= ", " unless weight_id == rule.weight_classes.last %> <%= weight ? weight.max : "N/A" %>
<%= ", " unless index == Array(rule.weight_classes).size - 1 %>
<% end %> <% end %>
</td> </td>
<td><%= Array(rule.bracket_positions).join(", ") %></td>
<!-- Display bracket positions and rounds --> <td><%= Array(rule.rounds).join(", ") %></td>
<td><%= rule.bracket_positions.join(", ") %></td>
<td><%= rule.rounds.join(", ") %></td>
<!-- Edit and Delete Actions -->
<td> <td>
<%= link_to '', edit_tournament_mat_assignment_rule_path(@tournament, rule), :class=>"fas fa-edit" %> <%= link_to '', edit_tournament_mat_assignment_rule_path(@tournament, rule), class: "fas fa-edit" %>
<%= link_to '', tournament_mat_assignment_rule_path(@tournament, rule), method: :delete, data: { confirm: "Are you sure?" }, :class=>"fas fa-trash-alt" %> <%= link_to '', tournament_mat_assignment_rule_path(@tournament, rule), method: :delete, data: { confirm: "Are you sure?" }, class: "fas fa-trash-alt" %>
</td> </td>
</tr> </tr>
<% end %> <% end %>

View File

@@ -0,0 +1,7 @@
class UseCommaDelimmStringForMatAssignmentRules < ActiveRecord::Migration[7.2]
def change
change_column :mat_assignment_rules, :weight_classes, :string
change_column :mat_assignment_rules, :bracket_positions, :string
change_column :mat_assignment_rules, :rounds, :string
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_01_22_142911) do ActiveRecord::Schema[7.2].define(version: 2025_01_26_004721) do
create_table "delayed_jobs", force: :cascade do |t| create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false t.integer "priority", default: 0, null: false
t.integer "attempts", default: 0, null: false t.integer "attempts", default: 0, null: false
@@ -31,9 +31,9 @@ ActiveRecord::Schema[7.2].define(version: 2025_01_22_142911) do
create_table "mat_assignment_rules", force: :cascade do |t| create_table "mat_assignment_rules", force: :cascade do |t|
t.integer "tournament_id", null: false t.integer "tournament_id", null: false
t.integer "mat_id", null: false t.integer "mat_id", null: false
t.json "weight_classes" t.string "weight_classes"
t.json "bracket_positions" t.string "bracket_positions"
t.json "rounds" t.string "rounds"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["mat_id"], name: "index_mat_assignment_rules_on_mat_id", unique: true t.index ["mat_id"], name: "index_mat_assignment_rules_on_mat_id", unique: true

View File

@@ -41,7 +41,7 @@ services:
test: curl http://127.0.0.1/ test: curl http://127.0.0.1/
db: db:
image: mysql:5.7 image: mariadb:10.10
ports: ports:
- "3306:3306" - "3306:3306"
volumes: volumes: