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 +++--- 4 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 app/models/pokeviewer/box.rb (limited to 'app') 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 -- cgit 1.4.1