File tree Expand file tree Collapse file tree 4 files changed +19
-21
lines changed Expand file tree Collapse file tree 4 files changed +19
-21
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ def show
17
17
end
18
18
19
19
def create
20
+ # TODO(plural): Wrap all of this in a transaction.
21
+
20
22
# Use the incoming parameters, but build the actual object against the model directly.
21
23
22
24
new_deck = Deck . new
@@ -33,8 +35,9 @@ def create
33
35
# TODO(plural): Flesh out nice error messages.
34
36
raise ApplicationController ::BadDeckError , 'There was an error creating your deck.' unless new_deck . save
35
37
38
+ new_deck . deck_cards . create ( card_id : new_deck . identity_card_id , quantity : 1 )
36
39
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 :)
38
41
end
39
42
40
43
simplified_params = { data : { id : new_deck . id , type : 'decks' } }
Original file line number Diff line number Diff line change @@ -21,4 +21,16 @@ class Deck < ApplicationRecord
21
21
22
22
has_many :deck_cards
23
23
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
+
24
36
end
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ class DeckCard < ApplicationRecord
6
6
7
7
belongs_to :deck ,
8
8
primary_key : :id ,
9
- inverse_of : :card_slots ,
9
+ inverse_of : :deck_cards ,
10
10
touch : true
11
11
belongs_to :card ,
12
12
primary_key : :id
Original file line number Diff line number Diff line change @@ -26,17 +26,8 @@ class DeckResource < PrivateApplicationResource
26
26
end
27
27
end
28
28
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
40
31
41
32
# This is the basic definition, but does not take restriction modifications
42
33
# into account. Leaving this here as an example for now, but it will need to
@@ -66,13 +57,5 @@ class DeckResource < PrivateApplicationResource
66
57
end
67
58
end
68
59
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
-
77
60
many_to_many :cards
78
61
end
You can’t perform that action at this time.
0 commit comments