From 23e0a32fd7efe878afcbebb5592856b055413b6b Mon Sep 17 00:00:00 2001 From: moveson Date: Mon, 30 Oct 2023 09:32:18 -0600 Subject: [PATCH] Don't show Best Performance selector if results include nonbinary category --- app/models/results_template.rb | 7 +++++++ app/presenters/podium_presenter.rb | 7 ++++++- app/views/events/podium.html.erb | 12 +++++++----- spec/models/results_template_spec.rb | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/models/results_template.rb b/app/models/results_template.rb index 25a3697aa..684c9bd43 100644 --- a/app/models/results_template.rb +++ b/app/models/results_template.rb @@ -19,10 +19,12 @@ class ResultsTemplate < ApplicationRecord validates_presence_of :name, :aggregation_method + # @return [ResultsTemplate] def self.default find_by(slug: "simple") end + # @return [ResultsTemplate] def dup_with_categories # This must be done first or relations will be lost set_category_positions @@ -32,6 +34,11 @@ def dup_with_categories template end + # @return [Boolean] + def includes_nonbinary? + results_categories.where(nonbinary: true).exists? + end + private def set_category_positions diff --git a/app/presenters/podium_presenter.rb b/app/presenters/podium_presenter.rb index de48ee6dc..ae120df78 100644 --- a/app/presenters/podium_presenter.rb +++ b/app/presenters/podium_presenter.rb @@ -4,7 +4,7 @@ class PodiumPresenter < BasePresenter attr_reader :event delegate :name, :course, :course_name, :organization, :organization_name, :to_param, :multiple_laps?, - :event_group, :ordered_events_within_group, :scheduled_start_time_local, to: :event + :event_group, :ordered_events_within_group, :results_template, :scheduled_start_time_local, to: :event delegate :available_live, :multiple_events?, to: :event_group delegate :course_groups, to: :course @@ -19,6 +19,11 @@ def categories template&.results_categories || [] end + # @return [Array] + def sort_methods + results_template.includes_nonbinary? ? [:category] : [:category, :best_performance] + end + # @return [Array] def sorted_categories if performance_sort? diff --git a/app/views/events/podium.html.erb b/app/views/events/podium.html.erb index 0a5d8c15f..ebdd3c180 100644 --- a/app/views/events/podium.html.erb +++ b/app/views/events/podium.html.erb @@ -7,7 +7,7 @@
-

<%= [@presenter.name, nil].compact.join(": ") %>

+

<%= [@presenter.name, nil].compact.join(": ") %>

- <% [:category, :best_performance].each do |sort_method| %> - <%= link_to sort_method.to_s.titleize, - request.params.merge(sort: sort_method), - class: "btn #{ @presenter.sort_method == sort_method ? 'btn-primary' : 'btn-outline-secondary' }" %> + <% if @presenter.sort_methods.many? %> + <% @presenter.sort_methods.each do |sort_method| %> + <%= link_to sort_method.to_s.titleize, + request.params.merge(sort: sort_method), + class: "btn #{ @presenter.sort_method == sort_method ? 'btn-primary' : 'btn-outline-secondary' }" %> + <% end %> <% end %>
diff --git a/spec/models/results_template_spec.rb b/spec/models/results_template_spec.rb index 17bc61e42..c0aeb28b1 100644 --- a/spec/models/results_template_spec.rb +++ b/spec/models/results_template_spec.rb @@ -11,4 +11,20 @@ expect { results_template.save }.to change { ResultsTemplate.count }.by(1) end end + + describe "#includes_nonbinary?" do + let(:result) { results_template.includes_nonbinary? } + + context "when the template includes a nonbinary category" do + let(:results_template) { results_templates(:masters_and_grandmasters_with_nonbinary) } + + it { expect(result).to eq(true)} + end + + context "when the template does not include a nonbinary category" do + let(:results_template) { results_templates(:masters_and_grandmasters) } + + it { expect(result).to eq(false)} + end + end end