Skip to content

Commit ab302a1

Browse files
committed
Update private deck to have ID as a card slot.
1 parent dc1a958 commit ab302a1

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

app/controllers/decks_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def show
1717
end
1818

1919
def create
20+
# TODO(plural): Wrap all of this in a transaction.
21+
2022
# Use the incoming parameters, but build the actual object against the model directly.
2123

2224
new_deck = Deck.new
@@ -33,8 +35,9 @@ def create
3335
# TODO(plural): Flesh out nice error messages.
3436
raise ApplicationController::BadDeckError, 'There was an error creating your deck.' unless new_deck.save
3537

38+
new_deck.deck_cards.create(card_id: new_deck.identity_card_id, quantity: 1)
3639
card_slots.each do |card_id, quantity|
37-
new_deck.card_slots.create(card_id:, quantity:)
40+
new_deck.deck_cards.create(card_id:, quantity:)
3841
end
3942

4043
simplified_params = { data: { id: new_deck.id, type: 'decks' } }

app/models/deck.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ class Deck < ApplicationRecord
2121

2222
has_many :deck_cards
2323
has_many :cards, through: :deck_cards
24+
25+
def card_slots
26+
deck_cards.order(:card_id).each_with_object({}) { |c, h| h[c.card_id] = c.quantity }
27+
end
28+
29+
def num_cards
30+
deck_cards
31+
# Exclude identity
32+
.reject { |c| c.card_id == identity_card_id }
33+
.map(&:quantity).sum
34+
end
35+
2436
end

app/models/deck_card.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class DeckCard < ApplicationRecord
66

77
belongs_to :deck,
88
primary_key: :id,
9-
inverse_of: :card_slots,
9+
inverse_of: :deck_cards,
1010
touch: true
1111
belongs_to :card,
1212
primary_key: :id

app/resources/deck_resource.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@ class DeckResource < PrivateApplicationResource
2626
end
2727
end
2828

29-
attribute :card_slots, :hash do
30-
cards = {}
31-
@object.deck_cards.order(:card_id).each do |c|
32-
cards[c.card_id] = c.quantity
33-
end
34-
cards
35-
end
36-
37-
attribute :num_cards, :integer do
38-
@object.deck_cards.map(&:quantity).sum
39-
end
29+
attribute :card_slots, :hash
30+
attribute :num_cards, :integer
4031

4132
# This is the basic definition, but does not take restriction modifications
4233
# into account. Leaving this here as an example for now, but it will need to
@@ -66,13 +57,5 @@ class DeckResource < PrivateApplicationResource
6657
end
6758
end
6859

69-
# The rubocop warning is disabled because this relationship won't work
70-
# without it because there is no identity_card table.
71-
belongs_to :identity_card, resource: CardResource, foreign_key: :identity_card_id do # rubocop:disable Rails/RedundantForeignKey
72-
link do |deck|
73-
format('%<url>s/%<id>s', url: Rails.application.routes.url_helpers.cards_url, id: deck.identity_card_id)
74-
end
75-
end
76-
7760
many_to_many :cards
7861
end

0 commit comments

Comments
 (0)