diff options
| -rw-r--r-- | app/models/chouette/line.rb | 3 | ||||
| -rw-r--r-- | app/models/simple_importer.rb | 15 | ||||
| -rw-r--r-- | lib/tasks/imports.rake | 28 | ||||
| -rw-r--r-- | spec/models/simple_importer_spec.rb | 31 | 
4 files changed, 61 insertions, 16 deletions
| diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index ba2e2755d..874353752 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -41,6 +41,7 @@ module Chouette      validates_presence_of :name +      scope :by_text, ->(text) { where('lower(name) LIKE :t or lower(published_name) LIKE :t or lower(objectid) LIKE :t or lower(comment) LIKE :t or lower(number) LIKE :t',        t: "%#{text.downcase}%") } @@ -48,6 +49,8 @@ module Chouette        [:published_name, :number, :comment, :url, :color, :text_color, :stable_id]      end +    def local_id; registration_number end +      def geometry_presenter        Chouette::Geometry::LinePresenter.new self      end diff --git a/app/models/simple_importer.rb b/app/models/simple_importer.rb index 41ce379db..bf6f3b406 100644 --- a/app/models/simple_importer.rb +++ b/app/models/simple_importer.rb @@ -55,7 +55,7 @@ class SimpleImporter < ActiveRecord::Base      statuses = ""      log "#{"%#{padding}d" % 0}/#{number_of_lines}", clear: true      ActiveRecord::Base.transaction do -      self.configuration.before_actions(:all).each &:call +      self.configuration.before_actions(:all).each do |action| action.call self end        CSV.foreach(filepath, self.configuration.csv_options) do |row|          status = handle_row row, status @@ -63,7 +63,7 @@ class SimpleImporter < ActiveRecord::Base          fail_with_error ->(){ @current_record.errors.messages } do            new_record = @current_record.new_record?            self.configuration.before_actions(:each_save).each do |action| -            action.call @current_record +            action.call self, @current_record            end            ### This could fail if the record has a mandatory relation which is not yet resolved            ### TODO: do not attempt to save if the current record if waiting for resolution @@ -162,7 +162,7 @@ class SimpleImporter < ActiveRecord::Base    end    class Configuration -    attr_accessor :model, :headers, :separator, :key, :context +    attr_accessor :model, :headers, :separator, :key, :context, :encoding      attr_reader :columns      def initialize import_name, opts={} @@ -170,8 +170,10 @@ class SimpleImporter < ActiveRecord::Base        @key = opts[:key] || "id"        @headers = opts.has_key?(:headers) ? opts[:headers] : true        @separator = opts[:separator] || "," +      @encoding = opts[:encoding]        @columns = opts[:columns] || []        @model = opts[:model] +      @custom_handler = opts[:custom_handler]      end      def duplicate @@ -183,8 +185,10 @@ class SimpleImporter < ActiveRecord::Base          key: @key,          headers: @headers,          separator: @separator, +        encoding: @encoding,          columns: @columns.map(&:duplicate), -        model: model +        model: model, +        custom_handler: @custom_handler        }      end @@ -204,7 +208,8 @@ class SimpleImporter < ActiveRecord::Base      def csv_options        {          headers: self.headers, -        col_sep: self.separator +        col_sep: self.separator, +        encoding: self.encoding        }      end diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake index e1043106e..9c6b1fbcd 100644 --- a/lib/tasks/imports.rake +++ b/lib/tasks/imports.rake @@ -29,4 +29,32 @@ namespace :import do      importer.import(verbose: true)      puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m"    end + +  desc "import the given file with the corresponding importer in the given Referential and StopAreaReferential" +  task :import_in_referential_and_stop_area_referential, [:referential_id, :stop_area_referential_id, :configuration_name, :filepath] => :environment do |t, args| +    referential = Referential.find args[:referential_id] +    referential.switch +    stop_area_referential = StopAreaReferential.find args[:stop_area_referential_id] +    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] +    importer.configure do |config| +      config.add_value :stop_area_referential, referential +      config.context = {stop_area_referential: stop_area_referential} +    end +    puts "\e[33m***\e[0m Start importing" +    importer.import(verbose: true) +    puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +  end + +  desc "import the given file with the corresponding importer in the given LineReferential" +  task :import_lines_in_referential, [:referential_id, :configuration_name, :filepath] => :environment do |t, args| +    referential = LineReferential.find args[:referential_id] +    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] +    importer.configure do |config| +      config.add_value :line_referential, referential +      config.context = {line_referential: referential} +    end +    puts "\e[33m***\e[0m Start importing" +    importer.import(verbose: true) +    puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +  end  end diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb index c8bcf285d..e324399c7 100644 --- a/spec/models/simple_importer_spec.rb +++ b/spec/models/simple_importer_spec.rb @@ -128,11 +128,11 @@ RSpec.describe SimpleImporter do        let!(:missing){ create :stop_area, name: "Another", stop_area_referential: stop_area_referential }        before(:each){          importer.configure do |config| -          config.before do +          config.before do |importer|              stop_area_referential.stop_areas.each &:deactivate!            end -          config.before(:each_save) do |stop_area| +          config.before(:each_save) do |importer, stop_area|              stop_area.activate!            end          end @@ -179,11 +179,11 @@ RSpec.describe SimpleImporter do            config.add_value  :stop_area_referential_id, stop_area_referential.id            config.add_value  :long_lat_type, "WGS84"            config.add_value  :kind, :commercial -          config.before do +          config.before do |importer|              stop_area_referential.stop_areas.each &:deactivate!            end -          config.before(:each_save) do |stop_area| +          config.before(:each_save) do |importer, stop_area|              stop_area.activate            end          end @@ -221,20 +221,24 @@ RSpec.describe SimpleImporter do        let(:filename){ "stop_points_full.csv" }        before(:each) do -        create :route, number: 1136, stop_points_count: 0 -        create :route, number: 1137, stop_points_count: 0 +        create :line, name: "Paris centre - Bercy > Lille > Londres" +        create :line, name: "Londres > Lille > Paris centre - Bercy"          SimpleImporter.define :test do |config|            config.model = Chouette::Route            config.separator = ";"            config.context = {stop_area_referential: stop_area_referential}            config.custom_handler do |row| -            fail_with_error "MISSING ROUTE: #{row["timetable_route_id"]}" do -              @current_record = Chouette::Route.find_by! number: row["timetable_route_id"] +            line = nil +            fail_with_error "MISSING LINE: #{row["route_name"]}" do +              line = Chouette::Line.find_by! name: row["route_name"]              end +            @current_record = Chouette::Route.find_or_initialize_by number: row["timetable_route_id"]              @current_record.name = row["route_name"] +            @current_record.published_name = row["route_name"] + +            @current_record.line = line              if @prev_route != @current_record -              @current_record.stop_points.destroy_all                if @prev_route                  journey_pattern = @prev_route.full_journey_pattern                  journey_pattern.set_distances @distances @@ -257,7 +261,12 @@ RSpec.describe SimpleImporter do                  stop_area.save!                end              end -            stop_point = @current_record.stop_points.build(stop_area_id: stop_area.id, position: position) +            stop_point = @current_record.stop_points.find_by(stop_area_id: stop_area.id) +            if stop_point +              stop_point.set_list_position position +            else +              stop_point = @current_record.stop_points.build(stop_area_id: stop_area.id, position: position) +            end              @prev_route = @current_record            end @@ -280,7 +289,7 @@ RSpec.describe SimpleImporter do          stop_areas_count = Chouette::StopArea.count          expect{importer.import(verbose: false)}.to change{Chouette::StopPoint.count}.by 20          expect(importer.status).to eq "success" -        expect(Chouette::Route.count).to eq routes_count +        expect(Chouette::Route.count).to eq routes_count + 2          expect(Chouette::JourneyPattern.count).to eq journey_pattern_count + 2          expect(Chouette::StopArea.count).to eq stop_areas_count + 5          route = Chouette::Route.find_by number: 1136 | 
