Skip to content

Commit 7953e24

Browse files
authored
[Affiliations] Hide bad data and add metadata validation (#13131)
## Summary of the problem <!-- Why are these changes being made? What problem does it solve? Link any related issues to provide more details. --> https://appsignal.com/hack-club/sites/6596247683eb67648f30f807/exceptions/incidents/2761 Our frontend validations seem to have been bypassed, and affiliations with metadata `{}` were created. ## Describe your changes <!-- Explain your thought process to the solution and provide a quick summary of the changes. --> - Adds a `nonempty` scope to `Event::Affiliation` to hide any existing affiliations with empty metadata - Adds a `metadata_contains_required_fields` validation to ensure that all affiliation types have backend validations that match their frontend validations <!-- If there are any visual changes, please attach images, videos, or gifs. -->
1 parent 48554c9 commit 7953e24

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

app/models/event/affiliation.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Affiliation < ApplicationRecord
2727
store_accessor :metadata, :league, :team_number, :size, :venue_name
2828

2929
scope :robotics, -> { where(name: %w[first vex]) }
30+
scope :nonempty, -> { where.not(metadata: {}) }
31+
32+
validate :metadata_contains_required_fields
3033

3134
def display_name
3235
case name
@@ -59,6 +62,26 @@ def to_s
5962
[display_name, league&.upcase, team_number, size&.positive? ? pluralize(size, "people") : nil].compact.join(" – ")
6063
end
6164

65+
private
66+
67+
def metadata_contains_required_fields
68+
required_fields = case name
69+
when "first"
70+
["league", "team_number", "size"]
71+
when "vex"
72+
["league", "team_number", "size"]
73+
when "hack_club"
74+
["venue_name", "size"]
75+
else
76+
return errors.add(:name, "is not a valid affiliation")
77+
end
78+
79+
missing_fields = required_fields.select { |field| metadata[field].nil? }
80+
if missing_fields.any?
81+
errors.add(:metadata, "is missing fields: #{missing_fields.to_sentence}")
82+
end
83+
end
84+
6285
end
6386

6487
end

app/views/event/applications/project_info.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
<% end %>
144144
</div>
145145

146-
<%= render partial: "events/settings/affiliation", collection: @application.affiliations %>
146+
<%= render partial: "events/settings/affiliation", collection: @application.affiliations.nonempty %>
147147
<% end %>
148148
</div>
149149

app/views/events/settings/_affiliations.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
<% unless event.affiliations.none? %>
1313
<h3 class="mb2"><%= possessive(event.name) %> affiliations</h3>
1414
<% end %>
15-
<%= render partial: "events/settings/affiliation", collection: event.affiliations %>
15+
<%= render partial: "events/settings/affiliation", collection: event.affiliations.nonempty %>
1616
<% end %>

0 commit comments

Comments
 (0)