Skip to content

Commit

Permalink
Filter shinpans out of the kenshi list
Browse files Browse the repository at this point in the history
  • Loading branch information
yannis committed Aug 3, 2024
1 parent 4da7050 commit 63b55ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
13 changes: 1 addition & 12 deletions app/controllers/kenshis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/models/kenshi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"
)
Expand Down
15 changes: 15 additions & 0 deletions spec/models/kenshi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 63b55ae

Please sign in to comment.