From 4bc96847f621c4cb3940fe99b3b4b67b20596189 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 3 Oct 2017 16:18:46 -0400 Subject: Removed box model It seemed kind of strange to have a model that there should always be exactly 14 of for each of the parent (Trainer) model instances, so the box names were moved into the Trainer model, and the Box model was removed. This commit also adds some eager loading to speed up page loading times. Also made a small change to the way the gift ribbons are extracted. refs #2 --- app/jobs/pokeviewer/extract_save_data_job.rb | 41 ++++++++++++-------- app/models/pokeviewer/box.rb | 29 -------------- app/models/pokeviewer/pokemon.rb | 33 +++++++++++++--- app/models/pokeviewer/trainer.rb | 58 +++++++++++++++++++++++++--- app/views/pokeviewer/pokemon/index.html.haml | 4 +- 5 files changed, 108 insertions(+), 57 deletions(-) delete mode 100644 app/models/pokeviewer/box.rb (limited to 'app') diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index 99d71f1..14ba9a6 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -3,7 +3,7 @@ module Pokeviewer queue_as :default def perform(args) - game = Trainer.find_or_create_by!( + game = Trainer.find_or_initialize_by( name: args["playerName"], number: args["playerId"]) do |r| case args["gameId"].to_i @@ -22,41 +22,50 @@ module Pokeviewer end end + game.box_1_name = args["boxes"].shift + game.box_2_name = args["boxes"].shift + game.box_3_name = args["boxes"].shift + game.box_4_name = args["boxes"].shift + game.box_5_name = args["boxes"].shift + game.box_6_name = args["boxes"].shift + game.box_7_name = args["boxes"].shift + game.box_8_name = args["boxes"].shift + game.box_9_name = args["boxes"].shift + game.box_10_name = args["boxes"].shift + game.box_11_name = args["boxes"].shift + game.box_12_name = args["boxes"].shift + game.box_13_name = args["boxes"].shift + game.box_14_name = args["boxes"].shift + if args.key? "marineRibbon" - game.marine_ribbon = GiftRibbon.find_by_id(args["marineRibbon"]) + game.marine_ribbon_id = args["marineRibbon"] end if args.key? "landRibbon" - game.land_ribbon = GiftRibbon.find_by_id(args["landRibbon"]) + game.land_ribbon_id = args["landRibbon"] end if args.key? "skyRibbon" - game.sky_ribbon = GiftRibbon.find_by_id(args["skyRibbon"]) + game.sky_ribbon_id = args["skyRibbon"] end if args.key? "countryRibbon" - game.country_ribbon = GiftRibbon.find_by_id(args["countryRibbon"]) + game.country_ribbon_id = args["countryRibbon"] end if args.key? "nationalRibbon" - game.national_ribbon = GiftRibbon.find_by_id(args["nationalRibbon"]) + game.national_ribbon_id = args["nationalRibbon"] end if args.key? "earthRibbon" - game.earth_ribbon = GiftRibbon.find_by_id(args["earthRibbon"]) + game.earth_ribbon_id = args["earthRibbon"] end if args.key? "worldRibbon" - game.world_ribbon = GiftRibbon.find_by_id(args["worldRibbon"]) + game.world_ribbon_id = args["worldRibbon"] end - game.save! if game.changed? - - args["boxes"].each_with_index do |box_name,index| - box = Box.find_or_initialize_by(trainer: game, number: index) - box.name = box_name - box.save! - end + game.save! if game.new_record? or game.changed? game.pokemon.clear @@ -97,7 +106,7 @@ module Pokeviewer if param["storage"] == "party" pk.box = nil elsif param["storage"] == "box" - pk.box = param["box"] + pk.box = param["box"] + 1 end pk.slot = param["slot"] diff --git a/app/models/pokeviewer/box.rb b/app/models/pokeviewer/box.rb deleted file mode 100644 index 090175e..0000000 --- a/app/models/pokeviewer/box.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Pokeviewer - class Box < ApplicationRecord - belongs_to :trainer - - validates :name, presence: true - - validates :number, presence: true, - numericality: { - greater_than_or_equal_to: 0, - less_than: 14, - only_integer: true }, - uniqueness: { scope: :trainer_id } - - def contents - pokes = trainer.pokemon.where(box: number).order("slot ASC").to_a - - result = [] - (0..29).each do |i| - if pokes.empty? or (pokes.first.slot == i) - result << pokes.shift - else - result << nil - end - end - - result - end - end -end diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 9f0b4af..3818f58 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb @@ -3,9 +3,36 @@ module Pokeviewer extend Enumerize belongs_to :species - belongs_to :trainer, optional: true + has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy + belongs_to :trainer, optional: true + + validates :box, numericality: { + greater_than_or_equal_to: 1, + less_than_or_equal_to: 14, + only_integer: true }, + allow_nil: true + + validates :slot, presence: true, + uniqueness: { scope: [:trainer_id, :box] }, + numericality: { + greater_than_or_equal_to: 0, + only_integer: true }, + unless: Proc.new { |a| a.trainer_id.nil? } + + validates :slot, + numericality: { less_than: 30 }, + unless: Proc.new { |a| a.trainer_id.nil? or a.box.nil? } + + validates :slot, + numericality: { less_than: 6 }, + unless: Proc.new { |a| a.trainer_id.nil? or not a.box.nil? } + + scope :party, -> { where(box: nil) } + scope :box, ->(n) { where(box: n) } + scope :unaccounted, -> { where(trainer_id: nil) } + validate :uuid_is_constant, on: :update before_create :set_uuid @@ -41,10 +68,6 @@ module Pokeviewer :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z, :question, :exclamation] - validates :slot, presence: true, - uniqueness: { scope: [:trainer_id, :box] }, - unless: Proc.new { |a| a.trainer.nil? } - def to_param uuid end diff --git a/app/models/pokeviewer/trainer.rb b/app/models/pokeviewer/trainer.rb index 7862c1e..b79547d 100644 --- a/app/models/pokeviewer/trainer.rb +++ b/app/models/pokeviewer/trainer.rb @@ -3,7 +3,6 @@ module Pokeviewer extend Enumerize has_many :pokemon, dependent: :nullify - has_many :boxes, -> { order("number ASC") }, dependent: :destroy validates :number, presence: true, numericality: { greater_than_or_equal_to: 0, only_integer: true } @@ -24,12 +23,61 @@ module Pokeviewer belongs_to :earth_ribbon, class_name: "GiftRibbon", optional: true belongs_to :world_ribbon, class_name: "GiftRibbon", optional: true - def display_number - number.to_s.rjust(5, '0') - end + validates :box_1_name, presence: true + validates :box_2_name, presence: true + validates :box_3_name, presence: true + validates :box_4_name, presence: true + validates :box_5_name, presence: true + validates :box_6_name, presence: true + validates :box_7_name, presence: true + validates :box_8_name, presence: true + validates :box_9_name, presence: true + validates :box_10_name, presence: true + validates :box_11_name, presence: true + validates :box_12_name, presence: true + validates :box_13_name, presence: true + validates :box_14_name, presence: true def party - pokemon.where(box: nil).order("slot ASC") + pokemon.party.includes(:species, :revisions) + end + + def box(n) + pokemon.box(n).includes(:species, :revisions) + end + + def box_name(n) + if n > 0 and n <= 14 + send "box_#{n}_name".intern + else + nil + end + end + + def box_contents(n) + pokes = box(n).to_a + + result = [] + (0..29).each do |i| + if pokes.empty? or (pokes.first.slot == i) + result << pokes.shift + else + result << nil + end + end + + result + end + + def boxes + (1..14).map { |n| { + name: box_name(n), + pokemon: box_contents(n) + }} + end + + def display_number + number.to_s.rjust(5, '0') end def gift_ribbon_description(ribbon) diff --git a/app/views/pokeviewer/pokemon/index.html.haml b/app/views/pokeviewer/pokemon/index.html.haml index f16f0bc..0be3edd 100644 --- a/app/views/pokeviewer/pokemon/index.html.haml +++ b/app/views/pokeviewer/pokemon/index.html.haml @@ -12,9 +12,9 @@ %span.party-name= link_to p.revisions.last.nickname, p - trainer.boxes.each do |box| .pc-box - %h3= box.name + %h3= box[:name] %table.pc-contents - - box.contents.each_slice(6) do |row| + - box[:pokemon].each_slice(6) do |row| %tr - row.each do |p| %td.pc-pokemon.pkv-has-hover -- cgit 1.4.1