@@ -45,32 +45,34 @@ namespace :import_decklists do
45
45
username : decklist [ 'user_name' ] ,
46
46
uuid : decklist [ 'uuid' ] )
47
47
48
- d = Decklist . find_or_initialize_by ( id : decklist [ 'uuid' ] )
49
- d . name = decklist [ 'name' ]
50
- d . user_id = decklist [ 'user_name' ]
51
-
52
- d . created_at = DateTime . parse ( decklist [ 'date_creation' ] )
53
- d . updated_at = DateTime . parse ( decklist [ 'date_update' ] )
54
- d . notes = decklist [ 'description' ]
55
-
56
- decklist [ 'cards' ] . each_key do |printing_id |
57
- card = cards_by_id [ printing_to_card [ printing_id ] ]
58
- if %w[ corp_identity runner_identity ] . include? ( card . card_type_id )
59
- d . identity_card_id = card . id
60
- d . side_id = card . side_id
48
+ ActiveRecord ::Base . transaction do
49
+ d = Decklist . find_or_initialize_by ( id : decklist [ 'uuid' ] )
50
+ d . name = decklist [ 'name' ]
51
+ d . user_id = decklist [ 'user_name' ]
52
+
53
+ d . created_at = DateTime . parse ( decklist [ 'date_creation' ] )
54
+ d . updated_at = DateTime . parse ( decklist [ 'date_update' ] )
55
+ d . notes = decklist [ 'description' ]
56
+
57
+ decklist [ 'cards' ] . each_key do |printing_id |
58
+ card = cards_by_id [ printing_to_card [ printing_id ] ]
59
+ if %w[ corp_identity runner_identity ] . include? ( card . card_type_id )
60
+ d . identity_card_id = card . id
61
+ d . side_id = card . side_id
62
+ end
61
63
end
62
- end
63
-
64
- d . save!
65
64
66
- # To allow overwriting, clear out the existing cards.
67
- d . decklist_cards . delete_all
68
- d . decklist_cards << d . decklist_cards . build ( card_id : d . identity_card_id , quantity : 1 )
69
- decklist [ 'cards' ] . each do |printing_id , quantity |
70
- card = cards_by_id [ printing_to_card [ printing_id ] ]
71
- # Do not write identity cards to the decklist_cards table.
72
- unless %w[ corp_identity runner_identity ] . include? ( card . card_type_id )
73
- d . decklist_cards << d . decklist_cards . build ( card_id : printing_to_card [ printing_id ] , quantity :)
65
+ d . save!
66
+
67
+ # To allow overwriting, clear out the existing cards.
68
+ d . decklist_cards . delete_all
69
+ d . decklist_cards << d . decklist_cards . build ( card_id : d . identity_card_id , quantity : 1 )
70
+ decklist [ 'cards' ] . each do |printing_id , quantity |
71
+ card = cards_by_id [ printing_to_card [ printing_id ] ]
72
+ # Do not write identity cards to the decklist_cards table.
73
+ unless %w[ corp_identity runner_identity ] . include? ( card . card_type_id )
74
+ d . decklist_cards << d . decklist_cards . build ( card_id : printing_to_card [ printing_id ] , quantity :)
75
+ end
74
76
end
75
77
end
76
78
end
0 commit comments