Skip to content

Commit

Permalink
Add new product references to Cup
Browse files Browse the repository at this point in the history
  • Loading branch information
yannis committed Jan 2, 2024
1 parent e2291bd commit e0f1c50
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 38 deletions.
17 changes: 10 additions & 7 deletions app/admin/cup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
menu priority: 1

permit_params :year, :start_on, :end_on, :deadline, :canceled_at, :registerable_at, :description_en, :description_fr,
:header_image, :product_junior_id, :product_adult_id
:header_image, :product_individual_junior_id, :product_individual_adult_id

controller do
def find_resource
Expand All @@ -21,15 +21,15 @@ def find_resource
f.input :canceled_at, as: :datepicker
f.input :registerable_at, as: :datepicker
f.input(
:product_junior_id,
:product_individual_junior_id,
as: :select,
collection: f.object.products
.order(:position)
.where("LOWER(products.name_en) ~ 'junior'")
.map { |p| [p.name, p.id] }
)
f.input(
:product_adult_id,
:product_individual_adult_id,
as: :select,
collection: f.object.products
.order(:position)
Expand All @@ -51,8 +51,8 @@ def find_resource
column :end_on
column :deadline
column :canceled?
column :product_junior do |cup|
cup.product_junior&.name
column :product_individual_junior do |cup|
cup.product_individual_junior&.name
end
column :product_adult do |cup|
cup.product_adult&.name
Expand All @@ -79,8 +79,11 @@ def find_resource
row :deadline
row :canceled?
row :registerable_at
row :product_junior do |cup|
link_to cup.product_junior&.name, [:admin, cup.product_junior] if cup.product_junior
row :product_individual_junior do |cup|
if cup.product_individual_junior
link_to cup.product_individual_junior&.name,
[:admin, cup.product_individual_junior]
end
end
row :product_adult do |cup|
link_to cup.product_adult&.name, [:admin, cup.product_adult] if cup.product_adult
Expand Down
4 changes: 2 additions & 2 deletions app/models/cup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

require "translate"
class Cup < ApplicationRecord
belongs_to :product_junior, class_name: "Product", optional: true
belongs_to :product_adult, class_name: "Product", optional: true
belongs_to :product_individual_junior, class_name: "Product", optional: true
belongs_to :product_individual_adult, class_name: "Product", optional: true
has_many :kenshis, inverse_of: :cup, dependent: :destroy
has_many :participations, through: :kenshis
has_many :individual_categories, inverse_of: :cup, dependent: :destroy
Expand Down
4 changes: 2 additions & 2 deletions app/models/participation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Participation < ApplicationRecord
delegate :grade, to: "kenshi", allow_nil: true
delegate :club, to: "kenshi", allow_nil: true
delegate :cup, to: "kenshi", allow_nil: true
delegate :product_junior, :product_adult, to: :cup
delegate :product_individual_junior, :product_individual_adult, to: :cup

def self.no_pool
where(pool_number: nil)
Expand All @@ -45,7 +45,7 @@ def category_team
end

def product
kenshi.junior? ? product_junior : product_adult
kenshi.junior? ? product_individual_junior : product_individual_adult
end

def purchase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AddAndRenameProductReferencesToCups < ActiveRecord::Migration[7.1]
def change
add_reference :cups, :product_team_junior, foreign_key: { to_table: :products }, null: true
add_reference :cups, :product_team_adult, foreign_key: { to_table: :products }, null: true
add_reference :cups, :product_full_junior, foreign_key: { to_table: :products }, null: true
add_reference :cups, :product_full_adult, foreign_key: { to_table: :products }, null: true

rename_column :cups, :product_junior_id, :product_individual_junior_id
rename_column :cups, :product_adult_id, :product_individual_adult_id
end
end
26 changes: 19 additions & 7 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/tasks/temporary/cups/add_2023.rake
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,10 @@ namespace :temporary do
team_categories_data.each do |team_category_data|
cup.team_categories.create!(team_category_data)
end
product_junior = cup.products.find_by(name_en: "Participation Junior (17 years old and younger)")
product_adult = cup.products.find_by(name_en: "Participation Adult (18 years old and older)")
cup.update!(product_junior: product_junior, product_adult: product_adult)
product_individual_junior = cup.products.find_by(name_en: "Participation Junior (17 years old and younger)")
product_individual_adult = cup.products.find_by(name_en: "Participation Adult (18 years old and older)")
cup.update!(product_individual_junior: product_individual_junior,
product_individual_adult: product_individual_adult)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/tasks/temporary/cups/add_2024.rake
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ namespace :temporary do
team_categories_data.each do |team_category_data|
cup.team_categories.create!(team_category_data)
end
product_junior = cup.products.find_by(name_en: "Participation Junior (17 years old and younger)")
product_adult = cup.products.find_by(name_en: "Participation Adult (18 years old and older)")
cup.update!(product_junior: product_junior, product_adult: product_adult)
product_individual_junior = cup.products.find_by(name_en: "Participation Junior (17 years old and younger)")
product_individual_adult = cup.products.find_by(name_en: "Participation Adult (18 years old and older)")
cup.update!(product_individual_junior: product_individual_junior,
product_individual_adult: product_individual_adult)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/cup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
let(:cup) { build(:cup) }

it do
expect(cup).to belong_to(:product_junior).class_name("Product").optional
expect(cup).to belong_to(:product_adult).class_name("Product").optional
expect(cup).to belong_to(:product_individual_junior).class_name("Product").optional
expect(cup).to belong_to(:product_individual_adult).class_name("Product").optional
expect(cup).to have_many(:individual_categories).dependent(:destroy)
expect(cup).to have_many(:team_categories).dependent(:destroy)
expect(cup).to have_many(:events).dependent :destroy
Expand Down
2 changes: 1 addition & 1 deletion spec/models/kenshi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
let(:junior_product) { create(:product, cup: kenshi.cup, fee_chf: 16, fee_eu: 15) }

before do
cup.update(product_junior: junior_product, product_adult: adult_product)
cup.update(product_individual_junior: junior_product, product_individual_adult: adult_product)
end

context "when creating a team and an individual participations" do
Expand Down
27 changes: 16 additions & 11 deletions spec/models/participation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
let(:participation) { build(:participation) }

it do
expect(participation).to delegate_method(:product_junior).to(:cup)
expect(participation).to delegate_method(:product_adult).to(:cup)
expect(participation).to delegate_method(:product_individual_junior).to(:cup)
expect(participation).to delegate_method(:product_individual_adult).to(:cup)
expect(participation).to delegate_method(:full_name).to(:kenshi).allow_nil
expect(participation).to delegate_method(:grade).to(:kenshi).allow_nil
expect(participation).to delegate_method(:club).to(:kenshi).allow_nil
Expand Down Expand Up @@ -79,7 +79,9 @@

describe "after_commit" do
describe "#update_purchase" do
let!(:cup) { create(:cup, product_junior: create(:product), product_adult: create(:product)) }
let!(:cup) {
create(:cup, product_individual_junior: create(:product), product_individual_adult: create(:product))
}
let!(:team_category) { build(:team_category, cup: cup) }
let!(:individual_category) { create(:individual_category, cup: cup) }
let!(:kenshi) { create(:kenshi, cup: cup) }
Expand Down Expand Up @@ -153,34 +155,37 @@
end

describe "#product" do
let!(:product_junior) { build(:product) }
let!(:product_adult) { build(:product) }
let!(:cup) { create(:cup, product_junior: product_junior, product_adult: product_adult) }
let!(:product_individual_junior) { build(:product) }
let!(:product_individual_adult) { build(:product) }
let!(:cup) {
create(:cup, product_individual_junior: product_individual_junior,
product_individual_adult: product_individual_adult)
}
let!(:kenshi) { create(:kenshi, cup: cup) }
let!(:participation) { create(:participation, kenshi: kenshi) }

context "when kenshi is junior" do
before { allow(kenshi).to receive(:junior?).and_return(true) }

it { expect(participation.product).to eq(product_junior) }
it { expect(participation.product).to eq(product_individual_junior) }
end

context "when kenshi is adult" do
before { allow(kenshi).to receive(:junior?).and_return(false) }

it { expect(participation.product).to eq(product_adult) }
it { expect(participation.product).to eq(product_individual_adult) }
end
end

describe "#purchase" do
let(:product_adult) { create(:product) }
let(:cup) { create(:cup, product_adult: product_adult) }
let(:product_individual_adult) { create(:product) }
let(:cup) { create(:cup, product_individual_adult: product_individual_adult) }
let(:kenshi) { create(:kenshi, cup: cup) }
let(:participation) { create(:participation, kenshi: kenshi) }

it do
expect(participation.purchase).to be_a(Purchase)
expect(participation.purchase.product).to eq(product_adult)
expect(participation.purchase.product).to eq(product_individual_adult)
end
end
end

0 comments on commit e0f1c50

Please sign in to comment.