From b618e52428bb659cb1fe3bdbe3d3763d48b4556c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 3 Oct 2017 15:11:00 -0400 Subject: Made "met location" into an actual association --- app/jobs/pokeviewer/extract_save_data_job.rb | 4 ++-- app/models/pokeviewer/location.rb | 2 ++ app/models/pokeviewer/pokemon.rb | 12 ++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) (limited to 'app') diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index 546e6d2..99d71f1 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -69,7 +69,7 @@ module Pokeviewer if param["metLevel"] == 0 r.met_type = :hatched - r.met_location = param["metLocation"] + r.location_id = param["metLocation"] elsif param["metLocation"] == 254 r.met_type = :npc_trade elsif param["metLocation"] == 255 @@ -78,7 +78,7 @@ module Pokeviewer else r.met_type = :normal r.met_level = param["metLevel"] - r.met_location = param["metLocation"] + r.location_id = param["metLocation"] end r.shiny = param["shiny"] diff --git a/app/models/pokeviewer/location.rb b/app/models/pokeviewer/location.rb index e497cbc..db37df2 100644 --- a/app/models/pokeviewer/location.rb +++ b/app/models/pokeviewer/location.rb @@ -1,5 +1,7 @@ module Pokeviewer class Location < ApplicationRecord + has_many :pokemon, dependent: :nullify + validates :name, presence: true end end diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 6c33406..9f0b4af 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb @@ -24,6 +24,10 @@ module Pokeviewer validates :met_type, presence: true enumerize :met_type, in: [:normal, :hatched, :npc_trade, :fateful_encounter] + belongs_to :location, optional: true + validates :location, presence: true, + if: Proc.new { |c| c.met_type == :normal or c.met_type == :hatched} + validates :gender, presence: true enumerize :gender, in: [:genderless, :female, :male] @@ -105,14 +109,6 @@ module Pokeviewer (trainer.nil?) or (ot_name != trainer.name) or (ot_number != trainer.number) end - def location - if (met_type == :normal) or (met_type == :hatched) - Location.find_by_id(met_location) - else - nil - end - end - def display_ot_number ot_number.to_s.rjust(5, '0') end -- cgit 1.4.1