Skip to content

Commit ced9841

Browse files
authored
Merge pull request #299 from plural/id-takes-a-card-slot
Id takes a card slot
2 parents 996ffb3 + 066b41f commit ced9841

27 files changed

+48
-62
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/models/decklist.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def card_slots
2727
end
2828

2929
def num_cards
30-
decklist_cards.map(&:quantity).sum
30+
decklist_cards
31+
# Exclude identity
32+
.reject { |c| c.card_id == identity_card_id }
33+
.map(&:quantity).sum
3134
end
3235

3336
# TODO(plural): Extract this out to share between public and private decklists.
@@ -37,6 +40,8 @@ def num_cards
3740
def influence_spent
3841
qty = decklist_cards.each_with_object({}) { |c, h| h[c.card_id] = c.quantity }
3942
cards
43+
# Exclude identity
44+
.reject { |c| c.id == identity_card.id }
4045
.filter { |c| c.faction_id != identity_card.faction_id }
4146
.map { |c| c.influence_cost.nil? ? 0 : (c.influence_cost * qty[c.id]) }
4247
.sum

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

app/resources/decklist_resource.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,5 @@ class DecklistResource < ApplicationResource
5050
end
5151
end
5252

53-
# The rubocop warning is disabled because this relationship won't work
54-
# without it because there is no identity_card table.
55-
belongs_to :identity_card, resource: CardResource, foreign_key: :identity_card_id do # rubocop:disable Rails/RedundantForeignKey
56-
link do |decklist|
57-
format('%<url>s/%<id>s', url: Rails.application.routes.url_helpers.cards_url, id: decklist.identity_card_id)
58-
end
59-
end
60-
6153
many_to_many :cards
6254
end

doc/api/card_types/relationship_-_get_cards_for_a_card_type.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

doc/api/cards/all_cards.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

doc/api/cards/filter_-_card_search_operator.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"request_content_type": "application/json",
3434
"response_status": 200,
3535
"response_status_text": "OK",
36-
"response_body": "{\n \"data\": [\n {\n \"id\": \"sure_gamble\",\n \"type\": \"cards\",\n \"attributes\": {\n \"stripped_title\": \"Sure Gamble\",\n \"title\": \"Sure Gamble\",\n \"card_type_id\": \"event\",\n \"side_id\": \"runner\",\n \"faction_id\": \"neutral_runner\",\n \"cost\": \"5\",\n \"advancement_requirement\": null,\n \"agenda_points\": null,\n \"base_link\": null,\n \"deck_limit\": 3,\n \"in_restriction\": false,\n \"influence_cost\": 0,\n \"influence_limit\": null,\n \"memory_cost\": null,\n \"minimum_deck_size\": null,\n \"num_printings\": 2,\n \"printing_ids\": [\n \"01050\",\n \"01064\"\n ],\n \"date_release\": \"2012-09-06\",\n \"restriction_ids\": [\n\n ],\n \"strength\": null,\n \"stripped_text\": \"Gain 9 credits.\",\n \"text\": \"Gain 9[credit].\",\n \"trash_cost\": null,\n \"is_unique\": false,\n \"card_subtype_ids\": [\n\n ],\n \"display_subtypes\": null,\n \"attribution\": null,\n \"updated_at\": \"2024-07-21T21:08:35+00:00\",\n \"format_ids\": [\n \"eternal\",\n \"standard\",\n \"startup\"\n ],\n \"card_pool_ids\": [\n \"standard_01\",\n \"standard_02\",\n \"startup_01\",\n \"startup_02\",\n \"eternal_01\"\n ],\n \"snapshot_ids\": [\n \"eternal_01\",\n \"standard_01\",\n \"standard_02\",\n \"standard_03\",\n \"standard_04\",\n \"standard_05\",\n \"startup_01\",\n \"startup_02\"\n ],\n \"card_cycle_ids\": [\n \"core\",\n \"core\"\n ],\n \"card_set_ids\": [\n \"core\",\n \"core\"\n ],\n \"designed_by\": null,\n \"printings_released_by\": [\n \"\"\n ],\n \"pronouns\": null,\n \"pronunciation_approximation\": null,\n \"pronunciation_ipa\": null,\n \"card_abilities\": {\n \"additional_cost\": false,\n \"advanceable\": false,\n \"gains_subroutines\": false,\n \"interrupt\": false,\n \"link_provided\": null,\n \"mu_provided\": null,\n \"num_printed_subroutines\": null,\n \"on_encounter_effect\": false,\n \"performs_trace\": false,\n \"recurring_credits_provided\": null,\n \"rez_effect\": false,\n \"trash_ability\": false\n },\n \"restrictions\": {\n \"banned\": [\n\n ],\n \"global_penalty\": [\n\n ],\n \"points\": {\n },\n \"restricted\": [\n\n ],\n \"universal_faction_cost\": {\n }\n },\n \"latest_printing_id\": \"01050\"\n },\n \"relationships\": {\n \"card_cycles\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_cycles?filter[id]=core,core\"\n }\n },\n \"card_sets\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_sets?filter[id]=core,core\"\n }\n },\n \"card_subtypes\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_subtypes?filter[id]=none\"\n }\n },\n \"card_type\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_types/event\"\n }\n },\n \"faction\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/factions/neutral_runner\"\n }\n },\n \"printings\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/printings?filter[card_id]=sure_gamble\"\n }\n },\n \"rulings\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/rulings?filter[card_id]=sure_gamble\"\n }\n },\n \"side\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/sides/runner\"\n }\n },\n \"decklists\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/decklists?filter[card_id]=sure_gamble\"\n }\n },\n \"card_pools\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_pools?filter[card_id]=sure_gamble\"\n }\n }\n },\n \"links\": {\n \"self\": \"http://localhost:3000/api/v3/public/cards/sure_gamble\"\n }\n }\n ],\n \"links\": {\n \"self\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\",\n \"first\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\",\n \"last\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\"\n },\n \"meta\": {\n }\n}",
36+
"response_body": "{\n \"data\": [\n {\n \"id\": \"sure_gamble\",\n \"type\": \"cards\",\n \"attributes\": {\n \"stripped_title\": \"Sure Gamble\",\n \"title\": \"Sure Gamble\",\n \"card_type_id\": \"event\",\n \"side_id\": \"runner\",\n \"faction_id\": \"neutral_runner\",\n \"cost\": \"5\",\n \"advancement_requirement\": null,\n \"agenda_points\": null,\n \"base_link\": null,\n \"deck_limit\": 3,\n \"in_restriction\": false,\n \"influence_cost\": 0,\n \"influence_limit\": null,\n \"memory_cost\": null,\n \"minimum_deck_size\": null,\n \"num_printings\": 2,\n \"printing_ids\": [\n \"01050\",\n \"01064\"\n ],\n \"date_release\": \"2012-09-06\",\n \"restriction_ids\": [\n\n ],\n \"strength\": null,\n \"stripped_text\": \"Gain 9 credits.\",\n \"text\": \"Gain 9[credit].\",\n \"trash_cost\": null,\n \"is_unique\": false,\n \"card_subtype_ids\": [\n\n ],\n \"display_subtypes\": null,\n \"attribution\": null,\n \"updated_at\": \"2024-07-28T20:37:49+00:00\",\n \"format_ids\": [\n \"eternal\",\n \"standard\",\n \"startup\"\n ],\n \"card_pool_ids\": [\n \"eternal_01\",\n \"standard_01\",\n \"standard_02\",\n \"startup_01\",\n \"startup_02\"\n ],\n \"snapshot_ids\": [\n \"eternal_01\",\n \"standard_01\",\n \"standard_02\",\n \"standard_03\",\n \"standard_04\",\n \"standard_05\",\n \"startup_01\",\n \"startup_02\"\n ],\n \"card_cycle_ids\": [\n \"core\",\n \"core\"\n ],\n \"card_set_ids\": [\n \"core\",\n \"core\"\n ],\n \"designed_by\": null,\n \"printings_released_by\": [\n \"\"\n ],\n \"pronouns\": null,\n \"pronunciation_approximation\": null,\n \"pronunciation_ipa\": null,\n \"card_abilities\": {\n \"additional_cost\": false,\n \"advanceable\": false,\n \"gains_subroutines\": false,\n \"interrupt\": false,\n \"link_provided\": null,\n \"mu_provided\": null,\n \"num_printed_subroutines\": null,\n \"on_encounter_effect\": false,\n \"performs_trace\": false,\n \"recurring_credits_provided\": null,\n \"rez_effect\": false,\n \"trash_ability\": false\n },\n \"restrictions\": {\n \"banned\": [\n\n ],\n \"global_penalty\": [\n\n ],\n \"points\": {\n },\n \"restricted\": [\n\n ],\n \"universal_faction_cost\": {\n }\n },\n \"latest_printing_id\": \"01050\"\n },\n \"relationships\": {\n \"card_cycles\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_cycles?filter[id]=core,core\"\n }\n },\n \"card_sets\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_sets?filter[id]=core,core\"\n }\n },\n \"card_subtypes\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_subtypes?filter[id]=none\"\n }\n },\n \"card_type\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_types/event\"\n }\n },\n \"faction\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/factions/neutral_runner\"\n }\n },\n \"printings\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/printings?filter[card_id]=sure_gamble\"\n }\n },\n \"rulings\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/rulings?filter[card_id]=sure_gamble\"\n }\n },\n \"side\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/sides/runner\"\n }\n },\n \"decklists\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/decklists?filter[card_id]=sure_gamble\"\n }\n },\n \"card_pools\": {\n \"links\": {\n \"related\": \"http://localhost:3000/api/v3/public/card_pools?filter[card_id]=sure_gamble\"\n }\n }\n },\n \"links\": {\n \"self\": \"http://localhost:3000/api/v3/public/cards/sure_gamble\"\n }\n }\n ],\n \"links\": {\n \"self\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\",\n \"first\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\",\n \"last\": \"http://localhost:3000/api/v3/public/cards?filter%5Bsearch%5D=gamble&page%5Bnumber%5D=1&page%5Bsize%5D=1000\"\n },\n \"meta\": {\n }\n}",
3737
"response_headers": {
3838
"Content-Type": "application/vnd.api+json; charset=utf-8"
3939
},

0 commit comments

Comments
 (0)