aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/chouette/journey_pattern.rb9
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb11
2 files changed, 14 insertions, 6 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb
index f3a7dfea7..c57d42e71 100644
--- a/app/models/chouette/journey_pattern.rb
+++ b/app/models/chouette/journey_pattern.rb
@@ -25,10 +25,8 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord
state.each do |item|
item.delete('errors')
jp = find_by(objectid: item['object_id']) || state_create_instance(route, item)
- if item['deletable']
- jp.destroy if jp.persisted?
- state.delete(item)
- next
+ if item['deletable'] && jp.persisted?
+ next if jp.destroy
end
# Update attributes and stop_points associations
@@ -42,8 +40,9 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord
raise ActiveRecord::Rollback
end
end
- # Remove new record flag
+ # clean
state.map {|item| item.delete('new_record')}
+ state.delete_if {|item| item['deletable']}
end
def self.state_permited_attributes item
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index d5dc5314e..19b5060d2 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -33,7 +33,7 @@ describe Chouette::JourneyPattern, :type => :model do
expect(journey_pattern.reload.stop_points.count).to eq(5)
end
- it 'should create now instance' do
+ it 'should create journey_pattern' do
new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
Chouette::JourneyPattern.state_create_instance route, new_state
expect(new_state['object_id']).to be_truthy
@@ -50,6 +50,15 @@ describe Chouette::JourneyPattern, :type => :model do
expect(collection).to be_empty
end
+ it 'should delete multiple journey_pattern' do
+ collection = 5.times.collect{journey_pattern_to_state(create(:journey_pattern, route: route))}
+ collection.map{|i| i['deletable'] = true}
+
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.from(5).to(0)
+ end
+
it 'should validate journey_pattern on update' do
journey_pattern.name = ''
collection = [state]