aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/stop_area_referential.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/stop_area_referential.rb')
-rw-r--r--app/models/stop_area_referential.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb
index 54e895cd0..ab416fd30 100644
--- a/app/models/stop_area_referential.rb
+++ b/app/models/stop_area_referential.rb
@@ -1,4 +1,6 @@
class StopAreaReferential < ActiveRecord::Base
+ validates :registration_number_format, format: { with: /\AX*\z/ }
+
include ObjectidFormatterSupport
has_many :stop_area_referential_memberships
has_many :organisations, through: :stop_area_referential_memberships
@@ -15,4 +17,30 @@ class StopAreaReferential < ActiveRecord::Base
def last_sync
stop_area_referential_syncs.last
end
+
+ def generate_registration_number
+ return "" unless registration_number_format.present?
+ last = self.stop_areas.order("registration_number DESC NULLS LAST").limit(1).first&.registration_number
+ if self.stop_areas.count == 26**self.registration_number_format.size
+ raise "NO MORE AVAILABLE VALUES FOR registration_number in referential #{self.name}"
+ end
+
+ return "A" * self.registration_number_format.size unless last
+
+ if last == "Z" * self.registration_number_format.size
+ val = "AAA"
+ while self.stop_areas.where(registration_number: val).exists?
+ val = val.next
+ end
+ val
+ else
+ last.next
+ end
+ end
+
+ def validates_registration_number value
+ return false unless value.size == registration_number_format.size
+ return false unless value =~ /^[A-Z]*$/
+ true
+ end
end