From 8baaa7947ea1e229f67149d221dea76450f040a5 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 26 Feb 2018 09:58:16 +0100 Subject: Refs #5972 @1h; Automatic registration_number generation --- app/assets/javascripts/forms.coffee | 2 +- app/helpers/stop_areas_helper.rb | 16 ++++++++++++++++ app/models/chouette/stop_area.rb | 21 +++++++++++++++++++++ app/models/stop_area_referential.rb | 28 ++++++++++++++++++++++++++++ app/views/stop_areas/_form.html.slim | 2 +- 5 files changed, 67 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/forms.coffee b/app/assets/javascripts/forms.coffee index b7ae3c6ca..9543220d0 100644 --- a/app/assets/javascripts/forms.coffee +++ b/app/assets/javascripts/forms.coffee @@ -25,7 +25,7 @@ isEdge = !isIE && !!window.StyleMedia if $('.page-action').children('.formSubmitr').length > 0 $('.page-action').children('.formSubmitr').remove() - $('.formSubmitr').appendTo('.page-action') + $('.formSubmitr').appendTo('.page-action').addClass('sticky-action') if isIE || isEdge $('.formSubmitr').off() diff --git a/app/helpers/stop_areas_helper.rb b/app/helpers/stop_areas_helper.rb index 05ae042f5..fa99f1b4c 100644 --- a/app/helpers/stop_areas_helper.rb +++ b/app/helpers/stop_areas_helper.rb @@ -54,4 +54,20 @@ module StopAreasHelper end end + def stop_area_registration_number_title stop_area + if stop_area&.stop_area_referential&.registration_number_format.present? + return t("formtastic.titles.stop_area.registration_number_format", registration_number_format: stop_area.stop_area_referential.registration_number_format) + end + t "formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.registration_number" + end + + def stop_area_registration_number_is_required stop_area + val = format_restriction_for_locales(@referential) == '.hub' + val ||= stop_area&.stop_area_referential&.registration_number_format.present? + val + end + + def stop_area_registration_number_value stop_area + stop_area&.registration_number || stop_area&.stop_area_referential&.generate_registration_number + end end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index f58f97eee..0a27b2f39 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -46,6 +46,11 @@ module Chouette validates_numericality_of :waiting_time, greater_than_or_equal_to: 0, only_integer: true, if: :waiting_time validate :parent_area_type_must_be_greater validate :area_type_of_right_kind + validate :registration_number_is_set + + before_validation do + self.registration_number ||= self.stop_area_referential.generate_registration_number + end def self.nullable_attributes [:registration_number, :street_name, :country_code, :fare_code, @@ -73,6 +78,22 @@ module Chouette end end + def registration_number_is_set + return unless self.stop_area_referential.registration_number_format.present? + if self.stop_area_referential.stop_areas.where(registration_number: self.registration_number).\ + where.not(id: self.id).exists? + errors.add(:registration_number, I18n.t('stop_areas.errors.registration_number.already_taken')) + end + + unless self.registration_number.present? + errors.add(:registration_number, I18n.t('stop_areas.errors.registration_number.cannot_be_empty')) + end + + unless self.stop_area_referential.validates_registration_number(self.registration_number) + errors.add(:registration_number, I18n.t('stop_areas.errors.registration_number.invalid')) + end + end + after_update :clean_invalid_access_links before_save :coordinates_to_lat_lng 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 diff --git a/app/views/stop_areas/_form.html.slim b/app/views/stop_areas/_form.html.slim index bb1fbe1e9..c63e95c89 100644 --- a/app/views/stop_areas/_form.html.slim +++ b/app/views/stop_areas/_form.html.slim @@ -48,7 +48,7 @@ - if has_feature?(:stop_area_waiting_time) = f.input :waiting_time, input_html: { min: 0 } - = f.input :registration_number, required: format_restriction_for_locales(@referential) == '.hub', :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.registration_number")} + = f.input :registration_number, required: stop_area_registration_number_is_required(f.object), :input_html => {title: stop_area_registration_number_title(f.object), value: stop_area_registration_number_value(f.object)} = f.input :fare_code = f.input :nearest_topic_name, :input_html => {:title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.nearest_topic_name")} = f.input :comment, as: :text, :input_html => {:rows => 5, :title => t("formtastic.titles#{format_restriction_for_locales(@referential)}.stop_area.comment")} -- cgit v1.2.3 From 50f4bbe61c143cd4e3f1f305306c211654c458bb Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Mon, 12 Mar 2018 09:33:02 +0100 Subject: Replace hardcoded AAA by expression sized by registration_number_format. Refs #5972 --- app/models/stop_area_referential.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb index ab416fd30..9e9e02d80 100644 --- a/app/models/stop_area_referential.rb +++ b/app/models/stop_area_referential.rb @@ -28,7 +28,7 @@ class StopAreaReferential < ActiveRecord::Base return "A" * self.registration_number_format.size unless last if last == "Z" * self.registration_number_format.size - val = "AAA" + val = "A" * self.registration_number_format.size while self.stop_areas.where(registration_number: val).exists? val = val.next end -- cgit v1.2.3