From 5513504555bc4382242ec90b7f64e5ebdb65eaf3 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 27 Jan 2016 15:48:27 +0100 Subject: [PATCH] - Refactor code carte - Delete auto-comments when edit carte and description --- .gitignore | 2 +- app/assets/javascripts/carte/carte.js | 15 ++-- app/controllers/users/carte_controller.rb | 83 ++++--------------- .../users/description_controller.rb | 8 +- app/services/module_api_carto_service.rb | 37 +++++++++ lib/carto/geocodeur.rb | 2 +- .../users/carte_controller_spec.rb | 25 ++---- .../users/description_controller_spec.rb | 16 ---- spec/spec_helper.rb | 5 ++ 9 files changed, 74 insertions(+), 119 deletions(-) create mode 100644 app/services/module_api_carto_service.rb diff --git a/.gitignore b/.gitignore index 255ab6ef0..9b7818561 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,4 @@ doc/*.svg rubocop.html config/france_connect.yml uploads/* - +coverage/**/* diff --git a/app/assets/javascripts/carte/carte.js b/app/assets/javascripts/carte/carte.js index 41cd316c9..83cf88b97 100644 --- a/app/assets/javascripts/carte/carte.js +++ b/app/assets/javascripts/carte/carte.js @@ -6,7 +6,7 @@ function initCarto() { attribution: '© OpenStreetMap' }); - position = get_position() || default_position(); + position = get_position() || default_gestionnaire_position(); map = L.map("map", { center: new L.LatLng(position.lat, position.lon), @@ -36,13 +36,13 @@ function initCarto() { map.fitBounds(freeDraw.polygons[0].getBounds()); } else if (position.lat == LAT && position.lon == LON) - map.setView(new L.LatLng(position.lat, position.lon), 5); + map.setView(new L.LatLng(position.lat, position.lon), position.zoom); add_event_freeDraw(); } -function default_position() { - return {lon: LON, lat: LAT, zoom: 13} +function default_gestionnaire_position() { + return {lon: LON, lat: LAT, zoom: 5} } function get_external_data(latLngs) { @@ -50,17 +50,15 @@ function get_external_data(latLngs) { display_qp(get_qp(latLngs)); if (cadastre_active()) { - cadastre_list = []; - polygons = {"type": "FeatureCollection", "features": []}; for (i = 0; i < latLngs.length; i++) polygons.features.push(feature_polygon_latLngs(latLngs[i])) + cadastre_list = [{zoom_error: true}]; + if (turf.area(polygons) < 300000) cadastre_list = get_cadastre(latLngs); - else - cadastre_list = [{zoom_error: true}]; display_cadastre(cadastre_list); } @@ -108,7 +106,6 @@ function get_position() { async: false }).done(function (data) { position = data - position.zoom = default_position().zoom }); return position; diff --git a/app/controllers/users/carte_controller.rb b/app/controllers/users/carte_controller.rb index 8bebb307e..0bf52f02e 100644 --- a/app/controllers/users/carte_controller.rb +++ b/app/controllers/users/carte_controller.rb @@ -19,62 +19,39 @@ class Users::CarteController < UsersController dossier.cadastres.map(&:destroy) unless params[:json_latlngs].blank? - if dossier.procedure.module_api_carto.quartiers_prioritaires? - qp_list = generate_qp JSON.parse(params[:json_latlngs]) - - qp_list.each do |key, qp| - qp.merge!({dossier_id: dossier.id}) - qp[:geometry] = qp[:geometry].to_json - QuartierPrioritaire.create(qp) - end - end - - if dossier.procedure.module_api_carto.cadastre? - cadastre_list = generate_cadastre JSON.parse(params[:json_latlngs]) - - cadastre_list.each do |cadastre| - cadastre.merge!({dossier_id: dossier.id}) - cadastre[:geometry] = cadastre[:geometry].to_json - Cadastre.create(cadastre) - end - end + ModuleApiCartoService.save_qp! dossier, params[:json_latlngs] + ModuleApiCartoService.save_cadastre! dossier, params[:json_latlngs] end dossier.update_attributes(json_latlngs: params[:json_latlngs]) - if dossier.draft? - redirect_to url_for(controller: :description, action: :show, dossier_id: params[:dossier_id]) - else - commentaire_params = { - email: 'Modification localisation', - body: 'La localisation de la demande a été modifiée. Merci de le prendre en compte.', - dossier_id: dossier.id - } - Commentaire.create commentaire_params - redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params[:dossier_id]) - end + controller = :recapitulatif + controller = :description if dossier.draft? + + redirect_to url_for(controller: controller, action: :show, dossier_id: params[:dossier_id]) end def get_position - tmp_position = Carto::Geocodeur.convert_adresse_to_point(current_user_dossier.etablissement.geo_adresse) + point = Carto::Geocodeur.convert_adresse_to_point(current_user_dossier.etablissement.geo_adresse) - if !tmp_position.point.nil? - render json: {lon: tmp_position.point.x.to_s, lat: tmp_position.point.y.to_s, dossier_id: params[:dossier_id]} - else - render json: {lon: '0', lat: '0', dossier_id: params[:dossier_id]} + lon = '2.428462' + lat = '46.538192' + zoom = '13' + + unless point.nil? + lon = point.x.to_s + lat = point.y.to_s end + + render json: {lon: lon, lat: lat, zoom: zoom, dossier_id: params[:dossier_id]} end def get_qp - qp = generate_qp JSON.parse(params[:coordinates]) - - render json: {quartier_prioritaires: qp} + render json: {quartier_prioritaires: ModuleApiCartoService.generate_qp(JSON.parse(params[:coordinates]))} end def get_cadastre - cadastres = generate_cadastre JSON.parse(params[:coordinates]) - - render json: {cadastres: cadastres} + render json: {cadastres: ModuleApiCartoService.generate_cadastre(JSON.parse(params[:coordinates]))} end def self.route_authorization @@ -83,28 +60,4 @@ class Users::CarteController < UsersController api_carto: true } end - - private - - def generate_qp coordinates - qp = {} - - coordinates.each_with_index do |coordinate, index| - coordinate = coordinates[index].map { |latlng| [latlng['lng'], latlng['lat']] } - qp = qp.merge CARTO::SGMAP::QuartiersPrioritaires::Adapter.new(coordinate).to_params - end - - qp - end - - def generate_cadastre coordinates - cadastre = [] - - coordinates.each_with_index do |coordinate, index| - coordinate = coordinates[index].map { |latlng| [latlng['lng'], latlng['lat']] } - cadastre << CARTO::SGMAP::Cadastre::Adapter.new(coordinate).to_params - end - - cadastre.flatten - end end diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 7739f2ccd..584d5767d 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -50,13 +50,7 @@ class Users::DescriptionController < UsersController end end - if !@dossier.draft? - commentaire = Commentaire.create - commentaire.email = 'Modification détails' - commentaire.body = 'Les informations détaillées de la demande ont été modifiées. Merci de le prendre en compte.' - commentaire.dossier = @dossier - commentaire.save - else + if @dossier.draft? @dossier.initiated! end diff --git a/app/services/module_api_carto_service.rb b/app/services/module_api_carto_service.rb new file mode 100644 index 000000000..4838d13b1 --- /dev/null +++ b/app/services/module_api_carto_service.rb @@ -0,0 +1,37 @@ +class ModuleApiCartoService + def self.save_qp! dossier, json_latlngs + if dossier.procedure.module_api_carto.quartiers_prioritaires? + qp_list = generate_qp JSON.parse(json_latlngs) + + qp_list.each_value do |qp| + qp.merge!({dossier_id: dossier.id}) + qp[:geometry] = qp[:geometry].to_json + QuartierPrioritaire.create(qp) + end + end + end + + def self.save_cadastre! dossier, json_latlngs + if dossier.procedure.module_api_carto.cadastre? + cadastre_list = generate_cadastre JSON.parse(json_latlngs) + + cadastre_list.each do |cadastre| + cadastre.merge!({dossier_id: dossier.id}) + cadastre[:geometry] = cadastre[:geometry].to_json + Cadastre.create(cadastre) + end + end + end + + def self.generate_qp coordinates + coordinates.inject({}) { |acc, coordinate| + acc.merge CARTO::SGMAP::QuartiersPrioritaires::Adapter.new( + coordinate.map { |element| [element['lng'], element['lat']] }).to_params } + end + + def self.generate_cadastre coordinates + (coordinates.inject([]) { |acc, coordinate| + acc << CARTO::SGMAP::Cadastre::Adapter.new( + coordinate.map { |element| [element['lng'], element['lat']] }).to_params }).flatten + end +end diff --git a/lib/carto/geocodeur.rb b/lib/carto/geocodeur.rb index 67f033551..0d9e4f3ed 100644 --- a/lib/carto/geocodeur.rb +++ b/lib/carto/geocodeur.rb @@ -2,7 +2,7 @@ module Carto # this class take a string in input and return a point class Geocodeur def self.convert_adresse_to_point(address) - Carto::Bano::PointRetriever.new(address) + Carto::Bano::PointRetriever.new(address).point rescue RestClient::Exception, JSON::ParserError => e Rails.logger.error e.message nil diff --git a/spec/controllers/users/carte_controller_spec.rb b/spec/controllers/users/carte_controller_spec.rb index c26a3b879..01bef63d3 100644 --- a/spec/controllers/users/carte_controller_spec.rb +++ b/spec/controllers/users/carte_controller_spec.rb @@ -91,22 +91,6 @@ RSpec.describe Users::CarteController, type: :controller do post :save, dossier_id: dossier.id, json_latlngs: '' end - context 'Enregistrement d\'un commentaire informant la modification' do - subject { dossier.commentaires.last } - - it 'champs email' do - expect(subject.email).to eq('Modification localisation') - end - - it 'champs body' do - expect(subject.body).to eq('La localisation de la demande a été modifiée. Merci de le prendre en compte.') - end - - it 'champs dossier' do - expect(subject.dossier.id).to eq(dossier.id) - end - end - it 'Redirection vers la page récapitulatif' do expect(response).to redirect_to("/users/dossiers/#{dossier.id}/recapitulatif") end @@ -214,9 +198,10 @@ RSpec.describe Users::CarteController, type: :controller do end describe '#get_position' do - context 'Geocodeur renvoie des positions nil' do + context 'Geocodeur renvoie les positions par defaut' do let(:etablissement) { create(:etablissement, adresse: bad_adresse, numero_voie: 'dzj', type_voie: 'fzjfk', nom_voie: 'hdidjkz', complement_adresse: 'fjef', code_postal: 'fjeiefk', localite: 'zjfkfz') } let(:dossier) { create(:dossier, :with_procedure, etablissement: etablissement) } + before do stub_request(:get, /http:\/\/api-adresse[.]data[.]gouv[.]fr\/search[?]limit=1&q=/) .to_return(status: 200, body: '{"query": "babouba", "version": "draft", "licence": "ODbL 1.0", "features": [], "type": "FeatureCollection", "attribution": "BAN"}', headers: {}) @@ -225,9 +210,9 @@ RSpec.describe Users::CarteController, type: :controller do subject { JSON.parse(response.body) } - it 'on enregistre des coordonnées lat et lon à 0' do - expect(subject['lat']).to eq('0') - expect(subject['lon']).to eq('0') + it 'on enregistre des coordonnées lat et lon avec les valeurs par defaut' do + expect(subject['lat']).to eq('46.538192') + expect(subject['lon']).to eq('2.428462') end end diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index cafeca6d3..89403bdab 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -87,22 +87,6 @@ describe Users::DescriptionController, type: :controller do dossier.reload end - context 'Enregistrement d\'un commentaire informant la modification' do - subject { Commentaire.last } - - it 'champs email' do - expect(subject.email).to eq('Modification détails') - end - - it 'champs body' do - expect(subject.body).to eq('Les informations détaillées de la demande ont été modifiées. Merci de le prendre en compte.') - end - - it 'champs dossier' do - expect(subject.dossier.id).to eq(dossier_id) - end - end - it 'Redirection vers la page récapitulatif' do expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fb305ea06..76da353d4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,6 +19,10 @@ ENV['RAILS_ENV'] ||= 'test' +require 'simplecov' +SimpleCov.start 'rails' +puts "required simplecov" + require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' require 'capybara/rspec' @@ -34,6 +38,7 @@ Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, js_errors: true, port: 44_678 + ENV['TEST_ENV_NUMBER'].to_i, phantomjs_options: ['--proxy-type=none'], timeout: 180) end + Capybara.default_wait_time = 1 # Requires supporting ruby files with custom matchers and macros, etc,