From 42153d1d37a1897abf52367bf0cf7572ae12c607 Mon Sep 17 00:00:00 2001 From: lydiasan <> Date: Wed, 12 May 2021 16:30:35 +0200 Subject: [PATCH 01/11] ajout du selecteur de langue --- app/assets/images/icons/translate-icon.svg | 1 + app/controllers/application_controller.rb | 15 +++++++++++---- app/controllers/root_controller.rb | 5 +++++ app/views/layouts/_header.haml | 3 +++ app/views/layouts/_locale_dropdown.html.haml | 11 +++++++++++ config/routes.rb | 1 + 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/assets/images/icons/translate-icon.svg create mode 100644 app/views/layouts/_locale_dropdown.html.haml diff --git a/app/assets/images/icons/translate-icon.svg b/app/assets/images/icons/translate-icon.svg new file mode 100644 index 000000000..2c34cd92b --- /dev/null +++ b/app/assets/images/icons/translate-icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 35b393a6b..c23773bd8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,7 +18,8 @@ class ApplicationController < ActionController::Base before_action :set_active_storage_host before_action :setup_javascript_settings before_action :setup_tracking - before_action :set_locale + + around_action :switch_locale helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?, :administrateur_signed_in?, :current_administrateur, :current_account @@ -308,9 +309,15 @@ class ApplicationController < ActionController::Base current_user&.email end - def set_locale - if ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' - I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) + def switch_locale(&action) + locale = nil + if cookies[:locale] + locale = cookies[:locale] + elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' + locale = http_accept_language.compatible_language_from(I18n.available_locales) + else + locale = I18n.default_locale end + I18n.with_locale(locale, &action) end end diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index a61a15282..1a1c7e910 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -87,4 +87,9 @@ class RootController < ApplicationController format.js { render js: helpers.remove_element('#outdated-browser-banner') } end end + + def save_locale + cookies[:locale] = params[:locale] + redirect_to request.referer + end end diff --git a/app/views/layouts/_header.haml b/app/views/layouts/_header.haml index 88e2c7266..0ab8116d8 100644 --- a/app/views/layouts/_header.haml +++ b/app/views/layouts/_header.haml @@ -81,3 +81,6 @@ - else = render partial: 'shared/help/help_button' + + %li + = render partial: 'layouts/locale_dropdown' diff --git a/app/views/layouts/_locale_dropdown.html.haml b/app/views/layouts/_locale_dropdown.html.haml new file mode 100644 index 000000000..d05429745 --- /dev/null +++ b/app/views/layouts/_locale_dropdown.html.haml @@ -0,0 +1,11 @@ +.dropdown.locale-dropdown + %button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' } + .hidden Translate + = image_tag "icons/translate-icon.svg", alt: 'Translate', 'aria-hidden':'true' + %ul.header-menu.dropdown-content#mon_compte_menu + %li + = link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do + EN - English + %li + = link_to save_locale_path(locale: :fr), method: :post, class: "menu-item menu-link" do + FR - français diff --git a/config/routes.rb b/config/routes.rb index acdac88ea..f9714db2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -151,6 +151,7 @@ Rails.application.routes.draw do get "patron" => "root#patron" get "suivi" => "root#suivi" post "dismiss_outdated_browser" => "root#dismiss_outdated_browser" + post "save_locale" => "root#save_locale" get "contact", to: "support#index" post "contact", to: "support#create" From 71069c0e1b4603a05e48beaede0b5fc3b4151919 Mon Sep 17 00:00:00 2001 From: lydiasan <> Date: Tue, 25 May 2021 10:05:52 +0200 Subject: [PATCH 02/11] correction de la variable d'environnement 'LOCALIZATION_ENABLED' --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c23773bd8..6ce5dc733 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base locale = nil if cookies[:locale] locale = cookies[:locale] - elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' + elsif ENV['LOCALIZATION_ENABLED'] == 'enabled' locale = http_accept_language.compatible_language_from(I18n.available_locales) else locale = I18n.default_locale From acb277e6501fab22a13bd1173be723ce51a5199f Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 21 Jan 2021 14:11:17 +0100 Subject: [PATCH 03/11] Use stable_id when replacing tags --- .../concerns/tags_substitution_concern.rb | 33 +++++++++++++++---- app/models/procedure.rb | 24 ++++++++++++++ .../linked_drop_down_list_type_de_champ.rb | 12 +++---- .../types_de_champ/type_de_champ_base.rb | 5 +-- .../concern/tags_substitution_concern_spec.rb | 28 ++++++++++++++++ 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index cb2464c93..e562cfa8c 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -196,12 +196,14 @@ module TagsSubstitutionConcern tags.filter { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) } end - def champ_public_tags - types_de_champ_tags(procedure.types_de_champ, Dossier::SOUMIS) + def champ_public_tags(dossier: nil) + types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ + types_de_champ_tags(types_de_champ, Dossier::SOUMIS) end - def champ_private_tags - types_de_champ_tags(procedure.types_de_champ_private, Dossier::INSTRUCTION_COMMENCEE) + def champ_private_tags(dossier: nil) + types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ_private + types_de_champ_tags(types_de_champ, Dossier::INSTRUCTION_COMMENCEE) end def types_de_champ_tags(types_de_champ, available_for_states) @@ -217,9 +219,11 @@ module TagsSubstitutionConcern return '' end + text = normalize_tags(text) + tags_and_datas = [ - [champ_public_tags, dossier.champs], - [champ_private_tags, dossier.champs_private], + [champ_public_tags(dossier: dossier), dossier.champs], + [champ_private_tags(dossier: dossier), dossier.champs_private], [dossier_tags, dossier], [ROUTAGE_TAGS, dossier], [INDIVIDUAL_TAGS, dossier.individual], @@ -242,7 +246,7 @@ module TagsSubstitutionConcern end def replace_tag(text, tag, data) - libelle = Regexp.quote(tag[:libelle]) + libelle = Regexp.quote(tag[:id] ? tag[:id] : tag[:libelle]) # allow any kind of space (non-breaking or other) in the tag’s libellé to match any kind of space in the template # (the '\\ |' is there because plain ASCII spaces were escaped by preceding Regexp.quote) @@ -256,4 +260,19 @@ module TagsSubstitutionConcern text.gsub(/--#{libelle}--/, value.to_s) end + + def normalize_tags(text) + tags = types_de_champ_tags(procedure.types_de_champ_for_tags, Dossier::SOUMIS) + types_de_champ_tags(procedure.types_de_champ_private_for_tags, Dossier::INSTRUCTION_COMMENCEE) + filter_tags(tags).reduce(text) { |text, tag| normalize_tag(text, tag) } + end + + def normalize_tag(text, tag) + libelle = Regexp.quote(tag[:libelle]) + + # allow any kind of space (non-breaking or other) in the tag’s libellé to match any kind of space in the template + # (the '\\ |' is there because plain ASCII spaces were escaped by preceding Regexp.quote) + libelle.gsub!(/\\ |[[:blank:]]/, "[[:blank:]]") + + text.gsub(/--#{libelle}--/, "--#{tag[:id]}--") + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 0e6a11783..27ceb25c5 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -87,6 +87,30 @@ class Procedure < ApplicationRecord brouillon? ? draft_types_de_champ_private : published_types_de_champ_private end + def types_de_champ_for_tags + if brouillon? + draft_types_de_champ + else + TypeDeChamp.root + .public_only + .where(revision: revisions - [draft_revision]) + .order(:created_at) + .uniq + end + end + + def types_de_champ_private_for_tags + if brouillon? + draft_types_de_champ_private + else + TypeDeChamp.root + .private_only + .where(revision: revisions - [draft_revision]) + .order(:created_at) + .uniq + end + end + def types_de_champ_for_export types_de_champ.reject(&:exclude_from_export?) end diff --git a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb index ca6817c56..5b903e666 100644 --- a/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/linked_drop_down_list_type_de_champ.rb @@ -6,26 +6,24 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas def tags_for_template tags = super - tdc = @type_de_champ + stable_id = @type_de_champ.stable_id tags.push( { libelle: "#{libelle}/primaire", + id: "tdc#{stable_id}/primaire", description: "#{description} (menu primaire)", lambda: -> (champs) { - champs - .find { |champ| champ.type_de_champ == tdc } - &.primary_value + champs.find { |champ| champ.stable_id == stable_id }&.primary_value } } ) tags.push( { libelle: "#{libelle}/secondaire", + id: "tdc#{stable_id}/secondaire", description: "#{description} (menu secondaire)", lambda: -> (champs) { - champs - .find { |champ| champ.type_de_champ == tdc } - &.secondary_value + champs.find { |champ| champ.stable_id == stable_id }&.secondary_value } } ) diff --git a/app/models/types_de_champ/type_de_champ_base.rb b/app/models/types_de_champ/type_de_champ_base.rb index b9d2f3f56..10dbc74fb 100644 --- a/app/models/types_de_champ/type_de_champ_base.rb +++ b/app/models/types_de_champ/type_de_champ_base.rb @@ -8,13 +8,14 @@ class TypesDeChamp::TypeDeChampBase end def tags_for_template - tdc = @type_de_champ + stable_id = @type_de_champ.stable_id [ { libelle: libelle, + id: "tdc#{stable_id}", description: description, lambda: -> (champs) { - champs.find { |champ| champ.type_de_champ == tdc }&.for_tag + champs.find { |champ| champ.stable_id == stable_id }&.for_tag } } ] diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index cbc914d98..3b5404541 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -8,6 +8,7 @@ describe TagsSubstitutionConcern, type: :model do let(:procedure) do create(:procedure, + :published, libelle: 'Une magnifique démarche', types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private, @@ -389,6 +390,33 @@ describe TagsSubstitutionConcern, type: :model do is_expected.to eq('--motivation-- --date de décision--') end end + + context 'when procedure has revisions' do + let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon tag')] } + let(:draft_type_de_champ) { procedure.draft_revision.find_or_clone_type_de_champ(types_de_champ[0].stable_id) } + + before do + draft_type_de_champ.update(libelle: 'ton tag') + dossier.champs.first.update(value: 'valeur') + procedure.update!(draft_revision: procedure.create_new_revision, published_revision: procedure.draft_revision) + end + + context "replace by old label" do + let(:template) { '--mon tag--' } + + it "should replace tag" do + is_expected.to eq('valeur') + end + end + + context "replace by new label" do + let(:template) { '--ton tag--' } + + it "should replace tag" do + is_expected.to eq('valeur') + end + end + end end describe 'tags' do From 179bb5a9fee60900dabde43a7b73dfe404680858 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 May 2021 11:14:02 +0200 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: Pierre de La Morinerie --- app/models/concerns/tags_substitution_concern.rb | 6 +++--- spec/models/concern/tags_substitution_concern_spec.rb | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index e562cfa8c..e9ab51137 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -197,12 +197,12 @@ module TagsSubstitutionConcern end def champ_public_tags(dossier: nil) - types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ + types_de_champ = (dossier || procedure.active_revision).types_de_champ types_de_champ_tags(types_de_champ, Dossier::SOUMIS) end def champ_private_tags(dossier: nil) - types_de_champ = (dossier ? dossier : procedure.active_revision).types_de_champ_private + types_de_champ = (dossier || procedure.active_revision).types_de_champ_private types_de_champ_tags(types_de_champ, Dossier::INSTRUCTION_COMMENCEE) end @@ -246,7 +246,7 @@ module TagsSubstitutionConcern end def replace_tag(text, tag, data) - libelle = Regexp.quote(tag[:id] ? tag[:id] : tag[:libelle]) + libelle = Regexp.quote(tag[:id].presence || tag[:libelle]) # allow any kind of space (non-breaking or other) in the tag’s libellé to match any kind of space in the template # (the '\\ |' is there because plain ASCII spaces were escaped by preceding Regexp.quote) diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index 3b5404541..c9c95ea9c 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -401,18 +401,18 @@ describe TagsSubstitutionConcern, type: :model do procedure.update!(draft_revision: procedure.create_new_revision, published_revision: procedure.draft_revision) end - context "replace by old label" do + context "when using the champ's original label" do let(:template) { '--mon tag--' } - it "should replace tag" do + it "replaces the tag" do is_expected.to eq('valeur') end end - context "replace by new label" do + context "when using the champ's revised label" do let(:template) { '--ton tag--' } - it "should replace tag" do + it "replaces the tag" do is_expected.to eq('valeur') end end From 21ee79669deabbb43d8affdd7a2a250c1f52ec93 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 May 2021 11:29:06 +0200 Subject: [PATCH 05/11] Better labels in test --- spec/models/concern/tags_substitution_concern_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index c9c95ea9c..a8854c45c 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -392,17 +392,17 @@ describe TagsSubstitutionConcern, type: :model do end context 'when procedure has revisions' do - let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon tag')] } + let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon ancien libellé')] } let(:draft_type_de_champ) { procedure.draft_revision.find_or_clone_type_de_champ(types_de_champ[0].stable_id) } before do - draft_type_de_champ.update(libelle: 'ton tag') + draft_type_de_champ.update(libelle: 'mon nouveau libellé') dossier.champs.first.update(value: 'valeur') procedure.update!(draft_revision: procedure.create_new_revision, published_revision: procedure.draft_revision) end context "when using the champ's original label" do - let(:template) { '--mon tag--' } + let(:template) { '--mon ancien libellé--' } it "replaces the tag" do is_expected.to eq('valeur') @@ -410,7 +410,7 @@ describe TagsSubstitutionConcern, type: :model do end context "when using the champ's revised label" do - let(:template) { '--ton tag--' } + let(:template) { '--mon nouveau libellé--' } it "replaces the tag" do is_expected.to eq('valeur') From e85dcf4c58b008e2c6a2da3bb68a24bc14144955 Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:32:26 +0200 Subject: [PATCH 06/11] Update app/controllers/application_controller.rb Co-authored-by: Pierre de La Morinerie --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ce5dc733..c23773bd8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base locale = nil if cookies[:locale] locale = cookies[:locale] - elsif ENV['LOCALIZATION_ENABLED'] == 'enabled' + elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' locale = http_accept_language.compatible_language_from(I18n.available_locales) else locale = I18n.default_locale From 355616e9a76e92792ea89cad8905656ec39bd2c4 Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:36:32 +0200 Subject: [PATCH 07/11] Update app/views/layouts/_header.haml Co-authored-by: Pierre de La Morinerie --- app/views/layouts/_header.haml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/_header.haml b/app/views/layouts/_header.haml index 0ab8116d8..9ac9fe22d 100644 --- a/app/views/layouts/_header.haml +++ b/app/views/layouts/_header.haml @@ -82,5 +82,6 @@ - else = render partial: 'shared/help/help_button' - %li - = render partial: 'layouts/locale_dropdown' + - if ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' + %li + = render partial: 'layouts/locale_dropdown' From 884e52326e49e5b997b5ed6e36231412b49921ea Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:36:50 +0200 Subject: [PATCH 08/11] Update app/views/layouts/_locale_dropdown.html.haml Co-authored-by: Pierre de La Morinerie --- app/views/layouts/_locale_dropdown.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/_locale_dropdown.html.haml b/app/views/layouts/_locale_dropdown.html.haml index d05429745..b9a12432f 100644 --- a/app/views/layouts/_locale_dropdown.html.haml +++ b/app/views/layouts/_locale_dropdown.html.haml @@ -2,7 +2,7 @@ %button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' } .hidden Translate = image_tag "icons/translate-icon.svg", alt: 'Translate', 'aria-hidden':'true' - %ul.header-menu.dropdown-content#mon_compte_menu + %ul.header-menu.dropdown-content %li = link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do EN - English From 18ce027b8485cc841ba31fe0ea66d6163c369e70 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 May 2021 12:59:11 +0200 Subject: [PATCH 09/11] Update dossier serialize query to stop using deprecated fileds --- app/graphql/schema.graphql | 4 +- app/graphql/types/geo_area_type.rb | 1 + .../geo_areas/selection_utilisateur_type.rb | 2 - app/services/serializer_service.rb | 100 ++++++++++-------- 4 files changed, 57 insertions(+), 50 deletions(-) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index d2c597811..2dce3fbd5 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1139,6 +1139,7 @@ type File { } interface GeoArea { + description: String geometry: GeoJSON! id: ID! source: GeoAreaSource! @@ -1471,6 +1472,7 @@ type ParcelleCadastrale implements GeoArea { codeCom: String! @deprecated(reason: "Utilisez le champ `commune` à la place.") codeDep: String! @deprecated(reason: "Utilisez le champ `commune` à la place.") commune: String! + description: String feuille: Int! @deprecated(reason: "L‘information n‘est plus disponible.") geometry: GeoJSON! id: ID! @@ -1590,7 +1592,7 @@ type Revision { } type SelectionUtilisateur implements GeoArea { - description: String! + description: String geometry: GeoJSON! id: ID! source: GeoAreaSource! diff --git a/app/graphql/types/geo_area_type.rb b/app/graphql/types/geo_area_type.rb index 9a824b031..5827b3576 100644 --- a/app/graphql/types/geo_area_type.rb +++ b/app/graphql/types/geo_area_type.rb @@ -13,6 +13,7 @@ module Types global_id_field :id field :source, GeoAreaSource, null: false field :geometry, Types::GeoJSON, null: false, method: :safe_geometry + field :description, String, null: true definition_methods do def resolve_type(object, context) diff --git a/app/graphql/types/geo_areas/selection_utilisateur_type.rb b/app/graphql/types/geo_areas/selection_utilisateur_type.rb index 6de50ab73..004f07583 100644 --- a/app/graphql/types/geo_areas/selection_utilisateur_type.rb +++ b/app/graphql/types/geo_areas/selection_utilisateur_type.rb @@ -1,7 +1,5 @@ module Types::GeoAreas class SelectionUtilisateurType < Types::BaseObject implements Types::GeoAreaType - - field :description, String, null: false end end diff --git a/app/services/serializer_service.rb b/app/services/serializer_service.rb index ef501d8e6..04d816f3c 100644 --- a/app/services/serializer_service.rb +++ b/app/services/serializer_service.rb @@ -48,7 +48,9 @@ class SerializerService query serializeChamp($number: Int!, $id: ID!) { dossier(number: $number) { champs(id: $id) { - ...RootChampFragment + ...ChampFragment + ...RepetitionChampFragment + ...CarteChampFragment } } } @@ -56,7 +58,9 @@ class SerializerService query serializeAnnotation($number: Int!, $id: ID!) { dossier(number: $number) { annotations(id: $id) { - ...RootChampFragment + ...ChampFragment + ...RepetitionChampFragment + ...CarteChampFragment } } } @@ -77,29 +81,28 @@ class SerializerService label } champs { - ...RootChampFragment + ...ChampFragment + ...RepetitionChampFragment + ...CarteChampFragment } annotations { - ...RootChampFragment + ...ChampFragment + ...RepetitionChampFragment + ...CarteChampFragment } avis { ...AvisFragment } demandeur { - ... on PersonnePhysique { - civilite - nom - prenom - dateDeNaissance - } + ...PersonnePhysiqueFragment ...PersonneMoraleFragment } motivation motivationAttachment { - byteSize - checksum - filename - contentType + ...FileFragment + } + revision { + id } } @@ -116,10 +119,7 @@ class SerializerService email } attachment { - byteSize - checksum - filename - contentType + ...FileFragment } } @@ -141,10 +141,7 @@ class SerializerService } ... on PieceJustificativeChamp { file { - byteSize - checksum - filename - contentType + ...FileFragment } } ... on AddressChamp { @@ -154,35 +151,37 @@ class SerializerService } } - fragment RootChampFragment on Champ { - ...ChampFragment - ... on RepetitionChamp { - champs { - ...ChampFragment - } - } - ... on CarteChamp { - geoAreas { - source - geometry { - type - coordinates - } - ... on ParcelleCadastrale { - codeArr - codeCom - codeDep - feuille - nomCom - numero - section - surfaceParcelle - surfaceIntersection - } + fragment RepetitionChampFragment on RepetitionChamp { + champs { + ...ChampFragment + } + } + + fragment CarteChampFragment on CarteChamp { + geoAreas { + source + description + geometry { + type + coordinates + } + ... on ParcelleCadastrale { + prefixe + numero + commune + section + surface } } } + fragment PersonnePhysiqueFragment on PersonnePhysique { + civilite + nom + prenom + dateDeNaissance + } + fragment PersonneMoraleFragment on PersonneMorale { siret siegeSocial @@ -229,5 +228,12 @@ class SerializerService regionName regionCode } + + fragment FileFragment on File { + filename + checksum + byteSize + contentType + } GRAPHQL end From dfc46a8736f3aae8d355693b99f7e84383fccf25 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 May 2021 16:10:01 +0200 Subject: [PATCH 10/11] 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 } From 4922bc88d6f61640df87980687ab51108be6d0e7 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 26 May 2021 12:22:35 +0200 Subject: [PATCH 11/11] Do not crash when properties is nil --- app/models/geo_area.rb | 2 +- spec/models/geo_area_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index b8dcd31fe..ff57fbb9c 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -22,7 +22,7 @@ class GeoArea < ApplicationRecord if value.is_a? String ActiveRecord::Coders::YAMLColumn.new(:properties).load(value) else - value + value || {} end end diff --git a/spec/models/geo_area_spec.rb b/spec/models/geo_area_spec.rb index ac3e1aab2..dfb5aa10e 100644 --- a/spec/models/geo_area_spec.rb +++ b/spec/models/geo_area_spec.rb @@ -100,4 +100,12 @@ RSpec.describe GeoArea, type: :model do end end end + + describe 'description' do + context 'when properties is nil' do + let(:geo_area) { build(:geo_area, properties: nil) } + + it { expect(geo_area.description).to be_nil } + end + end end