Merge pull request #3098 from betagouv/dev

MEP 2018-11-29-01
This commit is contained in:
Mathieu Magnin 2018-11-29 18:00:21 +01:00 committed by GitHub
commit a9fa3f4934
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 138 additions and 22 deletions

View file

@ -8,9 +8,9 @@ class Champs::CarteController < ApplicationController
@selector = ".carte-#{params[:position]}" @selector = ".carte-#{params[:position]}"
if params[:dossier].key?(:champs_attributes) if params[:dossier].key?(:champs_attributes)
geo_json = params[:dossier][:champs_attributes][params[:position]][:value] coordinates = params[:dossier][:champs_attributes][params[:position]][:value]
else else
geo_json = params[:dossier][:champs_private_attributes][params[:position]][:value] coordinates = params[:dossier][:champs_private_attributes][params[:position]][:value]
end end
if params[:champ_id].present? if params[:champ_id].present?
@ -30,18 +30,18 @@ class Champs::CarteController < ApplicationController
geo_areas = [] geo_areas = []
if geo_json == EMPTY_GEO_JSON if coordinates == EMPTY_GEO_JSON
@champ.value = nil @champ.value = nil
@champ.geo_areas = [] @champ.geo_areas = []
elsif geo_json == ERROR_GEO_JSON elsif coordinates == ERROR_GEO_JSON
@error = true @error = true
@champ.value = nil @champ.value = nil
@champ.geo_areas = [] @champ.geo_areas = []
else else
geo_json = JSON.parse(geo_json) coordinates = JSON.parse(coordinates)
if @champ.cadastres? if @champ.cadastres?
cadastres = ModuleApiCartoService.generate_cadastre(geo_json) cadastres = ModuleApiCartoService.generate_cadastre(coordinates)
geo_areas += cadastres.map do |cadastre| geo_areas += cadastres.map do |cadastre|
cadastre[:source] = GeoArea.sources.fetch(:cadastre) cadastre[:source] = GeoArea.sources.fetch(:cadastre)
cadastre cadastre
@ -49,7 +49,7 @@ class Champs::CarteController < ApplicationController
end end
if @champ.quartiers_prioritaires? if @champ.quartiers_prioritaires?
quartiers_prioritaires = ModuleApiCartoService.generate_qp(geo_json) quartiers_prioritaires = ModuleApiCartoService.generate_qp(coordinates)
geo_areas += quartiers_prioritaires.map do |qp| geo_areas += quartiers_prioritaires.map do |qp|
qp[:source] = GeoArea.sources.fetch(:quartier_prioritaire) qp[:source] = GeoArea.sources.fetch(:quartier_prioritaire)
qp qp
@ -57,7 +57,7 @@ class Champs::CarteController < ApplicationController
end end
if @champ.parcelles_agricoles? if @champ.parcelles_agricoles?
parcelles_agricoles = ModuleApiCartoService.generate_rpg(geo_json) parcelles_agricoles = ModuleApiCartoService.generate_rpg(coordinates)
geo_areas += parcelles_agricoles.map do |parcelle_agricole| geo_areas += parcelles_agricoles.map do |parcelle_agricole|
parcelle_agricole[:source] = GeoArea.sources.fetch(:parcelle_agricole) parcelle_agricole[:source] = GeoArea.sources.fetch(:parcelle_agricole)
parcelle_agricole parcelle_agricole
@ -68,7 +68,7 @@ class Champs::CarteController < ApplicationController
GeoArea.new(geo_area) GeoArea.new(geo_area)
end end
@champ.value = geo_json.to_json @champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)
end end
if @champ.persisted? if @champ.persisted?

View file

@ -44,18 +44,18 @@ class Champs::CarteChamp < Champ
end end
def geo_json def geo_json
@geo_json ||= value.blank? ? [] : JSON.parse(value) @geo_json ||= value.blank? ? nil : JSON.parse(value)
end end
def user_geometry def user_geometry
{ # We used to store in the value column a json array with coordinates.
type: 'Polygon', if geo_json.is_a?(Array)
coordinates: [ # If it is a coordinates array, format it as a GEO-JSON
geo_json[0].map do |polygon| GeojsonService.to_json_polygon_for_selection_utilisateur(geo_json)
[polygon['lat'], polygon['lng']] else
# It is already a GEO-JSON
geo_json
end end
]
}
end end
def user_geo_area def user_geo_area

View file

@ -1,5 +1,5 @@
class Champs::DecimalNumberChamp < Champ class Champs::DecimalNumberChamp < Champ
validates :value, numericality: { allow_nil: true } validates :value, numericality: { allow_nil: true, allow_blank: true }
def value_for_export def value_for_export
value.to_f value.to_f

View file

@ -1,5 +1,5 @@
class Champs::IntegerNumberChamp < Champ class Champs::IntegerNumberChamp < Champ
validates :value, numericality: { only_integer: true, allow_nil: true } validates :value, numericality: { only_integer: true, allow_nil: true, allow_blank: true }
def value_for_export def value_for_export
value.to_i value.to_i

View file

@ -199,6 +199,7 @@ class Procedure < ApplicationRecord
end end
def clone(admin, from_library) def clone(admin, from_library)
populate_champ_stable_ids
procedure = self.deep_clone(include: procedure = self.deep_clone(include:
{ {
types_de_piece_justificative: nil, types_de_piece_justificative: nil,
@ -358,6 +359,12 @@ class Procedure < ApplicationRecord
gestionnaire || gestionnaires.first gestionnaire || gestionnaires.first
end end
def populate_champ_stable_ids
TypeDeChamp.where(procedure: self, stable_id: nil).find_each do |type_de_champ|
type_de_champ.update_column(:stable_id, type_de_champ.id)
end
end
private private
def claim_path_ownership!(path) def claim_path_ownership!(path)

View file

@ -36,6 +36,7 @@ class TypeDeChamp < ApplicationRecord
store :options, accessors: [:cadastres, :quartiers_prioritaires, :parcelles_agricoles] store :options, accessors: [:cadastres, :quartiers_prioritaires, :parcelles_agricoles]
after_initialize :set_dynamic_type after_initialize :set_dynamic_type
after_create :populate_stable_id
attr_reader :dynamic_type attr_reader :dynamic_type
@ -117,6 +118,12 @@ class TypeDeChamp < ApplicationRecord
private private
def populate_stable_id
if !stable_id
update_column(:stable_id, id)
end
end
def remove_piece_justificative_template def remove_piece_justificative_template
if type_champ != TypeDeChamp.type_champs.fetch(:piece_justificative) && piece_justificative_template.attached? if type_champ != TypeDeChamp.type_champs.fetch(:piece_justificative) && piece_justificative_template.attached?
piece_justificative_template.purge_later piece_justificative_template.purge_later

View file

@ -4,4 +4,8 @@ class TypeDeChampSerializer < ActiveModel::Serializer
:type_champ, :type_champ,
:order_place, :order_place,
:description :description
def id
object.stable_id || object.id
end
end end

View file

@ -36,4 +36,21 @@ class GeojsonService
polygon.to_json polygon.to_json
end end
def self.to_json_polygon_for_selection_utilisateur(coordinates)
coordinates = coordinates.map do |lat_longs|
outbounds = lat_longs.map do |lat_long|
[lat_long['lng'], lat_long['lat']]
end
[outbounds]
end
polygon = {
type: 'MultiPolygon',
coordinates: coordinates
}
polygon.to_json
end
end end

View file

@ -11,7 +11,7 @@ clever_cloud:
bucket: <%= ENV['CLEVER_CLOUD_BUCKET'] %> bucket: <%= ENV['CLEVER_CLOUD_BUCKET'] %>
openstack: openstack:
service: OpenStack service: OpenStack
container: "<%= ENV['FOG_DIRECTORY'] %>" container: "<%= ENV['FOG_ACTIVESTORAGE_DIRECTORY'] %>"
credentials: credentials:
openstack_tenant: "<%= ENV['FOG_OPENSTACK_TENANT'] %>" openstack_tenant: "<%= ENV['FOG_OPENSTACK_TENANT'] %>"
openstack_api_key: "<%= ENV['FOG_OPENSTACK_API_KEY'] %>" openstack_api_key: "<%= ENV['FOG_OPENSTACK_API_KEY'] %>"

View file

@ -0,0 +1,6 @@
class AddStableIdToTypesDeChamp < ActiveRecord::Migration[5.2]
def change
add_column :types_de_champ, :stable_id, :bigint
add_index :types_de_champ, :stable_id
end
end

View file

@ -556,7 +556,9 @@ ActiveRecord::Schema.define(version: 2018_11_23_195208) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.jsonb "options" t.jsonb "options"
t.bigint "stable_id"
t.index ["private"], name: "index_types_de_champ_on_private" t.index ["private"], name: "index_types_de_champ_on_private"
t.index ["stable_id"], name: "index_types_de_champ_on_stable_id"
end end
create_table "types_de_piece_justificative", id: :serial, force: :cascade do |t| create_table "types_de_piece_justificative", id: :serial, force: :cascade do |t|

View file

@ -0,0 +1,15 @@
namespace :after_party do
desc 'Deployment task: add_stable_id_to_types_de_champ'
task add_stable_id_to_types_de_champ: :environment do
types_de_champ = TypeDeChamp.where(stable_id: nil)
bar = RakeProgressbar.new(types_de_champ.count)
types_de_champ.find_each do |type_de_champ|
type_de_champ.update_column(:stable_id, type_de_champ.id)
bar.inc
end
bar.finished
AfterParty::TaskRecord.create version: '20181123181252'
end
end

View file

@ -21,5 +21,17 @@ describe Champs::DecimalNumberChamp do
it { is_expected.to_not be_valid } it { is_expected.to_not be_valid }
end end
context 'when the value is blank' do
let(:value) { '' }
it { is_expected.to be_valid }
end
context 'when the value is nil' do
let(:value) { nil }
it { is_expected.to be_valid }
end
end end
end end

View file

@ -21,5 +21,17 @@ describe Champs::IntegerNumberChamp do
it { is_expected.to_not be_valid } it { is_expected.to_not be_valid }
end end
context 'when the value is blank' do
let(:value) { '' }
it { is_expected.to be_valid }
end
context 'when the value is nil' do
let(:value) { nil }
it { is_expected.to be_valid }
end
end end
end end

View file

@ -424,6 +424,11 @@ describe Procedure do
expect(subject.path).to be_nil expect(subject.path).to be_nil
end end
end end
it 'should keep types_de_champ ids stable' do
expect(subject.types_de_champ_ordered.first.id).not_to eq(procedure.types_de_champ_ordered.first.id)
expect(subject.types_de_champ_ordered.first.stable_id).to eq(procedure.types_de_champ_ordered.first.id)
end
end end
describe '#publish!' do describe '#publish!' do

View file

@ -39,6 +39,15 @@ shared_examples 'type_de_champ_spec' do
it { is_expected.to allow_value('blabla').for(:description) } it { is_expected.to allow_value('blabla').for(:description) }
end end
context 'stable_id' do
it {
type_de_champ = create(:type_de_champ_text)
expect(type_de_champ.id).to eq(type_de_champ.stable_id)
cloned_type_de_champ = type_de_champ.clone
expect(cloned_type_de_champ.stable_id).to eq(type_de_champ.stable_id)
}
end
context 'remove piece_justificative_template' do context 'remove piece_justificative_template' do
context 'when the tdc is piece_justificative' do context 'when the tdc is piece_justificative' do
let(:template_double) { double('template', attached?: attached, purge_later: true) } let(:template_double) { double('template', attached?: attached, purge_later: true) }

View file

@ -21,7 +21,27 @@ describe ChampSerializer do
context 'when type champ is carte' do context 'when type champ is carte' do
let(:geo_area) { create(:geo_area) } let(:geo_area) { create(:geo_area) }
let(:champ) { create(:type_de_champ_carte).champ.create(geo_areas: [geo_area]) } let(:coordinates) { [[{ "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 }]] }
let(:champ_carte) { create(:champ_carte, value: coordinates.to_json, geo_areas: [geo_area]) }
let(:champ) { champ_carte }
context 'legacy champ user_geometry' do
let(:champ) { champ_carte.user_geo_area }
it {
expect(subject).to include(
type_de_champ: {
descripton: "",
id: -1,
libelle: "user geometry",
order_place: -1,
type_champ: "user_geometry"
},
value: champ_carte.user_geometry
)
}
end
context 'and geo_area is cadastre' do context 'and geo_area is cadastre' do
it { it {