diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/chouette/company.rb | 5 | ||||
| -rw-r--r-- | app/models/chouette/line.rb | 2 | ||||
| -rw-r--r-- | app/models/chouette/netex_object_id.rb | 40 | ||||
| -rw-r--r-- | app/models/concerns/default_netex_attributes_support.rb | 81 |
4 files changed, 126 insertions, 2 deletions
diff --git a/app/models/chouette/company.rb b/app/models/chouette/company.rb index 8ddd4c37d..7b96e875e 100644 --- a/app/models/chouette/company.rb +++ b/app/models/chouette/company.rb @@ -1,5 +1,8 @@ -class Chouette::Company < Chouette::TridentActiveRecord +class Chouette::Company < Chouette::ActiveRecord include CompanyRestrictions + include DefaultNetexAttributesSupport + include LineReferentialSupport + has_many :lines validates_format_of :registration_number, :with => %r{\A[0-9A-Za-z_-]+\Z}, :allow_nil => true, :allow_blank => true diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index 351cb0b0e..4a4115cc2 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -1,5 +1,5 @@ class Chouette::Line < Chouette::ActiveRecord - include DefaultAttributesSupport + include DefaultNetexAttributesSupport include LineRestrictions include LineReferentialSupport diff --git a/app/models/chouette/netex_object_id.rb b/app/models/chouette/netex_object_id.rb new file mode 100644 index 000000000..07d862992 --- /dev/null +++ b/app/models/chouette/netex_object_id.rb @@ -0,0 +1,40 @@ +class Chouette::NetexObjectId < String + + def valid? + parts.present? + end + alias_method :objectid?, :valid? + + @@format = /^([A-Za-z_]+):([0-9A-Za-z_]+):([A-Za-z]+):([0-9A-Za-z_-]+)$/ + cattr_reader :format + + def parts + match(format).try(:captures) + end + + def provider_id + parts.try(:first) + end + + def system_id + parts.try(:second) + end + + def object_type + parts.try(:third) + end + + def local_id + parts.try(:fourth) + end + + def self.create(provider_id, system_id, object_type, local_id) + new [provider_id, system_id, object_type, local_id].join(":") + end + + def self.new(string) + string ||= "" + self === string ? string : super + end + +end diff --git a/app/models/concerns/default_netex_attributes_support.rb b/app/models/concerns/default_netex_attributes_support.rb new file mode 100644 index 000000000..89007f2a2 --- /dev/null +++ b/app/models/concerns/default_netex_attributes_support.rb @@ -0,0 +1,81 @@ +module DefaultNetexAttributesSupport + extend ActiveSupport::Concern + + included do + before_validation :prepare_auto_columns + + validates_presence_of :objectid + validates_uniqueness_of :objectid + validates_numericality_of :object_version + validate :objectid_format_compliance + + before_validation :default_values, :on => :create + end + + module ClassMethods + def object_id_key + model_name + end + + def model_name + ActiveModel::Name.new self, Chouette, self.name.demodulize + end + end + + def objectid + Chouette::NetexObjectId.new read_attribute(:objectid) + end + + def prepare_auto_columns + if object_version.nil? + self.object_version = 1 + else + self.object_version += 1 + end + self.creation_time = Time.now + self.creator_id = 'chouette' + end + + def fix_uniq_objectid + base_objectid = objectid.rpartition(":").first + self.objectid = "#{base_objectid}:#{id}" + if !valid? + base_objectid="#{objectid}_" + cnt=1 + while !valid? + self.objectid = "#{base_objectid}#{cnt}" + cnt += 1 + end + end + + end + + def objectid_format_compliance + if !objectid.valid? + errors.add :objectid, I18n.t("activerecord.errors.models.trident.invalid_object_id", type: self.class.object_id_key) + end + end + + def uniq_objectid + # OPTIMIZEME + i = 0 + baseobjectid = objectid + while self.class.exists?(:objectid => objectid) + i += 1 + self.objectid = baseobjectid+"_"+i.to_s + end + end + + def default_values + self.object_version ||= 1 + end + + def timestamp_attributes_for_update #:nodoc: + [:creation_time] + end + + def timestamp_attributes_for_create #:nodoc: + [:creation_time] + end + +end |
