diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-10-03 16:18:46 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-10-03 16:18:46 -0400 |
commit | 4bc96847f621c4cb3940fe99b3b4b67b20596189 (patch) | |
tree | c5b0e651e1ed0663a5eabc29966ca9778cdcd5eb /app/models | |
parent | b618e52428bb659cb1fe3bdbe3d3763d48b4556c (diff) | |
download | pokeviewer-4bc96847f621c4cb3940fe99b3b4b67b20596189.tar.gz pokeviewer-4bc96847f621c4cb3940fe99b3b4b67b20596189.tar.bz2 pokeviewer-4bc96847f621c4cb3940fe99b3b4b67b20596189.zip |
Removed box model
It seemed kind of strange to have a model that there should always be exactly 14 of for each of the parent (Trainer) model instances, so the box names were moved into the Trainer model, and the Box model was removed. This commit also adds some eager loading to speed up page loading times. Also made a small change to the way the gift ribbons are extracted. refs #2
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/pokeviewer/box.rb | 29 | ||||
-rw-r--r-- | app/models/pokeviewer/pokemon.rb | 33 | ||||
-rw-r--r-- | app/models/pokeviewer/trainer.rb | 58 |
3 files changed, 81 insertions, 39 deletions
diff --git a/app/models/pokeviewer/box.rb b/app/models/pokeviewer/box.rb deleted file mode 100644 index 090175e..0000000 --- a/app/models/pokeviewer/box.rb +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
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/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 9f0b4af..3818f58 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb | |||
@@ -3,9 +3,36 @@ module Pokeviewer | |||
3 | extend Enumerize | 3 | extend Enumerize |
4 | 4 | ||
5 | belongs_to :species | 5 | belongs_to :species |
6 | belongs_to :trainer, optional: true | 6 | |
7 | has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy | 7 | has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy |
8 | 8 | ||
9 | belongs_to :trainer, optional: true | ||
10 | |||
11 | validates :box, numericality: { | ||
12 | greater_than_or_equal_to: 1, | ||
13 | less_than_or_equal_to: 14, | ||
14 | only_integer: true }, | ||
15 | allow_nil: true | ||
16 | |||
17 | validates :slot, presence: true, | ||
18 | uniqueness: { scope: [:trainer_id, :box] }, | ||
19 | numericality: { | ||
20 | greater_than_or_equal_to: 0, | ||
21 | only_integer: true }, | ||
22 | unless: Proc.new { |a| a.trainer_id.nil? } | ||
23 | |||
24 | validates :slot, | ||
25 | numericality: { less_than: 30 }, | ||
26 | unless: Proc.new { |a| a.trainer_id.nil? or a.box.nil? } | ||
27 | |||
28 | validates :slot, | ||
29 | numericality: { less_than: 6 }, | ||
30 | unless: Proc.new { |a| a.trainer_id.nil? or not a.box.nil? } | ||
31 | |||
32 | scope :party, -> { where(box: nil) } | ||
33 | scope :box, ->(n) { where(box: n) } | ||
34 | scope :unaccounted, -> { where(trainer_id: nil) } | ||
35 | |||
9 | validate :uuid_is_constant, on: :update | 36 | validate :uuid_is_constant, on: :update |
10 | before_create :set_uuid | 37 | before_create :set_uuid |
11 | 38 | ||
@@ -41,10 +68,6 @@ module Pokeviewer | |||
41 | :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z, | 68 | :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z, |
42 | :question, :exclamation] | 69 | :question, :exclamation] |
43 | 70 | ||
44 | validates :slot, presence: true, | ||
45 | uniqueness: { scope: [:trainer_id, :box] }, | ||
46 | unless: Proc.new { |a| a.trainer.nil? } | ||
47 | |||
48 | def to_param | 71 | def to_param |
49 | uuid | 72 | uuid |
50 | end | 73 | end |
diff --git a/app/models/pokeviewer/trainer.rb b/app/models/pokeviewer/trainer.rb index 7862c1e..b79547d 100644 --- a/app/models/pokeviewer/trainer.rb +++ b/app/models/pokeviewer/trainer.rb | |||
@@ -3,7 +3,6 @@ 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 | ||
7 | 6 | ||
8 | validates :number, presence: true, | 7 | validates :number, presence: true, |
9 | numericality: { greater_than_or_equal_to: 0, only_integer: true } | 8 | numericality: { greater_than_or_equal_to: 0, only_integer: true } |
@@ -24,12 +23,61 @@ module Pokeviewer | |||
24 | belongs_to :earth_ribbon, class_name: "GiftRibbon", optional: true | 23 | belongs_to :earth_ribbon, class_name: "GiftRibbon", optional: true |
25 | belongs_to :world_ribbon, class_name: "GiftRibbon", optional: true | 24 | belongs_to :world_ribbon, class_name: "GiftRibbon", optional: true |
26 | 25 | ||
27 | def display_number | 26 | validates :box_1_name, presence: true |
28 | number.to_s.rjust(5, '0') | 27 | validates :box_2_name, presence: true |
29 | end | 28 | validates :box_3_name, presence: true |
29 | validates :box_4_name, presence: true | ||
30 | validates :box_5_name, presence: true | ||
31 | validates :box_6_name, presence: true | ||
32 | validates :box_7_name, presence: true | ||
33 | validates :box_8_name, presence: true | ||
34 | validates :box_9_name, presence: true | ||
35 | validates :box_10_name, presence: true | ||
36 | validates :box_11_name, presence: true | ||
37 | validates :box_12_name, presence: true | ||
38 | validates :box_13_name, presence: true | ||
39 | validates :box_14_name, presence: true | ||
30 | 40 | ||
31 | def party | 41 | def party |
32 | pokemon.where(box: nil).order("slot ASC") | 42 | pokemon.party.includes(:species, :revisions) |
43 | end | ||
44 | |||
45 | def box(n) | ||
46 | pokemon.box(n).includes(:species, :revisions) | ||
47 | end | ||
48 | |||
49 | def box_name(n) | ||
50 | if n > 0 and n <= 14 | ||
51 | send "box_#{n}_name".intern | ||
52 | else | ||
53 | nil | ||
54 | end | ||
55 | end | ||
56 | |||
57 | def box_contents(n) | ||
58 | pokes = box(n).to_a | ||
59 | |||
60 | result = [] | ||
61 | (0..29).each do |i| | ||
62 | if pokes.empty? or (pokes.first.slot == i) | ||
63 | result << pokes.shift | ||
64 | else | ||
65 | result << nil | ||
66 | end | ||
67 | end | ||
68 | |||
69 | result | ||
70 | end | ||
71 | |||
72 | def boxes | ||
73 | (1..14).map { |n| { | ||
74 | name: box_name(n), | ||
75 | pokemon: box_contents(n) | ||
76 | }} | ||
77 | end | ||
78 | |||
79 | def display_number | ||
80 | number.to_s.rjust(5, '0') | ||
33 | end | 81 | end |
34 | 82 | ||
35 | def gift_ribbon_description(ribbon) | 83 | def gift_ribbon_description(ribbon) |