Skip to content

Commit

Permalink
Display remaining spots for products on home page (#577)
Browse files Browse the repository at this point in the history
Co-authored-by: Yannis JAQUET <[email protected]>
  • Loading branch information
yannis and yannis authored Aug 3, 2024
1 parent 41d39de commit ace3e00
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
19 changes: 16 additions & 3 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ class Product < ApplicationRecord

delegate :year, to: :cup

def still_available?
def remaining_spots
return if quota.nil? && !require_personal_infos

if require_personal_infos
dormitory_still_available
kenshis_in_dormitory_for_cup = Kenshi
.joins(purchases: :product)
.merge(Product.where(cup_id: cup_id, require_personal_infos: true))
.distinct

ENV.fetch("DORMITORY_QUOTA", 50).to_i - kenshis_in_dormitory_for_cup.count
else
quota.nil? || purchases.count < quota
quota - purchases.count
end
end

def still_available?
return true if remaining_spots.nil?

remaining_spots.positive?
end

def dormitory_still_available
# We need this validation as in our dormitory, a kenshi cannot reuse the bed
# of another kenshi another night.
Expand Down
7 changes: 6 additions & 1 deletion app/views/cups/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@
<% if product.description.present? %>
<em class="text-gray-500 ">(<%= product.description %>)</em>
<% end %>
<% if !product.still_available? %>

<% if product.still_available? %>
<% if product.remaining_spots %>
<span class="text-brand italic">(<%= t(".spots_left", count: product.remaining_spots) %> )</span>
<% end %>
<% else %>
<span class="text-brand italic">(<%= t(".products.not_available") %>)</span>
<% end %>
</th>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/views/cups.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ en:
<a id="google_map" href="https://maps.google.ch/maps?hl=fr&amp;ie=UTF8&amp;q=centre+sportif+du+bout+du+monde&amp;fb=1&amp;gl=ch&amp;hq=centre+sportif+du+bout+du+monde&amp;cid=0,0,2906637816380334854&amp;t=m&amp;ll=46.182846,6.156163&amp;spn=0.007131,0.013733&amp;z=16" target="_blank" rel="noopener noreferrer">Google map to the competition hall</a>
map_link: Larger map
title: Venue
spots_left:
one: 1 spot left
other: "%{count} spots left"
3 changes: 3 additions & 0 deletions config/locales/views/cups.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ fr:
<a id="google_map" href="https://maps.google.ch/maps?hl=fr&amp;ie=UTF8&amp;q=centre+sportif+du+bout+du+monde&amp;fb=1&amp;gl=ch&amp;hq=centre+sportif+du+bout+du+monde&amp;cid=0,0,2906637816380334854&amp;t=m&amp;ll=46.182846,6.156163&amp;spn=0.007131,0.013733&amp;z=16" target="_blank" rel="noopener noreferrer">Google map vers la salle de compétition</a>
map_link: Agrandir le plan
title: Où est-ce que ça se passe?
spots_left:
one: 1 place restante
other: "%{count} places restantes"
29 changes: 23 additions & 6 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@
end
end

describe "#still_available?" do
describe "#remaining_spots, #still_available?" do
context "when product require_personal_infos" do
let(:cup) { create(:cup) }
let!(:product_1) { create(:product, cup: cup, quota: 3, require_personal_infos: true) }
let!(:product_2) { create(:product, cup: cup, quota: 3, require_personal_infos: true) }
let!(:product_3) { create(:product, cup: cup, quota: 3, require_personal_infos: false) }
let!(:kenshi) { create(:kenshi) }
let(:purchase) { build(:purchase, product: product_1) }

before do
ENV["DORMITORY_QUOTA"] = "4"
Expand All @@ -64,9 +63,12 @@
after { ENV["DORMITORY_QUOTA"] = nil }

context "when product quota is not reached" do
before { create_list(:purchase, 1, product: product_2) }
before { create(:purchase, product: product_2) }

it do
expect(product_1.remaining_spots).to eq 1
expect(product_2.remaining_spots).to eq 1
expect(product_3.remaining_spots).to eq 3
expect(product_1).to be_still_available
expect(product_2).to be_still_available
expect(product_3).to be_still_available
Expand All @@ -78,6 +80,9 @@
before { create_list(:purchase, 2, product: product_2) }

it do
expect(product_1.remaining_spots).to eq 0
expect(product_2.remaining_spots).to eq 0
expect(product_3.remaining_spots).to eq 3
expect(product_1).not_to be_still_available
expect(product_2).not_to be_still_available
expect(product_3).to be_still_available
Expand All @@ -91,6 +96,9 @@
end

it do
expect(product_1.remaining_spots).to eq 1
expect(product_2.remaining_spots).to eq 1
expect(product_3.remaining_spots).to eq 3
expect(product_1).to be_still_available
expect(product_2).to be_still_available
expect(product_3).to be_still_available
Expand All @@ -110,19 +118,28 @@
context "without quota" do
let(:quota) { nil }

it { expect(product).to be_still_available }
it do
expect(product.remaining_spots).to be_nil
expect(product).to be_still_available
end
end

context "with purchases count < quota" do
let(:quota) { 3 }

it { expect(product).to be_still_available }
it do
expect(product.remaining_spots).to eq 1
expect(product).to be_still_available
end
end

context "with purchases count == quota" do
let(:quota) { 2 }

it { expect(product).not_to be_still_available }
it do
expect(product.remaining_spots).to eq 0
expect(product).not_to be_still_available
end
end
end
end
Expand Down

0 comments on commit ace3e00

Please sign in to comment.