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 +++------ .../20171003154157_rename_pokemon_met_location.rb | 30 ++++++++++++++++++++++ test/dummy/db/schema.rb | 4 +-- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20171003154157_rename_pokemon_met_location.rb 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 diff --git a/db/migrate/20171003154157_rename_pokemon_met_location.rb b/db/migrate/20171003154157_rename_pokemon_met_location.rb new file mode 100644 index 0000000..4ccba57 --- /dev/null +++ b/db/migrate/20171003154157_rename_pokemon_met_location.rb @@ -0,0 +1,30 @@ +class RenamePokemonMetLocation < ActiveRecord::Migration[5.1] + def up + add_column :pokeviewer_pokemon, :location_id, :integer, null: true + + add_foreign_key :pokeviewer_pokemon, :pokeviewer_locations, + column: :location_id + + Pokeviewer::Pokemon.all.each do |p| + unless p.met_location.nil? + p.location_id = p.met_location.to_i + p.save! + end + end + + remove_column :pokeviewer_pokemon, :met_location + end + + def down + add_column :pokeviewer_pokemon, :met_location, :string + + Pokeviewer::Pokemon.all.each do |p| + unless p.location_id.nil? + p.met_location = p.location_id.to_s + p.save! + end + end + + remove_column :pokeviewer_pokemon, :location_id + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 84e15a3..87db1f8 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170930213633) do +ActiveRecord::Schema.define(version: 20171003154157) do create_table "pokeviewer_boxes", force: :cascade do |t| t.integer "trainer_id", null: false @@ -66,7 +66,6 @@ ActiveRecord::Schema.define(version: 20170930213633) do t.string "ot_name", null: false t.integer "ot_number", null: false t.string "met_type", null: false - t.string "met_location" t.integer "met_level" t.boolean "shiny", default: false, null: false t.string "nature", null: false @@ -78,6 +77,7 @@ ActiveRecord::Schema.define(version: 20170930213633) do t.string "ot_gender", default: "", null: false t.integer "box" t.integer "slot" + t.integer "location_id" t.index ["key"], name: "index_pokeviewer_pokemon_on_key", unique: true t.index ["species_id"], name: "index_pokeviewer_pokemon_on_species_id" t.index ["trainer_id"], name: "index_pokeviewer_pokemon_on_trainer_id" -- cgit 1.4.1