diff options
| -rw-r--r-- | app/assets/javascripts/stop_area_imports.js.coffee | 0 | ||||
| -rw-r--r-- | app/controllers/stop_area_imports_controller.rb | 24 | ||||
| -rw-r--r-- | app/models/stop_area_import.rb | 9 | ||||
| -rw-r--r-- | app/views/routes/show.html.erb | 6 | ||||
| -rw-r--r-- | app/views/stop_area_imports/new.html.erb | 24 | ||||
| -rw-r--r-- | app/views/stop_areas/index.html.erb | 3 | ||||
| -rw-r--r-- | config/locales/stop_area_import.yml | 18 | ||||
| -rw-r--r-- | config/locales/stop_area_imports.yml | 33 | ||||
| -rw-r--r-- | config/routes.rb | 1 | ||||
| -rw-r--r-- | spec/fixtures/stop_area_import_invalid.csv | 14 | ||||
| -rw-r--r-- | spec/fixtures/stop_area_import_valid.csv | 14 | ||||
| -rw-r--r-- | spec/models/stop_area_import_spec.rb | 2 | ||||
| -rw-r--r-- | spec/requests/stop_area_imports_spec.rb | 34 |
13 files changed, 142 insertions, 40 deletions
diff --git a/app/assets/javascripts/stop_area_imports.js.coffee b/app/assets/javascripts/stop_area_imports.js.coffee new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/app/assets/javascripts/stop_area_imports.js.coffee diff --git a/app/controllers/stop_area_imports_controller.rb b/app/controllers/stop_area_imports_controller.rb new file mode 100644 index 000000000..ff93dc40e --- /dev/null +++ b/app/controllers/stop_area_imports_controller.rb @@ -0,0 +1,24 @@ +class StopAreaImportsController < ChouetteController + belongs_to :referential + + actions :new, :create + respond_to :html, :only => :new + + def new + @stop_area_import = StopAreaImport.new + new! + end + + def create + @stop_area_import = StopAreaImport.new(params[:stop_area_import]) + if @stop_area_import.save + redirect_to referential_stop_areas_path( @referential ), notice: I18n.t("stop_area_imports.new.success") + else + flash[:error] = I18n.t("stop_area_imports.errors.import_aborted") + "<br>" + @stop_area_import.errors.full_messages.join("<br>") + render :new + end + end + + protected + +end diff --git a/app/models/stop_area_import.rb b/app/models/stop_area_import.rb index 09fd92ddb..52d8a94bb 100644 --- a/app/models/stop_area_import.rb +++ b/app/models/stop_area_import.rb @@ -10,7 +10,7 @@ class StopAreaImport validates_presence_of :file def initialize(attributes = {}) - attributes.each { |name, value| send("#{name}=", value) } + attributes.each { |name, value| send("#{name}=", value) } if attributes end def persisted? @@ -26,14 +26,15 @@ class StopAreaImport else imported_stop_areas.each_with_index do |imported_stop_area, index| imported_stop_area.errors.full_messages.each do |message| - errors.add :base, I18n.t("stop_area_import.errors.invalid_stop_area", :column => index+2, :message => message) + errors.add :base, I18n.t("stop_area_imports.errors.invalid_stop_area", :column => index+2, :message => message) end end false end end rescue Exception => exception - errors.add :base, I18n.t("stop_area_import.errors.exception", :message => exception.message) + Rails.logger.error(exception.message) + errors.add :base, I18n.t("stop_area_imports.errors.exception") false end end @@ -58,7 +59,7 @@ class StopAreaImport when '.csv' then Roo::CSV.new(file.path) when '.xls' then Roo::Excel.new(file.path) when '.xlsx' then Roo::Excelx.new(file.path) - else + else raise "Unknown file type: #{file.original_filename}" end end diff --git a/app/views/routes/show.html.erb b/app/views/routes/show.html.erb index c36ce0e94..63f52974c 100644 --- a/app/views/routes/show.html.erb +++ b/app/views/routes/show.html.erb @@ -101,9 +101,9 @@ <%= link_to t('journey_patterns.actions.new'), new_referential_line_route_journey_pattern_path(@referential, @line, @route), :class => "add" %> </li> <% end %> - <!-- <li> --> - <!-- <%= link_to t('vehicle_journey_imports.new.title'), new_referential_line_route_vehicle_journey_import_path( @referential, @line, @route ), :class => "import" %> --> - <!-- </li> --> + <li> + <%= link_to t('vehicle_journey_imports.new.title'), new_referential_line_route_vehicle_journey_import_path( @referential, @line, @route ), :class => "import" %> + </li> <% if @route.journey_patterns.size > 0 %> <li> <i class="fa fa-clock-o"></i><%= link_to t('vehicle_journeys.actions.index'), [@referential, @line, @route, :vehicle_journeys], :class => "vehicle_journeys" %> diff --git a/app/views/stop_area_imports/new.html.erb b/app/views/stop_area_imports/new.html.erb new file mode 100644 index 000000000..67b02892d --- /dev/null +++ b/app/views/stop_area_imports/new.html.erb @@ -0,0 +1,24 @@ +<%= title_tag t('stop_area_imports.new.title') %> + +<div class="main_actions"> + <div class="btn-group"> + <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> + <%= t('stop_area_imports.new.export_stop_areas') %><span class="caret"></span> + </button> + <ul class="dropdown-menu" role="menu"> + <li><%= link_to image_tag("icons/file_csv.png") + " Csv" , "/stop_area_imports.csv" %></li> + <li><%= link_to image_tag("icons/file_excel.png") + " Excel", "/stop_area_imports.xls" %></li> + </ul> + </div> +</div> + +<%= semantic_form_for [@referential, @stop_area_import] do |form| %> + <br> + <%= form.inputs do %> + <%= form.input :file, :as => :file, :input_html => { :title => "#{t('.tooltip.file')}", :'data-placement' => "right", :'data-toggle' => "tooltip", :'data-trigger' =>"hover" } %> + <% end %> + <%= form.actions do %> + <%= form.action :submit, :as => :button , :label => t( 'formtastic.import' ) %> + <%= form.action :cancel, :as => :link %> + <% end %> +<% end %> diff --git a/app/views/stop_areas/index.html.erb b/app/views/stop_areas/index.html.erb index 5519ea55e..1ff6f73d4 100644 --- a/app/views/stop_areas/index.html.erb +++ b/app/views/stop_areas/index.html.erb @@ -26,6 +26,9 @@ <ul class="actions"> <li><%= link_to t('stop_areas.actions.new'), new_referential_stop_area_path(@referential), :class => "add" %></li> <li><%= link_to t('stop_areas.actions.default_geometry'), default_geometry_referential_stop_areas_path(@referential), :method => :put, :class => "calculator_edit" %></li> + <li> + <%= link_to t('stop_area_imports.new.title'), new_referential_stop_area_import_path( @referential ), :class => "import" %> + </li> </ul> <h4><%= t(".selection") %></h4> diff --git a/config/locales/stop_area_import.yml b/config/locales/stop_area_import.yml deleted file mode 100644 index cb557b540..000000000 --- a/config/locales/stop_area_import.yml +++ /dev/null @@ -1,18 +0,0 @@ -en: - stop_area_import: - new: - title: "Import stop areas" - form: - file: "File" - errors: - invalid_stop_area: "Error column %{column}, stop_area is invalid : %{message}" - exception: "An exception occured : %{message}" -fr: - stop_area_import: - new: - title: "Import des arrêts" - form: - file: "Fichier" - errors: - invalid_stop_area: "Erreur colonne %{column}, l'arrêt est invalide : %{message}" - exception: "Une exception est survenu : %{message}"
\ No newline at end of file diff --git a/config/locales/stop_area_imports.yml b/config/locales/stop_area_imports.yml new file mode 100644 index 000000000..124157cba --- /dev/null +++ b/config/locales/stop_area_imports.yml @@ -0,0 +1,33 @@ +en: + stop_area_imports: + new: + title: "Import stop areas" + export_stop_areas: "Export a model" + succcess: "Import is a success" + tooltip: + file: "Select a CSV or Excel file" + errors: + import_aborted: "Errors prohibited this import from completing: " + invalid_stop_area: "Error column %{column}, stop_area is invalid : %{message}" + exception: "Invalid file, you must provide valid csv, xls or xlsx file" + activemodel: + attributes: + stop_area_import: + file: "File" +fr: + stop_area_imports: + new: + title: "Import des arrêts" + export_stop_areas: "Exporter un modèle" + succcess: "L'import des données est un succès" + tooltip: + file: "Sélectionner un fichier CSV ou Excel" + errors: + import_aborted: "Des erreurs ont empéchées le bon déroulement de l'import: " + invalid_stop_area: "Erreur colonne %{column}, l'arrêt est invalide : %{message}" + exception: "Le fichier est invalide, vous devez fournir un fichier csv, xls ou xlsx valide" + activemodel: + attributes: + stop_area_import: + file: "Fichier" +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 12a83eea1..fe4db9a12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,7 @@ ChouetteIhm::Application.routes.draw do end resources :referentials do + resources :stop_area_imports resources :api_keys resources :rule_parameter_sets resources :stop_point_areas diff --git a/spec/fixtures/stop_area_import_invalid.csv b/spec/fixtures/stop_area_import_invalid.csv index 522bf029c..7fc4caefc 100644 --- a/spec/fixtures/stop_area_import_invalid.csv +++ b/spec/fixtures/stop_area_import_invalid.csv @@ -1,7 +1,7 @@ -"id","name","registration_number","longitude","latitude","area_type","comment","country_code","street_name","mobility_restricted_suitability","stairs_availability","lift_availability","int_user_needs" -,"StopArea1",,"0.1","0.1",,"Comment",,,,,, -,"StopArea2",,"0.2","0.2",,"Comment",,,,,, -,"StopArea3",,"0.3","0.3",,"Comment",,,,,, -,"StopArea4",,"0.4","0.4",,"Comment",,,,,, -,"StopArea5",,"0.5","0.5",,"Comment",,,,,, -,"StopArea6",,"0.6","0.6",,"Comment",,,,,, +"id","name","longitude","latitude","area_type","comment","country_code","street_name","mobility_restricted_suitability","stairs_availability","lift_availability","int_user_needs" +,"StopArea1","0.1","0.1",,"Comment",,,,,, +,"StopArea2","0.2","0.2",,"Comment",,,,,, +,"StopArea3","0.3","0.3",,"Comment",,,,,, +,"StopArea4","0.4","0.4",,"Comment",,,,,, +,"StopArea5","0.5","0.5",,"Comment",,,,,, +,"StopArea6","0.6","0.6",,"Comment",,,,,, diff --git a/spec/fixtures/stop_area_import_valid.csv b/spec/fixtures/stop_area_import_valid.csv index 0dc54f215..a929378f9 100644 --- a/spec/fixtures/stop_area_import_valid.csv +++ b/spec/fixtures/stop_area_import_valid.csv @@ -1,7 +1,7 @@ -"id","name","registration_number","longitude","latitude","area_type","comment","country_code","street_name","mobility_restricted_suitability","stairs_availability","lift_availability","int_user_needs" -,"StopArea1","1","0.1","0.1","Quay","Comment",,,,,, -,"StopArea2","2","0.2","0.2","Quay","Comment",,,,,, -,"StopArea3","3","0.3","0.3","Quay","Comment",,,,,, -,"StopArea4","4","0.4","0.4","Quay","Comment",,,,,, -,"StopArea5","5","0.5","0.5","Quay","Comment",,,,,, -,"StopArea6","6","0.6","0.6","Quay","Comment",,,,,, +"id","name","longitude","latitude","area_type","comment","country_code","street_name","mobility_restricted_suitability","stairs_availability","lift_availability","int_user_needs" +,"StopArea1","0.1","0.1","Quay","Comment",,,,,, +,"StopArea2","0.2","0.2","Quay","Comment",,,,,, +,"StopArea3","0.3","0.3","Quay","Comment",,,,,, +,"StopArea4","0.4","0.4","Quay","Comment",,,,,, +,"StopArea5","0.5","0.5","Quay","Comment",,,,,, +,"StopArea6","0.6","0.6","Quay","Comment",,,,,, diff --git a/spec/models/stop_area_import_spec.rb b/spec/models/stop_area_import_spec.rb index 1792ba8a3..07f3a111b 100644 --- a/spec/models/stop_area_import_spec.rb +++ b/spec/models/stop_area_import_spec.rb @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- require 'spec_helper' -describe VehicleJourneyImport do +describe StopAreaImport do let(:valid_file) { csv_file = File.open(Rails.root.join("spec", "fixtures", "stop_area_import_valid.csv").to_s, "r") diff --git a/spec/requests/stop_area_imports_spec.rb b/spec/requests/stop_area_imports_spec.rb new file mode 100644 index 000000000..c0da1edac --- /dev/null +++ b/spec/requests/stop_area_imports_spec.rb @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +require 'spec_helper' + +describe "StopAreaImports" do + login_user + + let!(:line) { create :line } + let!(:valid_file_path) { Rails.root + "spec/fixtures/stop_area_import_valid.csv" } + let!(:invalid_file_path) { Rails.root + "spec/fixtures/stop_area_import_invalid.csv" } + + describe "new" do + it "should create stop areas and return to stop areas index page" do + visit new_referential_stop_area_import_path(referential) + attach_file('Fichier', valid_file_path) + click_button "Lancer l'import" + expect(page).to have_content(I18n.t("stop_area_imports.new.success")) + expect(page).to have_content("StopArea1") + end + + it "should return error messages when file is invalid" do + visit new_referential_stop_area_import_path(referential) + attach_file('Fichier', invalid_file_path) + click_button "Lancer l'import" + expect(page).to have_content(I18n.t("stop_area_imports.errors.import_aborted")) + end + + it "should return error message when file missing on upload" do + visit new_referential_stop_area_import_path(referential) + click_button "Lancer l'import" + expect(page).to have_content(I18n.t("stop_area_imports.errors.import_aborted")) + end + end + +end |
