Skip to content

Commit

Permalink
Merge pull request #319 from plural/remove-extra-queries-for-cards-an…
Browse files Browse the repository at this point in the history
…d-printings

Use DB attributes to avoid unneccessary queries from relations.
  • Loading branch information
plural authored Oct 26, 2024
2 parents 200aefd + a8a88a4 commit 8b12d4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Card < ApplicationRecord
self.primary_key = :id

def latest_printing_id
printing_ids[0]
printing_ids_in_database[0]
rescue StandardError
nil
end
Expand Down
14 changes: 9 additions & 5 deletions app/resources/card_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ class CardResource < ApplicationResource # rubocop:disable Metrics/ClassLength
attribute :text, :string
attribute :trash_cost, :integer
attribute :is_unique, :boolean
attribute :card_subtype_ids, :array_of_strings
attribute :card_subtype_ids, :array_of_strings do
@object.card_subtype_ids_in_database
end
attribute :display_subtypes, :string
attribute :attribution, :string
attribute :updated_at, :datetime
attribute :format_ids, :array_of_strings
attribute :card_pool_ids, :array_of_strings
attribute :card_pool_ids, :array_of_strings do
@object.card_pool_ids_in_database
end
attribute :snapshot_ids, :array_of_strings
attribute :card_cycle_ids, :array_of_strings do
@object.card_cycle_ids_in_database
Expand Down Expand Up @@ -94,18 +98,18 @@ class CardResource < ApplicationResource # rubocop:disable Metrics/ClassLength
many_to_many :card_cycles do
link do |c|
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.card_cycles_url,
ids: c.card_cycle_ids.join(','))
ids: c.card_cycle_ids_in_database.join(','))
end
end
many_to_many :card_sets do
link do |c|
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.card_sets_url,
ids: c.card_set_ids.join(','))
ids: c.card_set_ids_in_database.join(','))
end
end
many_to_many :card_subtypes do
link do |c|
card_subtype_ids = c.card_subtype_ids.empty? ? 'none' : c.card_subtype_ids.join(',')
card_subtype_ids = c.card_subtype_ids_in_database.empty? ? 'none' : c.card_subtype_ids_in_database.join(',')
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.card_subtypes_url,
ids: card_subtype_ids)
end
Expand Down
16 changes: 11 additions & 5 deletions app/resources/printing_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class PrintingResource < ApplicationResource # rubocop:disable Metrics/ClassLeng

attribute :flavor, :string
attribute :display_illustrators, :string
attribute :illustrator_ids, :array_of_strings
attribute :illustrator_ids, :array_of_strings do
@object.illustrator_ids_in_database
end
attribute :illustrator_names, :array_of_strings

attribute :position, :integer
Expand Down Expand Up @@ -53,12 +55,16 @@ class PrintingResource < ApplicationResource # rubocop:disable Metrics/ClassLeng
attribute :text, :string
attribute :trash_cost, :integer
attribute :is_unique, :boolean
attribute :card_subtype_ids, :array_of_strings
attribute :card_subtype_ids, :array_of_strings do
@object.card_subtype_ids_in_database
end
attribute :card_subtype_names, :array_of_strings
attribute :display_subtypes, :string
attribute :attribution, :string
attribute :format_ids, :array_of_strings
attribute :card_pool_ids, :array_of_strings
attribute :card_pool_ids, :array_of_strings do
@object.card_pool_ids_in_database
end
attribute :snapshot_ids, :array_of_strings
attribute :card_cycle_ids, :array_of_strings do
@object.card_cycle_ids_in_database
Expand Down Expand Up @@ -118,14 +124,14 @@ class PrintingResource < ApplicationResource # rubocop:disable Metrics/ClassLeng
many_to_many :card_subtypes do
link do |p|
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.card_subtypes_url,
ids: p.card_subtype_ids.join(','))
ids: p.card_subtype_ids_in_database.join(','))
end
end

many_to_many :illustrators do
link do |p|
format('%<url>s?filter[id]=%<ids>s', url: Rails.application.routes.url_helpers.illustrators_url,
ids: p.illustrator_ids.join(','))
ids: p.illustrator_ids_in_database.join(','))
end
end

Expand Down

0 comments on commit 8b12d4f

Please sign in to comment.