about summary refs log tree commit diff stats
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
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
-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
-rw-r--r--db/migrate/20171003191205_remove_boxes.rb106
-rw-r--r--test/dummy/db/schema.rb26
7 files changed, 229 insertions, 68 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
diff --git a/db/migrate/20171003191205_remove_boxes.rb b/db/migrate/20171003191205_remove_boxes.rb new file mode 100644 index 0000000..476b98a --- /dev/null +++ b/db/migrate/20171003191205_remove_boxes.rb
@@ -0,0 +1,106 @@
1class RemoveBoxes < ActiveRecord::Migration[5.1]
2 def up
3 change_table :pokeviewer_trainers do |t|
4 t.string :box_1_name, null: false, default: ""
5 t.string :box_2_name, null: false, default: ""
6 t.string :box_3_name, null: false, default: ""
7 t.string :box_4_name, null: false, default: ""
8 t.string :box_5_name, null: false, default: ""
9 t.string :box_6_name, null: false, default: ""
10 t.string :box_7_name, null: false, default: ""
11 t.string :box_8_name, null: false, default: ""
12 t.string :box_9_name, null: false, default: ""
13 t.string :box_10_name, null: false, default: ""
14 t.string :box_11_name, null: false, default: ""
15 t.string :box_12_name, null: false, default: ""
16 t.string :box_13_name, null: false, default: ""
17 t.string :box_14_name, null: false, default: ""
18 end
19
20 change_column_default :pokeviewer_trainers, :box_1_name, nil
21 change_column_default :pokeviewer_trainers, :box_2_name, nil
22 change_column_default :pokeviewer_trainers, :box_3_name, nil
23 change_column_default :pokeviewer_trainers, :box_4_name, nil
24 change_column_default :pokeviewer_trainers, :box_5_name, nil
25 change_column_default :pokeviewer_trainers, :box_6_name, nil
26 change_column_default :pokeviewer_trainers, :box_7_name, nil
27 change_column_default :pokeviewer_trainers, :box_8_name, nil
28 change_column_default :pokeviewer_trainers, :box_9_name, nil
29 change_column_default :pokeviewer_trainers, :box_10_name, nil
30 change_column_default :pokeviewer_trainers, :box_11_name, nil
31 change_column_default :pokeviewer_trainers, :box_12_name, nil
32 change_column_default :pokeviewer_trainers, :box_13_name, nil
33 change_column_default :pokeviewer_trainers, :box_14_name, nil
34
35 Pokeviewer::Trainer.all.each do |t|
36 boxes = ActiveRecord::Base.connection.select_all(
37 "SELECT * FROM pokeviewer_boxes WHERE trainer_id = ? ORDER BY number ASC",
38 t.trainer_id).map { |b| b["name"] }
39
40 t.box_1_name = boxes.shift
41 t.box_2_name = boxes.shift
42 t.box_3_name = boxes.shift
43 t.box_4_name = boxes.shift
44 t.box_5_name = boxes.shift
45 t.box_6_name = boxes.shift
46 t.box_7_name = boxes.shift
47 t.box_8_name = boxes.shift
48 t.box_9_name = boxes.shift
49 t.box_10_name = boxes.shift
50 t.box_11_name = boxes.shift
51 t.box_12_name = boxes.shift
52 t.box_13_name = boxes.shift
53 t.box_14_name = boxes.shift
54
55 t.save
56 end
57
58 drop_table :pokeviewer_boxes
59 end
60
61 def down
62 create_table :pokeviewer_boxes do |t|
63 t.integer "trainer_id", null: false
64 t.integer "number", null: false
65 t.string "name", null: false
66
67 t.timestamps
68 end
69
70 add_index :pokeviewer_boxes, [:trainer_id, :number], unique: true
71
72 add_foreign_key :pokeviewer_boxes, :pokeviewer_trainers, column: :trainer_id
73
74 Pokeviewer::Trainer.all.each do |t|
75 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 1, name: t.box_1_name)
76 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 2, name: t.box_2_name)
77 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 3, name: t.box_3_name)
78 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 4, name: t.box_4_name)
79 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 5, name: t.box_5_name)
80 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 6, name: t.box_6_name)
81 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 7, name: t.box_7_name)
82 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 8, name: t.box_8_name)
83 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 9, name: t.box_9_name)
84 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 10, name: t.box_10_name)
85 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 11, name: t.box_11_name)
86 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 12, name: t.box_12_name)
87 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 13, name: t.box_13_name)
88 Pokeviewer::Box.create(trainer_id: t.trainer_id, number: 14, name: t.box_14_name)
89 end
90
91 remove_column :pokeviewer_trainers, :box_1_name
92 remove_column :pokeviewer_trainers, :box_2_name
93 remove_column :pokeviewer_trainers, :box_3_name
94 remove_column :pokeviewer_trainers, :box_4_name
95 remove_column :pokeviewer_trainers, :box_5_name
96 remove_column :pokeviewer_trainers, :box_6_name
97 remove_column :pokeviewer_trainers, :box_7_name
98 remove_column :pokeviewer_trainers, :box_8_name
99 remove_column :pokeviewer_trainers, :box_9_name
100 remove_column :pokeviewer_trainers, :box_10_name
101 remove_column :pokeviewer_trainers, :box_11_name
102 remove_column :pokeviewer_trainers, :box_12_name
103 remove_column :pokeviewer_trainers, :box_13_name
104 remove_column :pokeviewer_trainers, :box_14_name
105 end
106end
diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 87db1f8..b1c02e3 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb
@@ -10,17 +10,7 @@
10# 10#
11# It's strongly recommended that you check this file into your version control system. 11# It's strongly recommended that you check this file into your version control system.
12 12
13ActiveRecord::Schema.define(version: 20171003154157) do 13ActiveRecord::Schema.define(version: 20171003191205) do
14
15 create_table "pokeviewer_boxes", force: :cascade do |t|
16 t.integer "trainer_id", null: false
17 t.integer "number", null: false
18 t.string "name", null: false
19 t.datetime "created_at", null: false
20 t.datetime "updated_at", null: false
21 t.index ["trainer_id", "number"], name: "index_pokeviewer_boxes_on_trainer_id_and_number", unique: true
22 t.index ["trainer_id"], name: "index_pokeviewer_boxes_on_trainer_id"
23 end
24 14
25 create_table "pokeviewer_gift_ribbons", force: :cascade do |t| 15 create_table "pokeviewer_gift_ribbons", force: :cascade do |t|
26 t.string "description", null: false 16 t.string "description", null: false
@@ -158,6 +148,20 @@ ActiveRecord::Schema.define(version: 20171003154157) do
158 t.integer "national_ribbon_id" 148 t.integer "national_ribbon_id"
159 t.integer "earth_ribbon_id" 149 t.integer "earth_ribbon_id"
160 t.integer "world_ribbon_id" 150 t.integer "world_ribbon_id"
151 t.string "box_1_name", null: false
152 t.string "box_2_name", null: false
153 t.string "box_3_name", null: false
154 t.string "box_4_name", null: false
155 t.string "box_5_name", null: false
156 t.string "box_6_name", null: false
157 t.string "box_7_name", null: false
158 t.string "box_8_name", null: false
159 t.string "box_9_name", null: false
160 t.string "box_10_name", null: false
161 t.string "box_11_name", null: false
162 t.string "box_12_name", null: false
163 t.string "box_13_name", null: false
164 t.string "box_14_name", null: false
161 t.index ["country_ribbon_id"], name: "index_pokeviewer_trainers_on_country_ribbon_id" 165 t.index ["country_ribbon_id"], name: "index_pokeviewer_trainers_on_country_ribbon_id"
162 t.index ["earth_ribbon_id"], name: "index_pokeviewer_trainers_on_earth_ribbon_id" 166 t.index ["earth_ribbon_id"], name: "index_pokeviewer_trainers_on_earth_ribbon_id"
163 t.index ["land_ribbon_id"], name: "index_pokeviewer_trainers_on_land_ribbon_id" 167 t.index ["land_ribbon_id"], name: "index_pokeviewer_trainers_on_land_ribbon_id"