From 687299b33934d25017a226ba6b979e44f0b1f441 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Sep 2017 19:15:55 -0400 Subject: Added box names --- app/jobs/pokeviewer/extract_save_data_job.rb | 6 +++++ app/models/pokeviewer/box.rb | 29 ++++++++++++++++++++++ app/models/pokeviewer/trainer.rb | 20 +-------------- app/views/pokeviewer/pokemon/index.html.haml | 6 ++--- .../20170924224550_create_pokeviewer_boxes.rb | 15 +++++++++++ test/dummy/db/schema.rb | 12 ++++++++- 6 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 app/models/pokeviewer/box.rb create mode 100644 db/migrate/20170924224550_create_pokeviewer_boxes.rb diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index 404716a..3409a5f 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb @@ -22,6 +22,12 @@ module Pokeviewer end end + 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.pokemon.clear args["pokemon"].each do |param| diff --git a/app/models/pokeviewer/box.rb b/app/models/pokeviewer/box.rb new file mode 100644 index 0000000..090175e --- /dev/null +++ b/app/models/pokeviewer/box.rb @@ -0,0 +1,29 @@ +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/trainer.rb b/app/models/pokeviewer/trainer.rb index d3a5b91..f890d61 100644 --- a/app/models/pokeviewer/trainer.rb +++ b/app/models/pokeviewer/trainer.rb @@ -3,6 +3,7 @@ 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 } @@ -22,24 +23,5 @@ module Pokeviewer def party pokemon.where(box: nil).order("slot ASC") end - - def boxes - (0..13).map { |i| box(i) } - end - - def box(number) - pokes = 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/views/pokeviewer/pokemon/index.html.haml b/app/views/pokeviewer/pokemon/index.html.haml index 69b6a68..e536080 100644 --- a/app/views/pokeviewer/pokemon/index.html.haml +++ b/app/views/pokeviewer/pokemon/index.html.haml @@ -10,11 +10,11 @@ %li %span.party-icon= image_tag p.icon_path %span.party-name= link_to p.revisions.last.nickname, p - - trainer.boxes.each_with_index do |box,bid| + - trainer.boxes.each do |box| .pc-box - %h3= "Box #{bid+1}" + %h3= box.name %table.pc-contents - - box.each_slice(6) do |row| + - box.contents.each_slice(6) do |row| %tr - row.each do |p| %td.pc-pokemon diff --git a/db/migrate/20170924224550_create_pokeviewer_boxes.rb b/db/migrate/20170924224550_create_pokeviewer_boxes.rb new file mode 100644 index 0000000..02f6fa9 --- /dev/null +++ b/db/migrate/20170924224550_create_pokeviewer_boxes.rb @@ -0,0 +1,15 @@ +class CreatePokeviewerBoxes < ActiveRecord::Migration[5.1] + def change + create_table :pokeviewer_boxes do |t| + t.references :trainer, null: false + t.integer :number, null: false + t.string :name, null: false + + t.timestamps + end + + add_foreign_key :pokeviewer_boxes, :pokeviewer_trainer, column: :trainer_id + + add_index :pokeviewer_boxes, [:trainer_id, :number], unique: true + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 9a13685..e7322e9 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170924160524) do +ActiveRecord::Schema.define(version: 20170924224550) do + + create_table "pokeviewer_boxes", force: :cascade do |t| + t.integer "trainer_id", null: false + t.integer "number", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["trainer_id", "number"], name: "index_pokeviewer_boxes_on_trainer_id_and_number", unique: true + t.index ["trainer_id"], name: "index_pokeviewer_boxes_on_trainer_id" + end create_table "pokeviewer_moves", force: :cascade do |t| t.string "name", limit: 191, null: false -- cgit 1.4.1