Skip to content

Commit 5fd79c1

Browse files
authored
Merge pull request #277 from plural/card-and-printing-resource-specs
Add specs for card and printing resources.
2 parents 4f57a12 + aa3e780 commit 5fd79c1

File tree

9 files changed

+488
-144
lines changed

9 files changed

+488
-144
lines changed

app/models/card.rb

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,72 @@
11
# frozen_string_literal: true
2+
23
class Card < ApplicationRecord
34
self.table_name = 'unified_cards'
45
include CardAbilities
56

67
self.primary_key = :id
78

9+
def latest_printing_id
10+
printing_ids[0]
11+
rescue StandardError
12+
nil
13+
end
14+
15+
def restrictions
16+
{
17+
banned: restrictions_banned,
18+
global_penalty: restrictions_global_penalty,
19+
points: packed_restriction_to_map(restrictions_points),
20+
restricted: restrictions_restricted,
21+
universal_faction_cost: packed_restriction_to_map(restrictions_universal_faction_cost)
22+
}
23+
end
24+
825
belongs_to :card_type
926
belongs_to :faction
1027
belongs_to :side
1128

12-
scope :by_card_cycle, ->(card_cycle_id) {
13-
where(id: Printing.select(:card_id).where(card_cycle_id: card_cycle_id))
29+
scope :by_card_cycle, lambda { |card_cycle_id|
30+
where(id: Printing.select(:card_id).where(card_cycle_id:))
1431
}
15-
scope :by_card_set, ->(card_set_id) {
16-
where(id: Printing.select(:card_id).where(card_set_id: card_set_id))
32+
scope :by_card_set, lambda { |card_set_id|
33+
where(id: Printing.select(:card_id).where(card_set_id:))
1734
}
1835

1936
has_many :card_card_subtypes,
20-
primary_key: :id,
21-
foreign_key: :card_id
37+
primary_key: :id
2238

2339
has_many :card_subtypes, through: :card_card_subtypes
2440

2541
has_many :raw_printings,
2642
class_name: 'RawPrinting',
27-
primary_key: :id,
28-
foreign_key: :card_id
43+
primary_key: :id
2944

3045
has_many :printings,
31-
primary_key: :id,
32-
foreign_key: :card_id
46+
primary_key: :id
3347

3448
has_many :card_cycles, through: :printings
49+
has_many :card_sets, through: :printings
3550

3651
has_many :rulings,
37-
primary_key: :id,
38-
foreign_key: :card_id
52+
primary_key: :id
3953

4054
# Private decks
41-
has_many :deck_cards, primary_key: :id, foreign_key: :card_id
42-
has_many :decks, through: :deck_cards, primary_key: :id, foreign_key: :card_id
55+
has_many :deck_cards, primary_key: :id
56+
has_many :decks, through: :deck_cards, primary_key: :id
4357

4458
# Public decklists
45-
has_many :decklist_cards, primary_key: :id, foreign_key: :card_id
46-
has_many :decklists, through: :decklist_cards, primary_key: :id, foreign_key: :card_id
59+
has_many :decklist_cards, primary_key: :id
60+
has_many :decklists, through: :decklist_cards, primary_key: :id
61+
62+
private
63+
64+
def packed_restriction_to_map(packed)
65+
m = {}
66+
packed.each do |p|
67+
x = p.split('=')
68+
m[x[0]] = x[1].to_i
69+
end
70+
m
71+
end
4772
end

app/models/concerns/card_abilities.rb

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# Module included by Card and Printing to expose common card ability attributes.
14
module CardAbilities
2-
extend ActiveSupport::Concern
5+
extend ActiveSupport::Concern
36

4-
included do
5-
def card_abilities
6-
{
7-
additional_cost: self.additional_cost,
8-
advanceable: self.advanceable,
9-
gains_subroutines: self.gains_subroutines,
10-
interrupt: self.interrupt,
11-
link_provided: self.link_provided,
12-
mu_provided: self.mu_provided,
13-
num_printed_subroutines: self.num_printed_subroutines,
14-
on_encounter_effect: self.on_encounter_effect,
15-
performs_trace: self.performs_trace,
16-
recurring_credits_provided: self.recurring_credits_provided,
17-
rez_effect: self.rez_effect,
18-
trash_ability: self.trash_ability,
19-
}
20-
end
7+
included do
8+
def card_abilities
9+
{
10+
additional_cost:,
11+
advanceable:,
12+
gains_subroutines:,
13+
interrupt:,
14+
link_provided:,
15+
mu_provided:,
16+
num_printed_subroutines:,
17+
on_encounter_effect:,
18+
performs_trace:,
19+
recurring_credits_provided:,
20+
rez_effect:,
21+
trash_ability:
22+
}
2123
end
22-
end
24+
end
25+
end

app/models/printing.rb

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ class Printing < ApplicationRecord
55

66
self.primary_key = :id
77

8-
belongs_to :side
98
belongs_to :card
109
belongs_to :card_set
1110

12-
has_one :faction, through: :card
13-
has_one :card_cycle, through: :card_set
14-
has_one :card_type, through: :card
11+
belongs_to :faction
12+
belongs_to :card_cycle
13+
belongs_to :card_type
1514

1615
has_many :printing_card_subtypes,
1716
primary_key: :id
1817
has_many :card_subtypes, through: :printing_card_subtypes
1918

20-
has_one :side, through: :card
19+
belongs_to :side
2120
has_many :illustrator_printings,
2221
primary_key: :id
2322
has_many :illustrators, through: :illustrator_printings
@@ -29,4 +28,45 @@ class Printing < ApplicationRecord
2928
primary_key: :card_id,
3029
foreign_key: :card_id
3130
has_many :card_pools, through: :card_pool_cards
31+
32+
def images
33+
{ 'nrdb_classic' => nrdb_classic_images }
34+
end
35+
36+
def latest_printing_id
37+
printing_ids[0]
38+
rescue StandardError
39+
nil
40+
end
41+
42+
def restrictions
43+
{
44+
banned: restrictions_banned,
45+
global_penalty: restrictions_global_penalty,
46+
points: packed_restriction_to_map(restrictions_points),
47+
restricted: restrictions_restricted,
48+
universal_faction_cost: packed_restriction_to_map(restrictions_universal_faction_cost)
49+
}
50+
end
51+
52+
private
53+
54+
def nrdb_classic_images
55+
url_prefix = Rails.configuration.x.printing_images.nrdb_classic_prefix
56+
{
57+
'tiny' => format('%s/tiny/%s.jpg', url_prefix, id),
58+
'small' => format('%s/small/%s.jpg', url_prefix, id),
59+
'medium' => format('%s/medium/%s.jpg', url_prefix, id),
60+
'large' => format('%s/large/%s.jpg', url_prefix, id)
61+
}
62+
end
63+
64+
def packed_restriction_to_map(packed)
65+
m = {}
66+
packed.each do |p|
67+
x = p.split('=')
68+
m[x[0]] = x[1].to_i
69+
end
70+
m
71+
end
3272
end

app/resources/card_resource.rb

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
class CardResource < ApplicationResource
55
primary_endpoint '/cards', %i[index show]
66

7+
self.default_page_size = 1000
8+
79
attribute :id, :string
810
attribute :stripped_title, :string
911
attribute :title, :string
1012
attribute :card_type_id, :string
1113
attribute :side_id, :string
1214
attribute :faction_id, :string
15+
# TODO(plural): Move cost and agenda requirements into model.
16+
attribute :cost, :string do
17+
@object.cost == -1 ? 'X' : @object.cost
18+
end
1319
attribute :advancement_requirement, :string do
1420
@object.advancement_requirement == -1 ? 'X' : @object.advancement_requirement
1521
end
1622
attribute :agenda_points, :integer
1723
attribute :base_link, :integer
18-
attribute :cost, :string do
19-
@object.cost == -1 ? 'X' : @object.cost
20-
end
2124
attribute :deck_limit, :integer
2225
attribute :in_restriction, :boolean
2326
attribute :influence_cost, :integer
@@ -43,34 +46,15 @@ class CardResource < ApplicationResource
4346
attribute :card_cycle_ids, :array_of_strings
4447
attribute :card_set_ids, :array_of_strings
4548
attribute :designed_by, :string
46-
attribute :printings_released_by, :string
49+
attribute :printings_released_by, :array_of_strings
4750
attribute :pronouns, :string
4851
attribute :pronunciation_approximation, :string
4952
attribute :pronunciation_ipa, :string
5053

5154
# Synthesized attributes
5255
attribute :card_abilities, :hash
53-
def packed_restriction_to_map(packed)
54-
m = {}
55-
packed.each do |p|
56-
x = p.split('=')
57-
m[x[0]] = x[1].to_i
58-
end
59-
m
60-
end
61-
62-
attribute :restrictions, :hash do
63-
{
64-
banned: @object.restrictions_banned,
65-
global_penalty: @object.restrictions_global_penalty,
66-
points: packed_restriction_to_map(@object.restrictions_points),
67-
restricted: @object.restrictions_restricted,
68-
universal_faction_cost: packed_restriction_to_map(@object.restrictions_universal_faction_cost)
69-
}
70-
end
71-
attribute :latest_printing_id, :string do
72-
@object.printing_ids[0]
73-
end
56+
attribute :restrictions, :hash
57+
attribute :latest_printing_id, :string
7458

7559
filter :card_cycle_id, :string do
7660
eq do |scope, value|
@@ -98,35 +82,35 @@ def packed_restriction_to_map(packed)
9882
end
9983
end
10084

101-
has_many :card_cycles do
85+
many_to_many :card_cycles do
10286
link do |c|
103-
'%s?filter[id]=%s' % [Rails.application.routes.url_helpers.card_cycles_url, c.card_cycle_ids.join(',')]
87+
format('%s?filter[id]=%s', Rails.application.routes.url_helpers.card_cycles_url, c.card_cycle_ids.join(','))
10488
end
10589
end
106-
has_many :card_sets do
90+
many_to_many :card_sets do
10791
link do |c|
108-
'%s?filter[id]=%s' % [Rails.application.routes.url_helpers.card_sets_url, c.card_set_ids.join(',')]
92+
format('%s?filter[id]=%s', Rails.application.routes.url_helpers.card_sets_url, c.card_set_ids.join(','))
10993
end
11094
end
11195
many_to_many :card_subtypes do
11296
link do |c|
11397
card_subtype_ids = c.card_subtype_ids.empty? ? 'none' : c.card_subtype_ids.join(',')
114-
'%s?filter[id]=%s' % [Rails.application.routes.url_helpers.card_subtypes_url, card_subtype_ids]
98+
format('%s?filter[id]=%s', Rails.application.routes.url_helpers.card_subtypes_url, card_subtype_ids)
11599
end
116100
end
117101
belongs_to :card_type
118102
belongs_to :faction
119103
has_many :printings do
120104
link do |c|
121-
'%s?filter[card_id]=%s' % [Rails.application.routes.url_helpers.printings_url, c.id]
105+
format('%s?filter[card_id]=%s', Rails.application.routes.url_helpers.printings_url, c.id)
122106
end
123107
end
124108
has_many :rulings
125109
belongs_to :side
126110

127111
many_to_many :decklists do
128112
link do |c|
129-
'%s?filter[card_id]=%s' % [Rails.application.routes.url_helpers.decklists_url, c.id]
113+
format('%s?filter[card_id]=%s', Rails.application.routes.url_helpers.decklists_url, c.id)
130114
end
131115
end
132116
end

0 commit comments

Comments
 (0)