diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/pokeviewer/pokemon.rb | 16 | ||||
-rw-r--r-- | app/models/pokeviewer/revision.rb | 9 |
2 files changed, 24 insertions, 1 deletions
diff --git a/app/models/pokeviewer/pokemon.rb b/app/models/pokeviewer/pokemon.rb index 012abab..77ee89a 100644 --- a/app/models/pokeviewer/pokemon.rb +++ b/app/models/pokeviewer/pokemon.rb | |||
@@ -4,7 +4,9 @@ module Pokeviewer | |||
4 | extend ActiveModel::Naming | 4 | extend ActiveModel::Naming |
5 | 5 | ||
6 | has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy | 6 | has_many :revisions, -> { order "sequential_id ASC" }, dependent: :destroy |
7 | has_one :current, -> { order "sequential_id DESC" }, class_name: "Revision" | 7 | |
8 | belongs_to :current, class_name: "Revision", optional: true | ||
9 | validate :current_is_cached | ||
8 | 10 | ||
9 | belongs_to :trainer, optional: true | 11 | belongs_to :trainer, optional: true |
10 | 12 | ||
@@ -149,5 +151,17 @@ module Pokeviewer | |||
149 | def uuid_is_constant | 151 | def uuid_is_constant |
150 | errors.add(:uuid, "can't be changed") if self.uuid_changed? | 152 | errors.add(:uuid, "can't be changed") if self.uuid_changed? |
151 | end | 153 | end |
154 | |||
155 | def current_is_cached | ||
156 | if self.revisions.empty? | ||
157 | unless self.current_id.nil? | ||
158 | errors.add(:current, "must be null when there are no revisions") | ||
159 | end | ||
160 | else | ||
161 | unless self.current_id = self.revisions.last.id | ||
162 | errors.add(:current, "is not up-to-date") | ||
163 | end | ||
164 | end | ||
165 | end | ||
152 | end | 166 | end |
153 | end | 167 | end |
diff --git a/app/models/pokeviewer/revision.rb b/app/models/pokeviewer/revision.rb index 64b0e99..e5ad1dd 100644 --- a/app/models/pokeviewer/revision.rb +++ b/app/models/pokeviewer/revision.rb | |||
@@ -17,6 +17,8 @@ module Pokeviewer | |||
17 | belongs_to :pokemon | 17 | belongs_to :pokemon |
18 | acts_as_sequenced scope: :pokemon_id | 18 | acts_as_sequenced scope: :pokemon_id |
19 | 19 | ||
20 | after_create :cache_pokemon_current | ||
21 | |||
20 | belongs_to :species | 22 | belongs_to :species |
21 | 23 | ||
22 | validates :nickname, presence: true | 24 | validates :nickname, presence: true |
@@ -483,5 +485,12 @@ module Pokeviewer | |||
483 | 485 | ||
484 | result | 486 | result |
485 | end | 487 | end |
488 | |||
489 | private | ||
490 | |||
491 | def cache_pokemon_current | ||
492 | self.pokemon.current_id = self.id | ||
493 | self.pokemon.save! | ||
494 | end | ||
486 | end | 495 | end |
487 | end | 496 | end |