From dfc46a8736f3aae8d355693b99f7e84383fccf25 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 May 2021 16:10:01 +0200 Subject: [PATCH] Enable add descriptions to cadastres --- app/controllers/champs/carte_controller.rb | 14 ++++++++--- app/models/champ.rb | 2 +- .../shared/champs/carte/_geo_area.html.haml | 11 ++++++++ .../shared/champs/carte/_geo_areas.html.haml | 25 +++---------------- 4 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 app/views/shared/champs/carte/_geo_area.html.haml diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 11989e116..b94d8db57 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -10,12 +10,12 @@ class Champs::CarteController < ApplicationController def create champ = policy_scope(Champ).find(params[:champ_id]) geo_area = if params_source == GeoArea.sources.fetch(:cadastre) - champ.geo_areas.find_by("properties->>'id' = :id", id: params_feature[:properties][:id]) + champ.geo_areas.find_by("properties->>'id' = :id", id: create_params_feature[:properties][:id]) end if geo_area.nil? geo_area = champ.geo_areas.build(source: params_source, properties: {}) - save_feature!(geo_area, params_feature) + save_feature!(geo_area, create_params_feature) end render json: { feature: geo_area.to_feature }, status: :created @@ -24,7 +24,7 @@ class Champs::CarteController < ApplicationController def update champ = policy_scope(Champ).find(params[:champ_id]) geo_area = champ.geo_areas.find(params[:id]) - save_feature!(geo_area, params_feature) + save_feature!(geo_area, update_params_feature) head :no_content end @@ -42,7 +42,7 @@ class Champs::CarteController < ApplicationController params[:source] end - def params_feature + def create_params_feature params.require(:feature).permit(properties: [ :filename, :description, @@ -60,6 +60,12 @@ class Champs::CarteController < ApplicationController end end + def update_params_feature + params.require(:feature).permit(properties: [:description]).tap do |feature| + feature[:geometry] = params[:feature][:geometry] + end + end + def save_feature!(geo_area, feature) if feature[:geometry] geo_area.geometry = feature[:geometry] diff --git a/app/models/champ.rb b/app/models/champ.rb index 4b3fda82e..30c896e11 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -25,7 +25,7 @@ class Champ < ApplicationRecord # We declare champ specific relationships (Champs::CarteChamp, Champs::SiretChamp and Champs::RepetitionChamp) # here because otherwise we can't easily use includes in our queries. - has_many :geo_areas, dependent: :destroy + has_many :geo_areas, -> { order(:created_at) }, dependent: :destroy, inverse_of: :champ belongs_to :etablissement, optional: true, dependent: :destroy has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy diff --git a/app/views/shared/champs/carte/_geo_area.html.haml b/app/views/shared/champs/carte/_geo_area.html.haml new file mode 100644 index 000000000..c656ca71f --- /dev/null +++ b/app/views/shared/champs/carte/_geo_area.html.haml @@ -0,0 +1,11 @@ +%li{ class: editing ? '' : 'flex column mb-2' } + - if editing + = link_to '#', data: { geo_area: geo_area.id } do + = geo_area_label(geo_area) + = text_field_tag :description, geo_area.description, data: { geo_area: geo_area.id }, placeholder: 'Description', class: 'no-margin' + - else + = link_to '#', data: { geo_area: geo_area.id } do + = geo_area_label(geo_area) + - if geo_area.description.present? + %span + = geo_area.description diff --git a/app/views/shared/champs/carte/_geo_areas.html.haml b/app/views/shared/champs/carte/_geo_areas.html.haml index 6d594b3da..80418dd84 100644 --- a/app/views/shared/champs/carte/_geo_areas.html.haml +++ b/app/views/shared/champs/carte/_geo_areas.html.haml @@ -3,28 +3,11 @@ .areas %ul - champ.selections_utilisateur.each do |geo_area| - %li{ class: editing ? '' : 'flex column mb-2' } - - if editing - = link_to '#', data: { geo_area: geo_area.id } do - = geo_area_label(geo_area) - = text_field_tag :description, geo_area.description, data: { geo_area: geo_area.id }, placeholder: 'Description de la sélection', class: 'no-margin' - - else - = link_to '#', data: { geo_area: geo_area.id } do - = geo_area_label(geo_area) - - if geo_area.description.present? - %span - = geo_area.description + = render partial: 'shared/champs/carte/geo_area', locals: { geo_area: geo_area, editing: editing } - if champ.cadastres? .areas-title Parcelles cadastrales .areas - - if !champ.geometry? - Aucune zone tracée - - elsif champ.cadastres.blank? - = t('errors.messages.cadastres_empty', count: champ.selections_utilisateur.size) - - else - %ul - - champ.cadastres.each do |geo_area| - %li.flex.column.mb-2 - = link_to '#', data: { geo_area: geo_area.id } do - = geo_area_label(geo_area) + %ul + - champ.cadastres.each do |geo_area| + = render partial: 'shared/champs/carte/geo_area', locals: { geo_area: geo_area, editing: editing }