Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {card,printing} <-> card_pool relationships #287

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": "latest"
},
"ghcr.io/devcontainers/features/ruby:1": {
"version": "3.1" // Change to match Ruby version from Gemfile
"version": "3.1" // Change to match Ruby version from Gemfile
},
"ghcr.io/devcontainers/features/node:1": {
"version": 19 // Change Node version as required
Expand Down Expand Up @@ -66,7 +66,7 @@
],
"ruby.format": "rubocop",
"rubocop.yjitEnabled": false,
"editor.formatOnSave": false
"editor.formatOnSave": false
},
"extensions": [
"IBM.output-colorizer",
Expand Down
3 changes: 3 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def restrictions
has_many :card_card_subtypes,
primary_key: :id

has_many :card_pool_cards
has_many :card_pools, through: :card_pool_cards

has_many :card_subtypes, through: :card_card_subtypes

has_many :raw_printings,
Expand Down
5 changes: 5 additions & 0 deletions app/models/card_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class CardPool < ApplicationRecord
has_many :card_pool_cards
has_many :raw_cards, through: :card_pool_cards
has_many :cards, through: :card_pool_cards, primary_key: :card_id, foreign_key: :id
has_many :printings, through: :card_pool_cards, primary_key: :card_id, foreign_key: :card_id
has_many :snapshots

belongs_to :format
Expand All @@ -17,4 +18,8 @@ def num_cards
end

validates :name, uniqueness: true

scope :by_printing_ids, lambda { |printing_ids|
joins(:card_pool_cards).joins(:printings).where(unified_printings: { id: printing_ids }).distinct
}
end
15 changes: 7 additions & 8 deletions app/models/card_pool_card.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true

class CardPoolCard < ApplicationRecord
self.table_name = "card_pools_cards"
self.table_name = 'card_pools_cards'

belongs_to :card,
:primary_key => :id,
:foreign_key => :card_id
primary_key: :id
belongs_to :card_pool,
:primary_key => :id,
:foreign_key => :card_pool_id
primary_key: :id
belongs_to :unified_card,
:primary_key => :id,
:foreign_key => :card_id
end
primary_key: :id,
foreign_key: :card_id
has_many :printings, through: :card
end
10 changes: 8 additions & 2 deletions app/resources/card_pool_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
has_many :snapshots

# TODO(plural): Add working relationships for cards and printings.
# has_many :cards
# has_many :printings
many_to_many :cards
many_to_many :printings

filter :printing_id, :string do
eq do |scope, value|
scope.by_printing_ids(value)

Check warning on line 25 in app/resources/card_pool_resource.rb

View check run for this annotation

Codecov / codecov/patch

app/resources/card_pool_resource.rb#L25

Added line #L25 was not covered by tests
end
end
end
2 changes: 2 additions & 0 deletions app/resources/card_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ class CardResource < ApplicationResource
format('%s?filter[card_id]=%s', Rails.application.routes.url_helpers.decklists_url, c.id)
end
end

many_to_many :card_pools
end
9 changes: 9 additions & 0 deletions app/resources/printing_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@ class PrintingResource < ApplicationResource
format('%s?filter[id]=%s', Rails.application.routes.url_helpers.illustrators_url, p.illustrator_ids.join(','))
end
end

many_to_many :card_pools do
assign do |printings, _card_pools|
CardPool.by_printing_ids(printings.map(&:id))
end
link do |p|
format('%s?filter[printing_id]=%s', Rails.application.routes.url_helpers.card_pools_url, p.id)
end
end
end
9 changes: 9 additions & 0 deletions spec/resources/card/reads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,14 @@ def check_included_for_id(card_id, include_value, resource_type, id)
check_included_for_id(card.id, 'side', 'sides', side.id)
end
end

describe 'include card_pools' do
let!(:card) { Card.find('border_control') }
let!(:card_pool) { CardPool.find('eternal_01') }

it 'works' do
check_included_for_id(card.id, 'card_pools', 'card_pools', card_pool.id)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/resources/card_pool/reads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,23 @@ def check_included_for_id(card_pool_id, include_value, resource_type, id)
check_included_for_id(card_pool.id, 'snapshots', 'snapshots', snapshot.id)
end
end

describe 'include cards' do
let!(:card_pool) { CardPool.find('eternal_01') }
let!(:card) { Card.find('border_control') }

it 'works' do
check_included_for_id(card_pool.id, 'cards', 'cards', card.id)
end
end

describe 'include cards' do
let!(:card_pool) { CardPool.find('eternal_01') }
let!(:printing) { Printing.find(Card.find('border_control').latest_printing_id) }

it 'works' do
check_included_for_id(card_pool.id, 'printings', 'printings', printing.id)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/resources/printing/reads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,14 @@ def check_included_for_id(printing_id, include_value, resource_type, id)
check_included_for_id(printing.id, 'side', 'sides', side.id)
end
end

describe 'include card_pools' do
let!(:printing) { Printing.find(Card.find('border_control').latest_printing_id) }
let!(:card_pool) { CardPool.find('eternal_01') }

it 'works' do
check_included_for_id(printing.id, 'card_pools', 'card_pools', card_pool.id)
end
end
end
end