diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/pokeviewer/box.rb | 29 | ||||
-rw-r--r-- | app/models/pokeviewer/trainer.rb | 20 |
2 files changed, 30 insertions, 19 deletions
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 @@ | |||
1 | module Pokeviewer | ||
2 | class Box < ApplicationRecord | ||
3 | belongs_to :trainer | ||
4 | |||
5 | validates :name, presence: true | ||
6 | |||
7 | validates :number, presence: true, | ||
8 | numericality: { | ||
9 | greater_than_or_equal_to: 0, | ||
10 | less_than: 14, | ||
11 | only_integer: true }, | ||
12 | uniqueness: { scope: :trainer_id } | ||
13 | |||
14 | def contents | ||
15 | pokes = trainer.pokemon.where(box: number).order("slot ASC").to_a | ||
16 | |||
17 | result = [] | ||
18 | (0..29).each do |i| | ||
19 | if pokes.empty? or (pokes.first.slot == i) | ||
20 | result << pokes.shift | ||
21 | else | ||
22 | result << nil | ||
23 | end | ||
24 | end | ||
25 | |||
26 | result | ||
27 | end | ||
28 | end | ||
29 | 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 | |||
3 | extend Enumerize | 3 | extend Enumerize |
4 | 4 | ||
5 | has_many :pokemon, dependent: :nullify | 5 | has_many :pokemon, dependent: :nullify |
6 | has_many :boxes, -> { order("number ASC") }, dependent: :destroy | ||
6 | 7 | ||
7 | validates :number, presence: true, | 8 | validates :number, presence: true, |
8 | numericality: { greater_than_or_equal_to: 0, only_integer: true } | 9 | numericality: { greater_than_or_equal_to: 0, only_integer: true } |
@@ -22,24 +23,5 @@ module Pokeviewer | |||
22 | def party | 23 | def party |
23 | pokemon.where(box: nil).order("slot ASC") | 24 | pokemon.where(box: nil).order("slot ASC") |
24 | end | 25 | end |
25 | |||
26 | def boxes | ||
27 | (0..13).map { |i| box(i) } | ||
28 | end | ||
29 | |||
30 | def box(number) | ||
31 | pokes = pokemon.where(box: number).order("slot ASC").to_a | ||
32 | |||
33 | result = [] | ||
34 | (0..29).each do |i| | ||
35 | if pokes.empty? or (pokes.first.slot == i) | ||
36 | result << pokes.shift | ||
37 | else | ||
38 | result << nil | ||
39 | end | ||
40 | end | ||
41 | |||
42 | result | ||
43 | end | ||
44 | end | 26 | end |
45 | end | 27 | end |