aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/chouette/route.rb9
-rw-r--r--app/views/referentials/_period_fields.html.slim4
-rw-r--r--app/workers/route_way_cost_worker.rb11
-rw-r--r--lib/tasks/checks.rake19
-rw-r--r--spec/models/chouette/route/route_base_spec.rb8
5 files changed, 37 insertions, 14 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 14bfa47b6..c5e909d7e 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -7,11 +7,12 @@ module Chouette
include ObjectidSupport
extend Enumerize
- if Rails.env.development?
+ if ENV["CHOUETTE_ROUTE_POSITION_CHECK"] == "true" || !Rails.env.production?
after_commit do
positions = stop_points.pluck(:position)
+ Rails.logger.debug "Check positions in Route #{id} : #{positions.inspect}"
if positions.size != positions.uniq.size
- raise "DUPLICATED stop_points positions: #{positions}"
+ raise "DUPLICATED stop_points positions in Route #{id} : #{positions.inspect}"
end
end
end
@@ -77,7 +78,9 @@ module Chouette
validates_presence_of :published_name
validates_presence_of :line
validates :wayback, inclusion: { in: self.wayback.values }
- after_save :calculate_costs!, if: ->() { TomTom.enabled? }
+ after_commit :calculate_costs!,
+ on: [:create, :update],
+ if: ->() { TomTom.enabled? }
def duplicate opposite=false
overrides = {
diff --git a/app/views/referentials/_period_fields.html.slim b/app/views/referentials/_period_fields.html.slim
index 4d2372f7b..b0038c6b3 100644
--- a/app/views/referentials/_period_fields.html.slim
+++ b/app/views/referentials/_period_fields.html.slim
@@ -8,8 +8,8 @@
.wrapper
div
- = f.input :begin, as: :date, label: false, wrapper_html: { class: 'date smart_date' }
+ = f.input :begin, as: :date, label: false, start_year: Date.today.year - 15, end_year: Date.today.year + 15, wrapper_html: { class: 'date smart_date' }
div
- = f.input :end, as: :date, label: false, wrapper_html: { class: 'date smart_date' }
+ = f.input :end, as: :date, label: false, start_year: Date.today.year - 15, end_year: Date.today.year + 15, wrapper_html: { class: 'date smart_date' }
div
= link_to_remove_association '', f, class: 'fa fa-trash', data: { confirm: t('are_you_sure')}, title: t('actions.delete')
diff --git a/app/workers/route_way_cost_worker.rb b/app/workers/route_way_cost_worker.rb
index d6bfed592..b62416c3d 100644
--- a/app/workers/route_way_cost_worker.rb
+++ b/app/workers/route_way_cost_worker.rb
@@ -7,10 +7,11 @@ class RouteWayCostWorker
# Prevent recursive worker spawning since this call updates the
# `costs` field of the route.
- Chouette::Route.skip_callback(:save, :after, :calculate_costs!)
-
- RouteWayCostCalculator.new(route).calculate!
-
- Chouette::Route.set_callback(:save, :after, :calculate_costs!)
+ begin
+ Chouette::Route.skip_callback(:commit, :after, :calculate_costs!)
+ RouteWayCostCalculator.new(route).calculate!
+ ensure
+ Chouette::Route.set_callback(:commit, :after, :calculate_costs!)
+ end
end
end
diff --git a/lib/tasks/checks.rake b/lib/tasks/checks.rake
new file mode 100644
index 000000000..23c638964
--- /dev/null
+++ b/lib/tasks/checks.rake
@@ -0,0 +1,19 @@
+namespace :check do
+ desc "Check routes stop_points positions are valid"
+ task routes_integrity: :environment do
+ errors = []
+ max = Referential.pluck(:name).map(&:size).max + 20
+ Referential.find_each do |r|
+ r.switch do
+ Chouette::Route.find_each do |route|
+ positions = route.stop_points.pluck(:position)
+ if positions.size != positions.uniq.size
+ lb = "Referential: #{r.id}/#{r.name}"
+ errors << "#{lb + " "*(max-lb.size)} -> Wrong positions in Route #{route.id} : #{positions.inspect} "
+ end
+ end
+ end
+ end
+ puts errors.join("\n")
+ end
+end
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb
index 3d4a87791..43ff28c40 100644
--- a/spec/models/chouette/route/route_base_spec.rb
+++ b/spec/models/chouette/route/route_base_spec.rb
@@ -62,17 +62,17 @@ RSpec.describe Chouette::Route, :type => :model do
end
context "callbacks" do
- it "calls #calculate_costs! after_save when TomTom is enabled" do
+ it "calls #calculate_costs! after_commit when TomTom is enabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(true)
- route = create(:route)
+ route = build(:route)
expect(route).to receive(:calculate_costs!)
route.save
end
- it "doesn't call #calculate_costs! after_save if TomTom is disabled" do
+ it "doesn't call #calculate_costs! after_commit if TomTom is disabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(false)
- route = create(:route)
+ route = build(:route)
expect(route).not_to receive(:calculate_costs!)
route.save