aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-03-05 12:00:29 +0100
committerZog2018-03-05 12:00:29 +0100
commita412f915c885f3bf2962d0b786ff864f1b0e120e (patch)
tree3352d379f3d5c963f4382c374c6e78eea33cb2e1
parentea3de6035cdf9fbbcd92f51c90e5a2c5c3400cb3 (diff)
downloadchouette-core-a412f915c885f3bf2962d0b786ff864f1b0e120e.tar.bz2
Refs #6068; Add some helpers in the models
-rw-r--r--app/models/chouette/journey_pattern.rb13
-rw-r--r--app/models/chouette/route.rb5
-rw-r--r--app/models/chouette/stop_area.rb5
-rw-r--r--app/models/simple_exporter.rb2
-rw-r--r--lib/tasks/exports.rake38
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb17
6 files changed, 76 insertions, 4 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb
index 830e985d9..10c8b5439 100644
--- a/app/models/chouette/journey_pattern.rb
+++ b/app/models/chouette/journey_pattern.rb
@@ -171,6 +171,19 @@ module Chouette
full
end
+ def distance_to stop
+ val = 0
+ i = 0
+ _end = stop_points.first
+ while _end != stop
+ i += 1
+ _start = _end
+ _end = stop_points[i]
+ val += costs_between(_start, _end)[:distance]
+ end
+ val
+ end
+
def set_distances distances
raise "inconsistent data: #{distances.count} values for #{stop_points.count} stops" unless distances.count == stop_points.count
prev = distances[0].to_i
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 3729deb7d..445346745 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -191,6 +191,11 @@ module Chouette
journey_pattern
end
+ def generated_name
+ return "" unless stop_points.size > 1
+ [stop_points.first, stop_points.last].map(&:name).join ' > '
+ end
+
protected
def self.vehicle_journeys_timeless(stop_point_id)
diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb
index 7170dd217..f58f97eee 100644
--- a/app/models/chouette/stop_area.rb
+++ b/app/models/chouette/stop_area.rb
@@ -390,9 +390,12 @@ module Chouette
ActiveSupport::TimeZone[time_zone]&.utc_offset
end
- def country_name
+ def country
return unless country_code
country = ISO3166::Country[country_code]
+ end
+
+ def country_name
return unless country
country.translations[I18n.locale.to_s] || country.name
end
diff --git a/app/models/simple_exporter.rb b/app/models/simple_exporter.rb
index 9c12a4e02..fe0aa05b5 100644
--- a/app/models/simple_exporter.rb
+++ b/app/models/simple_exporter.rb
@@ -72,7 +72,7 @@ class SimpleExporter < SimpleInterface
def map_item_to_rows item
return [item] unless configuration.item_to_rows_mapping
- configuration.item_to_rows_mapping.call(item).map {|row| CustomRow.new row }
+ configuration.item_to_rows_mapping.call(item).map {|row| row.is_a?(ActiveRecord::Base) ? row : CustomRow.new(row) }
end
def handle_item item
diff --git a/lib/tasks/exports.rake b/lib/tasks/exports.rake
index 6ff73dac0..8688611e8 100644
--- a/lib/tasks/exports.rake
+++ b/lib/tasks/exports.rake
@@ -43,7 +43,7 @@ namespace :export do
desc "export a complete offer from the given referential in the given X next days"
task :full_offer, [:referential_id, :configuration_name, :timelapse, :output_dir, :logs_output_dir] => :environment do |t, args|
referential = Referential.find args[:referential_id]
- args.with_defaults(output_dir: referential.name, logs_output_dir: "./log/exporters/", timelapse: 90)
+ args.with_defaults(output_dir: "#{referential.name.parameterize}_#{Time.now.strftime "%y%m%d%H%M"}", logs_output_dir: "./log/exporters/", timelapse: 90)
referential.switch
@@ -51,13 +51,47 @@ namespace :export do
if journeys.count == 0
puts "No maching journeys were found".red
else
-
exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_journeys", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_journeys.csv"
exporter.configure do |config|
config.collection = journeys
end
SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
+ return unless exporter.status == :success
+
+ exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_schedules", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_schedules.csv"
+ exporter.configure do |config|
+ config.collection = journeys
+ end
+
+ SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
+ return unless exporter.status == :success
+
+ exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_routes", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_routes.csv"
+ exporter.configure do |config|
+ config.collection = Chouette::JourneyPattern.where(id: journeys.pluck(:journey_pattern_id).uniq)
+ end
+
+ SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
+ return unless exporter.status == :success
+
+ exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_stops", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_stops.csv"
+ exporter.configure do |config|
+ config.collection = Chouette::StopArea.where(id: journeys.joins(:stop_points).pluck(:"stop_points.stop_area_id").uniq).order('parent_id ASC NULLS FIRST')
+ end
+
+ SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
+ return unless exporter.status == :success
+
+ exporter = SimpleExporter.create configuration_name: "#{args[:configuration_name]}_companies", filepath: "#{args[:output_dir]}/#{args[:configuration_name]}_companies.csv"
+ ids = journeys.pluck :company_id
+ ids += journeys.joins(route: :line).pluck :"lines.company_id"
+
+ exporter.configure do |config|
+ config.collection = Chouette::Company.where(id: ids.uniq).order('name')
+ end
+
+ SimpleInterfacesHelper.run_interface_controlling_interruption exporter, :export, args
end
end
end
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index 7c767e4d1..dac45d6b5 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -71,6 +71,23 @@ describe Chouette::JourneyPattern, :type => :model do
end
end
+ describe "distance_to" do
+ let(:journey_pattern) { create :journey_pattern }
+ before do
+ journey_pattern.costs = generate_journey_pattern_costs(10, 10)
+ end
+ subject{ journey_pattern.distance_to stop}
+ context "for the first stop" do
+ let(:stop){ journey_pattern.stop_points.first }
+ it { should eq 0 }
+ end
+
+ context "for the last stop" do
+ let(:stop){ journey_pattern.stop_points.last }
+ it { should eq 40 }
+ end
+ end
+
describe "set_distances" do
let(:journey_pattern) { create :journey_pattern }
let(:distances){ [] }