- Save QP on database

- Display on all Map (User and Gestionnaire)
This commit is contained in:
Xavier J 2015-11-25 17:05:14 +01:00
parent f4e63d40b0
commit eb5985efc4
9 changed files with 108 additions and 22 deletions

View file

@ -17,8 +17,6 @@ function initCarto() {
layers: [OSM] layers: [OSM]
}); });
display_qp([]);
freeDraw = new L.FreeDraw({ freeDraw = new L.FreeDraw({
//mode: L.FreeDraw.MODES.CREATE //mode: L.FreeDraw.MODES.CREATE
}); });
@ -36,6 +34,8 @@ function initCarto() {
map.setView(new L.LatLng(position.lat, position.lon), 5); map.setView(new L.LatLng(position.lat, position.lon), 5);
add_event_freeDraw(); add_event_freeDraw();
display_qp(JSON.parse($("#quartier_prioritaires").val()));
} }
function add_event_freeDraw() { function add_event_freeDraw() {
@ -94,10 +94,13 @@ function display_qp(qp_list) {
new_qpLayer(); new_qpLayer();
console.log(qp_list);
if (qp_array.length > 0) { if (qp_array.length > 0) {
qp_array.forEach(function (qp) { qp_array.forEach(function (qp) {
$("#qp_list ul").append('<li>' + qp.commune + ' : ' + qp.nom + '</li>'); $("#qp_list ul").append('<li>' + qp.commune + ' : ' + qp.nom + '</li>');
qpItems.addData(qp.geometry)
qpItems.addData(qp.geometry);
}); });
} }
else else

View file

@ -8,9 +8,21 @@ class Users::CarteController < UsersController
redirect_to url_for(root_path) redirect_to url_for(root_path)
end end
#TODO change name funtion def save
def save_ref_api_carto
dossier = current_user_dossier dossier = current_user_dossier
dossier.quartier_prioritaires.all.map(&:destroy)
unless params[:json_latlngs] == '' || params[:json_latlngs] == '[]'
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.new(qp).save
end
end
dossier.update_attributes(json_latlngs: params[:json_latlngs]) dossier.update_attributes(json_latlngs: params[:json_latlngs])
if dossier.draft? if dossier.draft?
@ -38,9 +50,7 @@ class Users::CarteController < UsersController
end end
def get_qp def get_qp
coordinates = JSON.parse(params[:coordinates]) qp = generate_qp JSON.parse(params[:coordinates])
qp = generate_qp coordinates
render json: {quartier_prioritaires: qp} render json: {quartier_prioritaires: qp}
end end

View file

@ -1,3 +1,7 @@
class QuartierPrioritaire < ActiveRecord::Base class QuartierPrioritaire < ActiveRecord::Base
belongs_to :dossier belongs_to :dossier
def geometry
JSON.parse(read_attribute(:geometry))
end
end end

View file

@ -17,6 +17,7 @@
.col-lg-6.col-md-6 .col-lg-6.col-md-6
#map.mini #map.mini
%input{id: 'json_latlngs', type:'hidden', value: "#{@dossier.json_latlngs}"} %input{id: 'json_latlngs', type:'hidden', value: "#{@dossier.json_latlngs}"}
%input{id: 'quartier_prioritaires', type:'hidden', value: "#{@dossier.quartier_prioritaires.to_json}"}
%script{type: 'text/javascript'} %script{type: 'text/javascript'}
= "var dossier_id =#{@dossier.id}" = "var dossier_id =#{@dossier.id}"
initCarto(); initCarto();

View file

@ -17,9 +17,10 @@
%h3 Quartiers prioritaites %h3 Quartiers prioritaites
%ul %ul
= form_tag(url_for({controller: :carte, action: :save_ref_api_carto, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do = form_tag(url_for({controller: :carte, action: :save, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do
%br %br
%input{type: 'hidden', value: "#{@dossier.json_latlngs}", name: 'json_latlngs', id: 'json_latlngs'} %input{type: 'hidden', value: "#{@dossier.json_latlngs}", name: 'json_latlngs', id: 'json_latlngs'}
%input{id: 'quartier_prioritaires', type:'hidden', value: "#{@dossier.quartier_prioritaires.to_json}"}
-if @dossier.draft? -if @dossier.draft?
=render partial: '/layouts/etape_suivante' =render partial: '/layouts/etape_suivante'

View file

@ -35,7 +35,7 @@ Rails.application.routes.draw do
get '/carte/position' => 'carte#get_position' get '/carte/position' => 'carte#get_position'
post '/carte/qp' => 'carte#get_qp' post '/carte/qp' => 'carte#get_qp'
get '/carte' => 'carte#show' get '/carte' => 'carte#show'
post '/carte' => 'carte#save_ref_api_carto' post '/carte' => 'carte#save'
end end
resource :dossiers resource :dossiers
@ -70,6 +70,7 @@ Rails.application.routes.draw do
post 'valid' => 'dossiers#valid' post 'valid' => 'dossiers#valid'
post 'close' => 'dossiers#close' post 'close' => 'dossiers#close'
end end
resources :commentaires, only: [:create] resources :commentaires, only: [:create]
end end

View file

@ -6,7 +6,6 @@ RSpec.describe Users::CarteController, type: :controller do
let(:dossier) { create(:dossier, :with_user, :with_procedure) } let(:dossier) { create(:dossier, :with_user, :with_procedure) }
let!(:entreprise) { create(:entreprise, dossier: dossier) } let!(:entreprise) { create(:entreprise, dossier: dossier) }
let!(:etablissement) { create(:etablissement, dossier: dossier) } let!(:etablissement) { create(:etablissement, dossier: dossier) }
let(:dossier_id) { dossier.id }
let(:bad_dossier_id) { Dossier.count + 1000 } let(:bad_dossier_id) { Dossier.count + 1000 }
let(:adresse) { etablissement.adresse } let(:adresse) { etablissement.adresse }
@ -21,13 +20,13 @@ RSpec.describe Users::CarteController, type: :controller do
end end
it 'redirects to users/sign_in' do it 'redirects to users/sign_in' do
get :show, dossier_id: dossier_id get :show, dossier_id: dossier.id
expect(response).to redirect_to('/users/sign_in') expect(response).to redirect_to('/users/sign_in')
end end
end end
it 'returns http success' do it 'returns http success' do
get :show, dossier_id: dossier_id get :show, dossier_id: dossier.id
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
@ -39,18 +38,18 @@ RSpec.describe Users::CarteController, type: :controller do
it_behaves_like "not owner of dossier", :show it_behaves_like "not owner of dossier", :show
end end
describe 'POST #save_ref_api_carto' do describe 'POST #save' do
context 'Aucune localisation n\'a jamais été enregistrée' do context 'Aucune localisation n\'a jamais été enregistrée' do
it do it do
post :save_ref_api_carto, dossier_id: dossier_id post :save, dossier_id: dossier.id, json_latlngs: ''
expect(response).to redirect_to("/users/dossiers/#{dossier_id}/description") expect(response).to redirect_to("/users/dossiers/#{dossier.id}/description")
end end
end end
context 'En train de modifier la localisation' do context 'En train de modifier la localisation' do
let(:dossier) { create(:dossier, :with_procedure, :with_user, state: 'initiated') } let(:dossier) { create(:dossier, :with_procedure, :with_user, state: 'initiated') }
before do before do
post :save_ref_api_carto, dossier_id: dossier_id post :save, dossier_id: dossier.id, json_latlngs: ''
end end
context 'Enregistrement d\'un commentaire informant la modification' do context 'Enregistrement d\'un commentaire informant la modification' do
@ -65,12 +64,61 @@ RSpec.describe Users::CarteController, type: :controller do
end end
it 'champs dossier' do it 'champs dossier' do
expect(subject.dossier.id).to eq(dossier_id) expect(subject.dossier.id).to eq(dossier.id)
end end
end end
it 'Redirection vers la page récapitulatif' do it 'Redirection vers la page récapitulatif' do
expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif") expect(response).to redirect_to("/users/dossiers/#{dossier.id}/recapitulatif")
end
end
describe 'Save quartier prioritaire' do
before do
allow_any_instance_of(CARTO::SGMAP::QuartierPrioritaireAdapter).
to receive(:to_params).
and_return({"QPCODE1234" => {:code => "QPCODE1234", :nom => "QP de test", :commune => "Paris", :geometry => {:type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]]}}})
post :save, dossier_id: dossier.id, json_latlngs: json_latlngs
end
context 'when json_latlngs params is empty' do
context 'when dossier have quartier prioritaire in database' do
let!(:dossier) { create(:dossier, :with_user, :with_procedure, :with_two_quartier_prioritaires) }
before do
dossier.reload
end
context 'when value is empty' do
let(:json_latlngs) { '' }
it { expect(dossier.quartier_prioritaires.size).to eq(0) }
end
context 'when value is empty array' do
let(:json_latlngs) { '[]' }
it { expect(dossier.quartier_prioritaires.size).to eq(0) }
end
end
end
context 'when json_latlngs params is informed' do
let(:json_latlngs) { '[[{"lat":48.87442541960633,"lng":2.3859214782714844},{"lat":48.87273183590832,"lng":2.3850631713867183},{"lat":48.87081237174292,"lng":2.3809432983398438},{"lat":48.8712640169951,"lng":2.377510070800781},{"lat":48.87510283703279,"lng":2.3778533935546875},{"lat":48.87544154230615,"lng":2.382831573486328},{"lat":48.87442541960633,"lng":2.3859214782714844}]]' }
before do
dossier.reload
end
it { expect(dossier.quartier_prioritaires.size).to eq(1) }
describe 'Quartier Prioritaire' do
subject { QuartierPrioritaire.last }
it { expect(subject.code).to eq('QPCODE1234') }
it { expect(subject.commune).to eq('Paris') }
it { expect(subject.nom).to eq('QP de test') }
it { expect(subject.dossier_id).to eq(dossier.id) }
end
end end
end end
end end
@ -98,7 +146,7 @@ RSpec.describe Users::CarteController, type: :controller do
stub_request(:get, "http://api-adresse.data.gouv.fr/search?limit=1&q=#{adresse}") stub_request(:get, "http://api-adresse.data.gouv.fr/search?limit=1&q=#{adresse}")
.to_return(status: 200, body: '{"query": "50 avenue des champs u00e9lysu00e9es Paris 75008", "version": "draft", "licence": "ODbL 1.0", "features": [{"geometry": {"coordinates": [2.306888, 48.870374], "type": "Point"}, "type": "Feature", "properties": {"city": "Paris", "label": "50 Avenue des Champs u00c9lysu00e9es 75008 Paris", "housenumber": "50", "id": "ADRNIVX_0000000270748251", "postcode": "75008", "name": "50 Avenue des Champs u00c9lysu00e9es", "citycode": "75108", "context": "75, u00cele-de-France", "score": 0.9054545454545454, "type": "housenumber"}}], "type": "FeatureCollection", "attribution": "BAN"}', headers: {}) .to_return(status: 200, body: '{"query": "50 avenue des champs u00e9lysu00e9es Paris 75008", "version": "draft", "licence": "ODbL 1.0", "features": [{"geometry": {"coordinates": [2.306888, 48.870374], "type": "Point"}, "type": "Feature", "properties": {"city": "Paris", "label": "50 Avenue des Champs u00c9lysu00e9es 75008 Paris", "housenumber": "50", "id": "ADRNIVX_0000000270748251", "postcode": "75008", "name": "50 Avenue des Champs u00c9lysu00e9es", "citycode": "75108", "context": "75, u00cele-de-France", "score": 0.9054545454545454, "type": "housenumber"}}], "type": "FeatureCollection", "attribution": "BAN"}', headers: {})
get :get_position, dossier_id: dossier_id get :get_position, dossier_id: dossier.id
end end
subject { JSON.parse(response.body) } subject { JSON.parse(response.body) }
@ -126,7 +174,7 @@ RSpec.describe Users::CarteController, type: :controller do
to receive(:to_params). to receive(:to_params).
and_return({"QPCODE1234" => {:code => "QPCODE1234", :geometry => {:type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]]}}}) and_return({"QPCODE1234" => {:code => "QPCODE1234", :geometry => {:type => "MultiPolygon", :coordinates => [[[[2.38715792094576, 48.8723062632126], [2.38724851642619, 48.8721392348061]]]]}}})
post :get_qp, dossier_id: dossier_id, coordinates: coordinates post :get_qp, dossier_id: dossier.id, coordinates: coordinates
end end
context 'when coordinates are empty' do context 'when coordinates are empty' do

View file

@ -24,5 +24,16 @@ FactoryGirl.define do
dossier.user = create(:user) dossier.user = create(:user)
end end
end end
trait :with_two_quartier_prioritaires do
after(:build) do |dossier, _evaluator|
qp1 = create(:quartier_prioritaire)
qp2 = create(:quartier_prioritaire)
dossier.quartier_prioritaires << qp1
dossier.quartier_prioritaires << qp2
end
end
end end
end end

View file

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :quartier_prioritaire do
code 'QPcode'
commune 'Paris'
nom 'Test des QP'
end
end