From eae1c764df31b5e5aa2a2d8315b2b5c0997babf4 Mon Sep 17 00:00:00 2001
From: Kelly Rauchenberger <fefferburbia@gmail.com>
Date: Wed, 4 Oct 2017 23:09:37 -0400
Subject: Pokémon now show the Poké Ball that they're in
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also fixed a bug with viewing Pokémon that aren't in any game.
---
 app/jobs/pokeviewer/extract_save_data_job.rb         | 1 +
 app/models/pokeviewer/pokemon.rb                     | 9 +++++++++
 app/views/pokeviewer/pokemon/show.html.haml          | 3 ++-
 db/migrate/20171004203404_add_pokeball_to_pokemon.rb | 9 +++++++++
 test/dummy/db/schema.rb                              | 3 ++-
 5 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 db/migrate/20171004203404_add_pokeball_to_pokemon.rb

diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb
index f94ce74..e9e0231 100644
--- a/app/jobs/pokeviewer/extract_save_data_job.rb
+++ b/app/jobs/pokeviewer/extract_save_data_job.rb
@@ -96,6 +96,7 @@ module Pokeviewer
           r.nature = Pokemon.nature.values[param["nature"]]
           r.gender = param["gender"]
           r.second_ability = param["secondAbility"]
+          r.pokeball = Pokemon.pokeball.values[param["pokeball"] - 1]
 
           # Handle Unown form
           if r.species_id == 201
diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb
index 18d1533..268a417 100644
--- a/app/models/pokeviewer/pokemon.rb
+++ b/app/models/pokeviewer/pokemon.rb
@@ -1,6 +1,7 @@
 module Pokeviewer
   class Pokemon < ApplicationRecord
     extend Enumerize
+    extend ActiveModel::Naming
 
     belongs_to :species
 
@@ -69,6 +70,10 @@ module Pokeviewer
       :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z,
       :question, :exclamation]
 
+    validates :pokeball, presence: true
+    enumerize :pokeball, in: [:master, :ultra, :great, :poke, :safari, :net,
+      :dive, :nest, :repeat, :timer, :luxury, :premier]
+
     def to_param
       uuid
     end
@@ -191,6 +196,10 @@ module Pokeviewer
       end
     end
 
+    def pokeball_icon_path
+      "pokeviewer/items/#{Pokemon.pokeball.values.find_index(pokeball) + 1}.png"
+    end
+
     private
 
       def set_uuid
diff --git a/app/views/pokeviewer/pokemon/show.html.haml b/app/views/pokeviewer/pokemon/show.html.haml
index 4485932..e3bee11 100644
--- a/app/views/pokeviewer/pokemon/show.html.haml
+++ b/app/views/pokeviewer/pokemon/show.html.haml
@@ -1,4 +1,4 @@
-.pokemon{ class: "in-#{@pokemon.trainer.game}" }
+.pokemon{ class: (not @pokemon.trainer.nil?) && "in-#{@pokemon.trainer.game}" }
   .pokemon-basics
     .pokemon-species-id= "No. #{@pokemon.species_id}"
     .pokemon-species-name= @pokemon.species.name
@@ -90,6 +90,7 @@
             = ribbon[:description]
   .pokemon-etc
     .pd-details Misc
+    = image_tag(@pokemon.pokeball_icon_path)
     - unless @pokemon.revisions.last.item.nil?
       .pd-hold-item.pkv-has-hover
         = image_tag(@pokemon.revisions.last.item.icon_path)
diff --git a/db/migrate/20171004203404_add_pokeball_to_pokemon.rb b/db/migrate/20171004203404_add_pokeball_to_pokemon.rb
new file mode 100644
index 0000000..9568d10
--- /dev/null
+++ b/db/migrate/20171004203404_add_pokeball_to_pokemon.rb
@@ -0,0 +1,9 @@
+class AddPokeballToPokemon < ActiveRecord::Migration[5.1]
+  def change
+    change_table :pokeviewer_pokemon do |t|
+      t.string :pokeball, null: false, default: :poke
+    end
+
+    change_column_default :pokeviewer_pokemon, :pokeball, nil
+  end
+end
diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb
index b1c02e3..5ae8e1d 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: 20171003191205) do
+ActiveRecord::Schema.define(version: 20171004203404) do
 
   create_table "pokeviewer_gift_ribbons", force: :cascade do |t|
     t.string "description", null: false
@@ -68,6 +68,7 @@ ActiveRecord::Schema.define(version: 20171003191205) do
     t.integer "box"
     t.integer "slot"
     t.integer "location_id"
+    t.string "pokeball", null: false
     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