diff options
| author | cedricnjanga | 2017-11-17 18:07:29 +0100 |
|---|---|---|
| committer | cedricnjanga | 2017-11-17 18:08:57 +0100 |
| commit | 70313954ea0431f02826aa5aaafefedea3c8fb00 (patch) | |
| tree | 034474dda8763c6cfe6475be3a01b676026583d1 /app | |
| parent | 0f7da86d0a54d84f9f9f5683b2bb95a106158373 (diff) | |
| download | chouette-core-70313954ea0431f02826aa5aaafefedea3c8fb00.tar.bz2 | |
Add some changes to the objectid construction
Diffstat (limited to 'app')
21 files changed, 51 insertions, 159 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 4503bef8b..fb30ab69e 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -25,7 +25,7 @@ module Chouette def local_id - "IBOO-#{self.referential.id}-#{self.try(:route).try(:line).try(:objectid).try(:local_id)}-#{self.id}" + "IBOO-#{self.referential.id}-#{self.route.line.get_objectid.local_id}-#{self.id}" end def checksum_attributes @@ -48,7 +48,7 @@ module Chouette if state.any? {|item| item['errors']} state.map {|item| item.delete('object_id') if item['new_record']} - raise ActiveRecord::Rollback + raise ::ActiveRecord::Rollback end end diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index e08eaf4d0..cae4e7c3f 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -71,7 +71,7 @@ class Chouette::Line < Chouette::ActiveRecord end def display_name - [objectid.local_id, number, name, company.try(:name)].compact.join(' - ') + [self.get_objectid.local_id, number, name, company.try(:name)].compact.join(' - ') end def companies diff --git a/app/models/chouette/objectid/netex.rb b/app/models/chouette/objectid/netex.rb index 254ce6c6e..0013de7c8 100644 --- a/app/models/chouette/objectid/netex.rb +++ b/app/models/chouette/objectid/netex.rb @@ -5,12 +5,13 @@ module Chouette attr_accessor :provider_id, :object_type, :local_id, :creation_id validates_presence_of :provider_id, :object_type, :local_id, :creation_id + validate :must_respect_format def initialize(**attributes) - @provider_id ||= 'chouette' + @provider_id ||= (attributes[:provider_id] ||= 'chouette') @object_type = attributes[:object_type] @local_id = attributes[:local_id] - @creation_id ||= 'LOC' + @creation_id = (attributes[:creation_id] ||= 'LOC') end @@format = /^([A-Za-z_-]+):([A-Za-z]+):([0-9A-Za-z_-]+):([A-Za-z]+)$/ @@ -20,12 +21,8 @@ module Chouette "#{self.provider_id}:#{self.object_type}:#{self.local_id}:#{self.creation_id}" end - def parts - self.to_s.match(format).try(:captures) - end - - def valid? - parts.present? + def must_respect_format + self.to_s.match(format) end def short_id diff --git a/app/models/chouette/objectid/stif_netex.rb b/app/models/chouette/objectid/stif_netex.rb index e9d40a00f..4894daac7 100644 --- a/app/models/chouette/objectid/stif_netex.rb +++ b/app/models/chouette/objectid/stif_netex.rb @@ -3,7 +3,7 @@ module Chouette class StifNetex < Chouette::Objectid::Netex def initialize(**attributes) - @provider_id = 'stif' + @provider_id = (attributes[:provider_id] ||= 'stif') super end diff --git a/app/models/chouette/objectid_formater/netex.rb b/app/models/chouette/objectid_formater/netex.rb deleted file mode 100644 index 0736b6ff9..000000000 --- a/app/models/chouette/objectid_formater/netex.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Chouette - module ObjectidFormater - class Netex - def before_validation(model) - model.objectid ||= Chouette::Objectid::Netex.new(local_id: SecureRandom.uuid, object_type: model.class.name.gsub(/Chouette::/,'')).to_s - end - - def after_commit(model) - # unused method in this context - end - - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::Netex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil - end - end - end -end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_codifligne.rb b/app/models/chouette/objectid_formater/stif_codifligne.rb deleted file mode 100644 index 53fd28c82..000000000 --- a/app/models/chouette/objectid_formater/stif_codifligne.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Chouette - module ObjectidFormater - class StifCodifligne - def before_validation(model) - # unused method in this context - end - - def after_commit(model) - # unused method in this context - end - - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifCodifligne.new(provider_id: parts[0], sync_id: parts[1], object_type: parts[2], local_id: parts[3]).to_s rescue nil - end - end - end -end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_netex.rb b/app/models/chouette/objectid_formater/stif_netex.rb deleted file mode 100644 index 88995dd05..000000000 --- a/app/models/chouette/objectid_formater/stif_netex.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Chouette - module ObjectidFormater - class StifNetex - def before_validation(model) - model.objectid ||= "__pending_id__#{rand(50)+ rand(50)}" - end - - def after_commit(model) - if model.objectid.include? ':__pending_id__' - model.objectid = Chouette::Objectid::StifNetex.new(provider_id: "stif", object_type: model.class.name.gsub(/Chouette::/,''), local_id: model.local_id).to_s - end - end - - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifNetex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil - end - end - end -end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formater/stif_reflex.rb b/app/models/chouette/objectid_formater/stif_reflex.rb deleted file mode 100644 index e6c6a6a70..000000000 --- a/app/models/chouette/objectid_formater/stif_reflex.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Chouette - module ObjectidFormater - class StifReflex - def before_validation(model) - # unused method in this context - end - - def after_commit(model) - # unused method in this context - end - - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifReflex.new(country_code: parts[0], zip_code: parts[1], object_type: parts[2], local_id: parts[3], provider_id: parts[4]).to_s rescue nil - end - end - end -end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formatter/netex.rb b/app/models/chouette/objectid_formatter/netex.rb index 13bcc195e..5beeae7ea 100644 --- a/app/models/chouette/objectid_formatter/netex.rb +++ b/app/models/chouette/objectid_formatter/netex.rb @@ -2,17 +2,17 @@ module Chouette module ObjectidFormatter class Netex def before_validation(model) - model.objectid ||= Chouette::Objectid::Netex.new(local_id: SecureRandom.uuid, object_type: model.class.name.gsub(/Chouette::/,'')).to_s + model.update_attribute(:objectid, Chouette::Objectid::Netex.new(local_id: SecureRandom.uuid, object_type: model.class.name.gsub(/Chouette::/,'')).to_s) unless model.objectid end - def after_commit(model) + def after_create(model) # unused method in this context end - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::Netex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil + def get_objectid(definition) + parts = definition.try(:split, ":") + Chouette::Objectid::Netex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]) rescue nil end end end -end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formatter/stif_codifligne.rb b/app/models/chouette/objectid_formatter/stif_codifligne.rb index b033b9b7d..8121bbf8c 100644 --- a/app/models/chouette/objectid_formatter/stif_codifligne.rb +++ b/app/models/chouette/objectid_formatter/stif_codifligne.rb @@ -5,14 +5,14 @@ module Chouette # unused method in this context end - def after_commit(model) + def after_create(model) # unused method in this context end - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifCodifligne.new(provider_id: parts[0], sync_id: parts[1], object_type: parts[2], local_id: parts[3]).to_s rescue nil + def get_objectid(definition) + parts = definition.try(:split, ":") + Chouette::Objectid::StifCodifligne.new(provider_id: parts[0], sync_id: parts[1], object_type: parts[2], local_id: parts[3]) rescue nil end end end -end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formatter/stif_netex.rb b/app/models/chouette/objectid_formatter/stif_netex.rb index 004dd2ec9..0531221e7 100644 --- a/app/models/chouette/objectid_formatter/stif_netex.rb +++ b/app/models/chouette/objectid_formatter/stif_netex.rb @@ -1,20 +1,20 @@ module Chouette module ObjectidFormatter class StifNetex - def before_validation(model) - model.objectid ||= "__pending_id__#{rand(50)+ rand(50)}" + def before_validation(model) + model.attributes = {objectid: "__pending_id__#{rand(50)+ rand(50)}"} unless model.read_attribute(:objectid) end - def after_commit(model) - if model.objectid.include? ':__pending_id__' - model.objectid = Chouette::Objectid::StifNetex.new(provider_id: "stif", object_type: model.class.name.gsub(/Chouette::/,''), local_id: model.local_id).to_s + def after_create(model) + if model.read_attribute(:objectid).include? '__pending_id__' + model.update_attributes(objectid: Chouette::Objectid::StifNetex.new(provider_id: "stif", object_type: model.class.name.gsub(/Chouette::/,''), local_id: model.local_id).to_s) end end - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifNetex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]).to_s rescue nil + def get_objectid(definition) + parts = definition.try(:split, ":") + Chouette::Objectid::StifNetex.new(provider_id: parts[0], object_type: parts[1], local_id: parts[2], creation_id: parts[3]) rescue nil end end end -end +end
\ No newline at end of file diff --git a/app/models/chouette/objectid_formatter/stif_reflex.rb b/app/models/chouette/objectid_formatter/stif_reflex.rb index c9e19d87b..7a45b7f6a 100644 --- a/app/models/chouette/objectid_formatter/stif_reflex.rb +++ b/app/models/chouette/objectid_formatter/stif_reflex.rb @@ -5,14 +5,14 @@ module Chouette # unused method in this context end - def after_commit(model) + def after_create(model) # unused method in this context end - def parse_objectid(definition) - parts = definition.split(":") - Chouette::Objectid::StifReflex.new(country_code: parts[0], zip_code: parts[1], object_type: parts[2], local_id: parts[3], provider_id: parts[4]).to_s rescue nil + def get_objectid(definition) + parts = definition.try(:split, ":") + Chouette::Objectid::StifReflex.new(country_code: parts[0], zip_code: parts[1], object_type: parts[2], local_id: parts[3], provider_id: parts[4]) rescue nil end end end -end +end
\ No newline at end of file diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 853ecee85..451510b79 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -100,7 +100,7 @@ class Chouette::Route < Chouette::TridentActiveRecord end def local_id - "IBOO-#{self.referential.id}-#{self.line.objectid.local_id}-#{self.id}" + "IBOO-#{self.referential.id}-#{self.line.get_objectid.local_id}-#{self.id}" end def geometry_presenter diff --git a/app/models/chouette/stop_point.rb b/app/models/chouette/stop_point.rb index 2c7bb3355..593de83a3 100644 --- a/app/models/chouette/stop_point.rb +++ b/app/models/chouette/stop_point.rb @@ -49,6 +49,10 @@ module Chouette self.class.create!(atts_for_create) end + def local_id + "IBOO-#{self.referential.id}-#{self.route.line.get_objectid.local_id}-#{self.route.id}-#{self.id}" + end + def self.area_candidates Chouette::StopArea.where(:area_type => ['Quay', 'BoardingPosition']) end diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 1cb2d7045..7b51b94ea 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -58,7 +58,7 @@ module Chouette end def local_id - "IBOO-#{self.referential.id}-#{self.route.line.objectid.local_id}-#{self.id}" + "IBOO-#{self.referential.id}-#{self.route.line.get_objectid.local_id}-#{self.id}" end def checksum_attributes diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index a4a4a02c8..271139830 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -41,7 +41,7 @@ module Chouette :arrival_day_offset, I18n.t( 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', - short_id: vehicle_journey.objectid.short_id, + short_id: vehicle_journey.get_objectid.short_id, max: DAY_OFFSET_MAX + 1 ) ) @@ -52,7 +52,7 @@ module Chouette :departure_day_offset, I18n.t( 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', - short_id: vehicle_journey.objectid.short_id, + short_id: vehicle_journey.get_objectid.short_id, max: DAY_OFFSET_MAX + 1 ) ) diff --git a/app/models/concerns/objectid_formater_support.rb b/app/models/concerns/objectid_formater_support.rb deleted file mode 100644 index 1064b156f..000000000 --- a/app/models/concerns/objectid_formater_support.rb +++ /dev/null @@ -1,15 +0,0 @@ -module ObjectidFormaterSupport - extend ActiveSupport::Concern - - included do - validates_presence_of :objectid_formater_class - - def objectid_formater - objectid_formater_class.new - end - - def objectid_formater_class - "Chouette::ObjectidFormater::#{read_attribute(:objectid_format).camelcase}".constantize if read_attribute(:objectid_format) - end - end -end
\ No newline at end of file diff --git a/app/models/concerns/objectid_support.rb b/app/models/concerns/objectid_support.rb index 1c8bdf751..9e4dc34c1 100644 --- a/app/models/concerns/objectid_support.rb +++ b/app/models/concerns/objectid_support.rb @@ -3,20 +3,24 @@ module ObjectidSupport included do before_validation :before_validation_objectid - after_commit :after_commit_objectid + after_create :after_create_objectid validates_presence_of :objectid_format, :objectid - validates_uniqueness_of :objectid + validates_uniqueness_of :objectid, skip_validation: Proc.new {|model| model.objectid == nil} def before_validation_objectid self.referential.objectid_formater.before_validation self end - def after_commit_objectid - self.referential.objectid_formater.after_commit self + def after_create_objectid + self.referential.objectid_formater.after_create self + end + + def get_objectid + self.referential.objectid_formater.get_objectid read_attribute(:objectid) if objectid_format && read_attribute(:objectid) end def objectid - self.referential.objectid_formater.parse_objectid read_attribute(:objectid) if objectid_format && read_attribute(:objectid) + get_objectid.try(:to_s) end def objectid_format diff --git a/app/models/line_referential.rb b/app/models/line_referential.rb index 0d7125a57..d8cf74bda 100644 --- a/app/models/line_referential.rb +++ b/app/models/line_referential.rb @@ -1,9 +1,5 @@ class LineReferential < ActiveRecord::Base -<<<<<<< HEAD include ObjectidFormatterSupport -======= - include ObjectidFormaterSupport ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 extend StifTransportModeEnumerations extend Enumerize @@ -15,11 +11,7 @@ class LineReferential < ActiveRecord::Base has_many :networks, class_name: 'Chouette::Network' has_many :line_referential_syncs, -> { order created_at: :desc } has_many :workbenches -<<<<<<< HEAD enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne), default: 'netex' -======= - enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne) ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 def add_member(organisation, options = {}) attributes = options.merge organisation: organisation diff --git a/app/models/referential.rb b/app/models/referential.rb index 8e64ef57d..8003d342f 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -1,10 +1,6 @@ class Referential < ActiveRecord::Base include DataFormatEnumerations -<<<<<<< HEAD include ObjectidFormatterSupport -======= - include ObjectidFormaterSupport ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 extend Enumerize validates_presence_of :name @@ -60,11 +56,7 @@ class Referential < ActiveRecord::Base belongs_to :referential_suite -<<<<<<< HEAD enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne), default: 'netex' -======= - enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne) ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 scope :ready, -> { where(ready: true) } scope :in_periode, ->(periode) { where(id: referential_ids_in_periode(periode)) } diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb index 5709a43a9..159ee07b3 100644 --- a/app/models/stop_area_referential.rb +++ b/app/models/stop_area_referential.rb @@ -1,21 +1,13 @@ class StopAreaReferential < ActiveRecord::Base extend Enumerize -<<<<<<< HEAD include ObjectidFormatterSupport -======= - include ObjectidFormaterSupport ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 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 -<<<<<<< HEAD enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne), default: 'netex' -======= - enumerize :objectid_format, in: %w(netex stif_netex stif_reflex stif_codifligne) ->>>>>>> Create objectid format and integrate it in models. Work in progress. Refs #4941 validates_presence_of :objectid_format def add_member(organisation, options = {}) |
