From 63b55ae1fc24a5c01552003a5ecd4f93946cd133 Mon Sep 17 00:00:00 2001 From: Yannis JAQUET Date: Sat, 3 Aug 2024 11:58:23 +0200 Subject: [PATCH] Filter shinpans out of the kenshi list --- app/controllers/kenshis_controller.rb | 13 +------------ app/models/kenshi.rb | 8 ++++++++ app/views/layouts/_navigation.html.erb | 4 ++-- spec/models/kenshi_spec.rb | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/controllers/kenshis_controller.rb b/app/controllers/kenshis_controller.rb index 704d2f28..8e9f139b 100644 --- a/app/controllers/kenshis_controller.rb +++ b/app/controllers/kenshis_controller.rb @@ -20,20 +20,9 @@ def index @title = t("kenshis.index.title_#{category}") end @kenshis = @kenshis + .not_shinpans .includes(:user, :club, participations: [:category]) .order(created_at: :desc) - respond_with @kenshis do |format| - format.html - format.csv { - filename = Time.zone.now.to_fs(:datetime).gsub(/[^0-9a-z]/, - "") + "_" + @title.gsub(/[^0-9a-zA-Z]/, "_").gsub("__", "_") + ".csv" - send_data( - Kenshi.to_csv(@kenshis), - as: "text/csv; charset=utf-8; header=present", - filename: filename - ) - } - end end def show diff --git a/app/models/kenshi.rb b/app/models/kenshi.rb index 7ca02643..9c26c5dd 100644 --- a/app/models/kenshi.rb +++ b/app/models/kenshi.rb @@ -33,6 +33,14 @@ class Kenshi < ApplicationRecord after_create_commit :notify_slack after_commit :update_purchase + scope :shinpans, -> { + where(shinpan: true).where.missing(:participations) + } + + scope :not_shinpans, -> { + where.not(id: shinpans) + } + def self.from(user) new( first_name: user.first_name, diff --git a/app/views/layouts/_navigation.html.erb b/app/views/layouts/_navigation.html.erb index c441b782..565b508d 100644 --- a/app/views/layouts/_navigation.html.erb +++ b/app/views/layouts/_navigation.html.erb @@ -42,7 +42,7 @@ <%= link_to t(".about"), about_path, class: "text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium" %> <%= link_to( - t("navigation.kenshis.text", kenshis: current_cup.kenshis.count), + t("navigation.kenshis.text", kenshis: current_cup.kenshis.not_shinpans.count), cup_kenshis_path(current_cup), class: "text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium" ) @@ -114,7 +114,7 @@ <%= link_to t(".about"), about_path, class: "text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium" %> <%= link_to( - t("navigation.kenshis.text", kenshis: current_cup.kenshis.count), + t("navigation.kenshis.text", kenshis: current_cup.kenshis.not_shinpans.count), cup_kenshis_path(current_cup), class: "text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium" ) diff --git a/spec/models/kenshi_spec.rb b/spec/models/kenshi_spec.rb index 5d38fb0c..2b29e021 100644 --- a/spec/models/kenshi_spec.rb +++ b/spec/models/kenshi_spec.rb @@ -49,6 +49,21 @@ end end + describe "Scopes" do + describe ".shinpans, .not_shinpans" do + let(:cup) { create(:cup) } + let!(:shinpan) { create(:kenshi, shinpan: true, cup: cup) } + let!(:kenshi) { create(:kenshi, cup: cup) } + let!(:shinpan_with_participation) { create(:kenshi, shinpan: true, cup: cup) } + let!(:participation) { create(:participation, kenshi: shinpan_with_participation) } + + it do + expect(described_class.shinpans).to eq [shinpan] + expect(described_class.not_shinpans).to contain_exactly(kenshi, shinpan_with_participation) + end + end + end + describe "Callbacks" do describe "after_create_commit" do let(:kenshi) { build(:kenshi) }