about summary refs log tree commit diff stats
path: root/app/models/trainer.rb
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2024-12-07 11:49:49 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2024-12-07 11:49:49 -0500
commit56f5841d4b9c12296cdfcaeff174b2627d59afc8 (patch)
tree4f7da4ebbe5ee15a1594b26466ed78e2cf10de35 /app/models/trainer.rb
parentc1b0443ba2aebdbd39291ddab0c189f3f4831320 (diff)
downloadpokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.gz
pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.tar.bz2
pokeviewer-56f5841d4b9c12296cdfcaeff174b2627d59afc8.zip
Migrate to full rails app
Diffstat (limited to 'app/models/trainer.rb')
-rw-r--r--app/models/trainer.rb93
1 files changed, 93 insertions, 0 deletions
diff --git a/app/models/trainer.rb b/app/models/trainer.rb new file mode 100644 index 0000000..930dbaf --- /dev/null +++ b/app/models/trainer.rb
@@ -0,0 +1,93 @@
1class Trainer < ApplicationRecord
2 extend Enumerize
3
4 has_many :pokemon, dependent: :nullify
5
6 has_many :pokedex_entries, dependent: :destroy
7
8 validates :number, presence: true,
9 numericality: { greater_than_or_equal_to: 0, only_integer: true }
10
11 validates :name, presence: true, uniqueness: {
12 scope: :number,
13 message: "and number should be pairwise unique" }
14
15 validates :game, presence: true
16 enumerize :game, in: [:ruby, :sapphire, :firered, :leafgreen, :emerald],
17 predicates: true
18
19 belongs_to :marine_ribbon, class_name: "GiftRibbon", optional: true
20 belongs_to :land_ribbon, class_name: "GiftRibbon", optional: true
21 belongs_to :sky_ribbon, class_name: "GiftRibbon", optional: true
22 belongs_to :country_ribbon, class_name: "GiftRibbon", optional: true
23 belongs_to :national_ribbon, class_name: "GiftRibbon", optional: true
24 belongs_to :earth_ribbon, class_name: "GiftRibbon", optional: true
25 belongs_to :world_ribbon, class_name: "GiftRibbon", optional: true
26
27 validates :box_1_name, presence: true
28 validates :box_2_name, presence: true
29 validates :box_3_name, presence: true
30 validates :box_4_name, presence: true
31 validates :box_5_name, presence: true
32 validates :box_6_name, presence: true
33 validates :box_7_name, presence: true
34 validates :box_8_name, presence: true
35 validates :box_9_name, presence: true
36 validates :box_10_name, presence: true
37 validates :box_11_name, presence: true
38 validates :box_12_name, presence: true
39 validates :box_13_name, presence: true
40 validates :box_14_name, presence: true
41
42 def party
43 pokemon.party.includes(current: [:species])
44 end
45
46 def box(n)
47 pokemon.box(n).includes(current: [:species])
48 end
49
50 def box_name(n)
51 if n > 0 and n <= 14
52 send "box_#{n}_name".intern
53 else
54 nil
55 end
56 end
57
58 def box_contents(n)
59 pokes = box(n).to_a
60
61 result = []
62 (0..29).each do |i|
63 if pokes.empty? or (pokes.first.slot == i)
64 result << pokes.shift
65 else
66 result << nil
67 end
68 end
69
70 result
71 end
72
73 def boxes
74 (1..14).map { |n| {
75 name: box_name(n),
76 pokemon: box_contents(n)
77 }}
78 end
79
80 def display_number
81 number.to_s.rjust(5, '0')
82 end
83
84 def gift_ribbon_description(ribbon)
85 gift_ribbon = send ribbon
86
87 if gift_ribbon.nil?
88 ""
89 else
90 gift_ribbon.description
91 end
92 end
93end