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/models/pokeviewer/box.rb | 29 -------------------- app/models/pokeviewer/pokemon.rb | 33 +++++++++++++++++++---- app/models/pokeviewer/trainer.rb | 58 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 39 deletions(-) delete mode 100644 app/models/pokeviewer/box.rb (limited to 'app/models') 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) -- cgit 1.4.1