diff options
| author | Alban Peignier | 2018-03-31 21:54:55 +0200 | 
|---|---|---|
| committer | Alban Peignier | 2018-03-31 21:54:55 +0200 | 
| commit | f41a85fa419d3598c2edec2de1eef0f69c09e62c (patch) | |
| tree | 03880ef1d6f4d8692d1a5bf7753afd066ee8c9ed | |
| parent | fc53a6c6c1fb95ce46e13f501344d71a6d1e6de9 (diff) | |
| download | chouette-core-f41a85fa419d3598c2edec2de1eef0f69c09e62c.tar.bz2 | |
Add calendar imports to Import::Gtfs. Refs #6368
| -rw-r--r-- | app/models/concerns/application_days_support.rb | 8 | ||||
| -rw-r--r-- | app/models/import/gtfs.rb | 20 | ||||
| -rw-r--r-- | db/schema.rb | 1 | ||||
| -rw-r--r-- | spec/models/import/gtfs_spec.rb | 44 | 
4 files changed, 66 insertions, 7 deletions
| diff --git a/app/models/concerns/application_days_support.rb b/app/models/concerns/application_days_support.rb index 2d00b5847..6086d9580 100644 --- a/app/models/concerns/application_days_support.rb +++ b/app/models/concerns/application_days_support.rb @@ -10,8 +10,10 @@ module ApplicationDaysSupport    SUNDAY    = 256    EVERYDAY  = MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY | SATURDAY | SUNDAY +  ALL_DAYS = %w(monday tuesday wednesday thursday friday saturday sunday).freeze +    def display_day_types -    %w(monday tuesday wednesday thursday friday saturday sunday).select{ |d| self.send(d) }.map{ |d| self.human_attribute_name(d).first(2)}.join(', ') +    ALL_DAYS.select{ |d| self.send(d) }.map{ |d| self.human_attribute_name(d).first(2)}.join(', ')    end    def day_by_mask(flag) @@ -39,6 +41,10 @@ module ApplicationDaysSupport      def self.day_by_mask(int_day_types,flag)        int_day_types & flag == flag      end + +    def self.all_days +      ALL_DAYS +    end    end    def valid_days diff --git a/app/models/import/gtfs.rb b/app/models/import/gtfs.rb index 5b332315d..75ff45f8b 100644 --- a/app/models/import/gtfs.rb +++ b/app/models/import/gtfs.rb @@ -111,6 +111,8 @@ class Import::Gtfs < Import::Base        vehicle_journey.published_journey_name = trip.headsign.presence || trip.id        save vehicle_journey +      vehicle_journey.time_tables << referential.time_tables.find(time_tables_by_service_id[trip.service_id]) +        vehicle_journey_by_trip_id[trip.id] = vehicle_journey.id      end    end @@ -143,6 +145,24 @@ class Import::Gtfs < Import::Base      end    end +  def time_tables_by_service_id +    @time_tables_by_service_id ||= {} +  end + +  def import_calendars +    source.calendars.each do |calendar| +      time_table = referential.time_tables.build comment: "Calendar #{calendar.service_id}" +      Chouette::TimeTable.all_days.each do |day| +        time_table.send("#{day}=", calendar.send(day)) +      end +      time_table.periods.build period_start: calendar.start_date, period_end: calendar.end_date + +      save time_table + +      time_tables_by_service_id[calendar.service_id] = time_table.id +    end +  end +    def save(model)      unless model.save        Rails.logger.info "Can't save #{model.class.name} : #{model.errors.inspect}" diff --git a/db/schema.rb b/db/schema.rb index d90bf7b6c..77e35f449 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -18,7 +18,6 @@ ActiveRecord::Schema.define(version: 20180319043333) do    enable_extension "hstore"    enable_extension "postgis"    enable_extension "unaccent" -  enable_extension "objectid"    create_table "access_links", id: :bigserial, force: :cascade do |t|      t.integer  "access_point_id",                        limit: 8 diff --git a/spec/models/import/gtfs_spec.rb b/spec/models/import/gtfs_spec.rb index bdfc565d3..dcdef45e2 100644 --- a/spec/models/import/gtfs_spec.rb +++ b/spec/models/import/gtfs_spec.rb @@ -116,17 +116,28 @@ RSpec.describe Import::Gtfs do      it "should create a VehicleJourney for each trip" do        referential.switch +      import.import_calendars        import.import_routes        import.import_trips -      defined_attributes = [ -        :published_journey_name -      ] +      defined_attributes = ->(v) { +        [v.published_journey_name, v.time_tables.first&.comment] +      }        expected_attributes = [ -        "to Bullfrog", "to Airport", "Shuttle", "CITY1", "CITY2", "to Furnace Creek Resort", "to Bullfrog", "to Amargosa Valley", "to Airport", "to Amargosa Valley", "to Airport" +        ["to Bullfrog", "Calendar FULLW"], +        ["to Airport", "Calendar FULLW"], +        ["Shuttle", "Calendar FULLW"], +        ["CITY1", "Calendar FULLW"], +        ["CITY2", "Calendar FULLW"], +        ["to Furnace Creek Resort", "Calendar FULLW"], +        ["to Bullfrog", "Calendar FULLW"], +        ["to Amargosa Valley", "Calendar WE"], +        ["to Airport", "Calendar WE"], +        ["to Amargosa Valley", "Calendar WE"], +        ["to Airport", "Calendar WE"]        ] -      expect(referential.vehicle_journeys.pluck(*defined_attributes)).to eq(expected_attributes) +      expect(referential.vehicle_journeys.map(&defined_attributes)).to eq(expected_attributes)      end    end @@ -182,4 +193,27 @@ RSpec.describe Import::Gtfs do      end    end +  describe "#import_calendars" do +    let(:import) { create_import "google-sample-feed.zip" } + +    it "should create a Timetable for each calendar" do +      referential.switch + +      import.import_calendars + +      def d(value) +        Date.parse(value) +      end + +      defined_attributes = ->(t) { +        [t.comment, t.valid_days, t.periods.first.period_start, t.periods.first.period_end] +      } +      expected_attributes = [ +        ["Calendar FULLW", [1, 2, 3, 4, 5, 6, 7], d("Mon, 01 Jan 2007"), d("Fri, 31 Dec 2010")], +        ["Calendar WE", [6, 7], d("Mon, 01 Jan 2007"), d("Fri, 31 Dec 2010")] +      ] +      expect(referential.time_tables.map(&defined_attributes)).to eq(expected_attributes) +    end +  end +  end | 
