Skip to content

Commit

Permalink
Merged in CHOUETTE-2289-fix-missing-referent-error-in-netex-import (p…
Browse files Browse the repository at this point in the history
…ull request #1066)

CHOUETTE-2387 Fix NeTEx import of Stop Area referents

Approved-by: Alban Peignier
Approved-by: Luc Donnet
  • Loading branch information
Hai Hieu Vu authored and Luc Donnet committed Oct 7, 2022
2 parents 31e5528 + 101f74e commit 993e877
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
8 changes: 3 additions & 5 deletions app/lib/chouette/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ def delete(resource_identifiers)
deleter.delete resource_identifiers
end

# To be overrided
def after_synchronisation; end

protected

def delete_after_update_or_create
deleter.delete_from(updater)
end

# To be overrided
def after_synchronisation

end

def updater_class
@updater_class ||=
begin
Expand Down
4 changes: 4 additions & 0 deletions app/lib/chouette/sync/referential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def update_or_create
end
end

def after_synchronisation
syncs.each(&:after_synchronisation)
end

def event_handler=(event_handler)
syncs.each do |sync|
sync.event_handler = event_handler
Expand Down
34 changes: 28 additions & 6 deletions app/lib/chouette/sync/stop_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def delete_after_update_or_create
def after_synchronisation
[stop_place_updater, quay_updater].each do |updater|
updater.update_pending_parents
updater.update_pending_referents
end
end

Expand Down Expand Up @@ -75,7 +76,7 @@ def stop_area_city_name
postal_address&.town
end

def stop_area_is_referent
def stop_area_is_particular
derived_from_object_ref.present?
end

Expand All @@ -93,12 +94,16 @@ def stop_area_parent_id
end
end

def stop_area_is_referent
stop_area_is_particular ? false : nil
end

def stop_area_referent_id
return unless stop_area_is_referent
return unless stop_area_is_particular

@stop_area_referent_id ||= resolve(:stop_area, derived_from_object_ref).tap do |referent_id|
pending_referent id, derived_from_object_ref if referent_id.nil?
end
pending_referent id, derived_from_object_ref

nil
end

def model_attributes
Expand All @@ -113,6 +118,7 @@ def model_attributes
latitude: latitude,
longitude: longitude,
is_referent: stop_area_is_referent,
referent_id: stop_area_referent_id,
parent_id: stop_area_parent_id,
status: :confirmed,
import_xml: raw_xml
Expand Down Expand Up @@ -142,7 +148,7 @@ def update_pending_referents
end

def pending_referent_resolver
@pending_referent_resolver ||= PendingResolver.new(self, :referent)
@pending_referent_resolver ||= PendingReferentResolver.new(self)
end
def pending_parent_resolver
@pending_parent_resolver ||= PendingResolver.new(self, :parent)
Expand Down Expand Up @@ -183,6 +189,22 @@ def update

end

# Resolve Stop Area referents
class PendingReferentResolver < PendingResolver
def initialize(updater)
super updater, :referent
end

def update
pending_referents.update_all is_referent: true
super
end

def pending_referents
scope.where(model_id_attribute => pendings.values)
end
end

end

class Deleter < Chouette::Sync::Deleter
Expand Down
1 change: 1 addition & 0 deletions app/models/import/netex_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def import!
sync.default_provider = default_provider

sync.update_or_create
sync.after_synchronisation
end

import.resources.each do |resource|
Expand Down
42 changes: 42 additions & 0 deletions spec/lib/chouette/sync/stop_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,48 @@ def stop_area(registration_number)
expect(useless_stop_area.reload).to be_deactivated
end

describe '#derived_from_object_ref' do
let(:xml) do
%(
<stopPlaces>
<StopPlace dataSourceRef="FR1-ARRET_AUTO" id="particular" derivedFromObjectRef="referent">
<Name>Particular Sample</Name>
</StopPlace>
<StopPlace dataSourceRef="FR1-ARRET_AUTO" id="referent">
<Name>Referent Sample</Name>
</StopPlace>
</stopPlaces>
)
end

let(:referent_stop_area) do
stop_area('referent')
end

let(:particular_stop_area) do
stop_area('particular')
end

before { sync.synchronize }

it 'should create referent stop area' do
expected_attributes = {
name: 'Referent Sample',
is_referent: true
}

expect(referent_stop_area.reload).to have_attributes(expected_attributes)
end

it 'should create particular stop area' do
expected_attributes = {
name: 'Particular Sample',
referent: referent_stop_area
}

expect(particular_stop_area.reload).to have_attributes(expected_attributes)
end
end
end

end

0 comments on commit 993e877

Please sign in to comment.