From d833c9cc797a124e8420f49a749e77c80e31666a Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sun, 24 Nov 2024 05:43:16 +0000 Subject: [PATCH] Optimize a few things for snapshots. --- app/controllers/snapshots_controller.rb | 5 +++-- app/models/snapshot.rb | 2 +- app/resources/snapshot_resource.rb | 8 +++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/snapshots_controller.rb b/app/controllers/snapshots_controller.rb index c4e4885b..4bca7aa9 100644 --- a/app/controllers/snapshots_controller.rb +++ b/app/controllers/snapshots_controller.rb @@ -3,14 +3,15 @@ # Controller for the Snapshot resource. class SnapshotsController < ApplicationController def index - base_scope = Snapshot.includes(:card_pool, :cards, :restriction) + base_scope = Snapshot.includes(:card_pool) snapshots = SnapshotResource.all(params, base_scope) respond_with(snapshots) end def show - snapshot = SnapshotResource.find(params) + base_scope = Snapshot.includes(:card_pool) + snapshot = SnapshotResource.find(params, base_scope) respond_with(snapshot) end end diff --git a/app/models/snapshot.rb b/app/models/snapshot.rb index 79525e31..61f4ffcf 100644 --- a/app/models/snapshot.rb +++ b/app/models/snapshot.rb @@ -11,6 +11,6 @@ class Snapshot < ApplicationRecord # TODO(plural): Convert date_start to a real date field. def num_cards - cards.length + cards.count end end diff --git a/app/resources/snapshot_resource.rb b/app/resources/snapshot_resource.rb index a581558f..500e123a 100644 --- a/app/resources/snapshot_resource.rb +++ b/app/resources/snapshot_resource.rb @@ -4,19 +4,21 @@ class SnapshotResource < ApplicationResource primary_endpoint '/snapshots', %i[index show] + self.default_page_size = 1000 + attribute :id, :string attribute :format_id, :string attribute :active, :boolean attribute :card_cycle_ids, :array_of_strings do - @object.card_pool.card_pool_card_cycles.pluck(:card_cycle_id) + @object.card_cycle_ids end attribute :card_set_ids, :array_of_strings do - @object.card_pool.card_pool_card_sets.pluck(:card_set_id) + @object.card_set_ids end attribute :card_pool_id, :string attribute :restriction_id, :string attribute :num_cards, :integer do - @object.card_pool.cards.length + @object.card_pool.cards.count end attribute :date_start, :date attribute :updated_at, :datetime