Skip to content

Commit 19b20e7

Browse files
authored
Merge pull request #331 from plural/optimize-snapshot-controller
Optimize a few things for snapshots.
2 parents e252696 + 15fd9c7 commit 19b20e7

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

app/controllers/snapshots_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
class SnapshotsController < ApplicationController
55
def index
66
add_total_stat(params)
7-
8-
base_scope = Snapshot.includes(:card_pool, :cards, :restriction)
7+
base_scope = Snapshot.includes(:card_pool)
98
snapshots = SnapshotResource.all(params, base_scope)
109

1110
respond_with(snapshots)
1211
end
1312

1413
def show
15-
snapshot = SnapshotResource.find(params)
14+
base_scope = Snapshot.includes(:card_pool)
15+
snapshot = SnapshotResource.find(params, base_scope)
1616
respond_with(snapshot)
1717
end
1818
end

app/models/snapshot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class Snapshot < ApplicationRecord
1111

1212
# TODO(plural): Convert date_start to a real date field.
1313
def num_cards
14-
cards.length
14+
cards.count
1515
end
1616
end

app/resources/snapshot_resource.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
class SnapshotResource < ApplicationResource
55
primary_endpoint '/snapshots', %i[index show]
66

7+
self.default_page_size = 1000
8+
79
attribute :id, :string
810
attribute :format_id, :string
911
attribute :active, :boolean
1012
attribute :card_cycle_ids, :array_of_strings do
11-
@object.card_pool.card_pool_card_cycles.pluck(:card_cycle_id)
13+
@object.card_cycle_ids
1214
end
1315
attribute :card_set_ids, :array_of_strings do
14-
@object.card_pool.card_pool_card_sets.pluck(:card_set_id)
16+
@object.card_set_ids
1517
end
1618
attribute :card_pool_id, :string
1719
attribute :restriction_id, :string
1820
attribute :num_cards, :integer do
19-
@object.card_pool.cards.length
21+
@object.card_pool.cards.count
2022
end
2123
attribute :date_start, :date
2224
attribute :updated_at, :datetime

0 commit comments

Comments
 (0)