blob: 6c339547ccd6eb5d5fb814a6b63c69427898542a (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 | class StopAreaReferential < ApplicationModel
  validates :registration_number_format, format: { with: /\AX*\z/ }
  include ObjectidFormatterSupport
  has_many :stop_area_referential_memberships
  has_many :organisations, through: :stop_area_referential_memberships
  has_many :stop_areas, class_name: 'Chouette::StopArea'
  has_many :stop_area_referential_syncs, -> {order created_at: :desc}
  has_many :workbenches
  has_one  :workgroup
  def add_member(organisation, options = {})
    attributes = options.merge organisation: organisation
    stop_area_referential_memberships.build attributes unless organisations.include?(organisation)
  end
  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 = "A" * self.registration_number_format.size
      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
 |