about summary refs log tree commit diff stats
path: root/app
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-10-03 16:18:46 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-10-03 16:18:46 -0400
commit4bc96847f621c4cb3940fe99b3b4b67b20596189 (patch)
treec5b0e651e1ed0663a5eabc29966ca9778cdcd5eb /app
parentb618e52428bb659cb1fe3bdbe3d3763d48b4556c (diff)
downloadpokeviewer-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')
-rw-r--r--app/jobs/pokeviewer/extract_save_data_job.rb41
-rw-r--r--app/models/pokeviewer/box.rb29
-rw-r--r--app/models/pokeviewer/pokemon.rb33
-rw-r--r--app/models/pokeviewer/trainer.rb58
-rw-r--r--app/views/pokeviewer/pokemon/index.html.haml4
5 files changed, 108 insertions, 57 deletions
diff --git a/app/jobs/pokeviewer/extract_save_data_job.rb b/app/jobs/pokeviewer/extract_save_data_job.rb index 99d71f1..14ba9a6 100644 --- a/app/jobs/pokeviewer/extract_save_data_job.rb +++ b/app/jobs/pokeviewer/extract_save_data_job.rb
@@ -3,7 +3,7 @@ module Pokeviewer
3 queue_as :default 3 queue_as :default
4 4
5 def perform(args) 5 def perform(args)
6 game = Trainer.find_or_create_by!( 6 game = Trainer.find_or_initialize_by(
7 name: args["playerName"], 7 name: args["playerName"],
8 number: args["playerId"]) do |r| 8 number: args["playerId"]) do |r|
9 case args["gameId"].to_i 9 case args["gameId"].to_i
@@ -22,41 +22,50 @@ module Pokeviewer
22 end 22 end
23 end 23 end
24 24
25 game.box_1_name = args["boxes"].shift
26 game.box_2_name = args["boxes"].shift
27 game.box_3_name = args["boxes"].shift
28 game.box_4_name = args["boxes"].shift
29 game.box_5_name = args["boxes"].shift
30 game.box_6_name = args["boxes"].shift
31 game.box_7_name = args["boxes"].shift
32 game.box_8_name = args["boxes"].shift
33 game.box_9_name = args["boxes"].shift
34 game.box_10_name = args["boxes"].shift
35 game.box_11_name = args["boxes"].shift
36 game.box_12_name = args["boxes"].shift
37 game.box_13_name = args["boxes"].shift
38 game.box_14_name = args["boxes"].shift
39
25 if args.key? "marineRibbon" 40 if args.key? "marineRibbon"
26 game.marine_ribbon = GiftRibbon.find_by_id(args["marineRibbon"]) 41 game.marine_ribbon_id = args["marineRibbon"]
27 end 42 end
28 43
29 if args.key? "landRibbon" 44 if args.key? "landRibbon"
30 game.land_ribbon = GiftRibbon.find_by_id(args["landRibbon"]) 45 game.land_ribbon_id = args["landRibbon"]
31 end 46 end
32 47
33 if args.key? "skyRibbon" 48 if args.key? "skyRibbon"
34 game.sky_ribbon = GiftRibbon.find_by_id(args["skyRibbon"]) 49 game.sky_ribbon_id = args["skyRibbon"]
35 end 50 end
36 51
37 if args.key? "countryRibbon" 52 if args.key? "countryRibbon"
38 game.country_ribbon = GiftRibbon.find_by_id(args["countryRibbon"]) 53 game.country_ribbon_id = args["countryRibbon"]
39 end 54 end
40 55
41 if args.key? "nationalRibbon" 56 if args.key? "nationalRibbon"
42 game.national_ribbon = GiftRibbon.find_by_id(args["nationalRibbon"]) 57 game.national_ribbon_id = args["nationalRibbon"]
43 end 58 end
44 59
45 if args.key? "earthRibbon" 60 if args.key? "earthRibbon"
46 game.earth_ribbon = GiftRibbon.find_by_id(args["earthRibbon"]) 61 game.earth_ribbon_id = args["earthRibbon"]
47 end 62 end
48 63
49 if args.key? "worldRibbon" 64 if args.key? "worldRibbon"
50 game.world_ribbon = GiftRibbon.find_by_id(args["worldRibbon"]) 65 game.world_ribbon_id = args["worldRibbon"]
51 end 66 end
52 67
53 game.save! if game.changed? 68 game.save! if game.new_record? or game.changed?
54
55 args["boxes"].each_with_index do |box_name,index|
56 box = Box.find_or_initialize_by(trainer: game, number: index)
57 box.name = box_name
58 box.save!
59 end
60 69
61 game.pokemon.clear 70 game.pokemon.clear
62 71
@@ -97,7 +106,7 @@ module Pokeviewer
97 if param["storage"] == "party" 106 if param["storage"] == "party"
98 pk.box = nil 107 pk.box = nil
99 elsif param["storage"] == "box" 108 elsif param["storage"] == "box"
100 pk.box = param["box"] 109 pk.box = param["box"] + 1
101 end 110 end
102 111
103 pk.slot = param["slot"] 112 pk.slot = param["slot"]
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 @@
1module 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
29end
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)
diff --git a/app/views/pokeviewer/pokemon/index.html.haml b/app/views/pokeviewer/pokemon/index.html.haml index f16f0bc..0be3edd 100644 --- a/app/views/pokeviewer/pokemon/index.html.haml +++ b/app/views/pokeviewer/pokemon/index.html.haml
@@ -12,9 +12,9 @@
12 %span.party-name= link_to p.revisions.last.nickname, p 12 %span.party-name= link_to p.revisions.last.nickname, p
13 - trainer.boxes.each do |box| 13 - trainer.boxes.each do |box|
14 .pc-box 14 .pc-box
15 %h3= box.name 15 %h3= box[:name]
16 %table.pc-contents 16 %table.pc-contents
17 - box.contents.each_slice(6) do |row| 17 - box[:pokemon].each_slice(6) do |row|
18 %tr 18 %tr
19 - row.each do |p| 19 - row.each do |p|
20 %td.pc-pokemon.pkv-has-hover 20 %td.pc-pokemon.pkv-has-hover