From e873b6a9c4c80f563e1e83041fd79b0ab8e45cd8 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 3 Dec 2018 12:49:03 +0100 Subject: [PATCH] Expose selection utilisateur size on champ carte --- app/models/champs/carte_champ.rb | 8 +++++ .../shared/champs/carte/_geo_areas.html.haml | 6 ++-- spec/models/champs/carte_champ_spec.rb | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index 8e5c6bb3d..c7631b1ad 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -62,6 +62,14 @@ class Champs::CarteChamp < Champ end end + def selection_utilisateur_size + if geo_json.present? + geo_json['coordinates'].size + else + 0 + end + end + def to_render_data { position: position, diff --git a/app/views/shared/champs/carte/_geo_areas.html.haml b/app/views/shared/champs/carte/_geo_areas.html.haml index 03d02e6e8..b2a990f8d 100644 --- a/app/views/shared/champs/carte/_geo_areas.html.haml +++ b/app/views/shared/champs/carte/_geo_areas.html.haml @@ -6,7 +6,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.quartiers_prioritaires.blank? - = t('errors.messages.quartiers_prioritaires_empty', count: champ.geo_json.size) + = t('errors.messages.quartiers_prioritaires_empty', count: champ.selection_utilisateur_size) - else %ul - champ.quartiers_prioritaires.each do |qp| @@ -20,7 +20,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.cadastres.blank? - = t('errors.messages.cadastres_empty', count: champ.geo_json.size) + = t('errors.messages.cadastres_empty', count: champ.selection_utilisateur_size) - else %ul - champ.cadastres.each do |pc| @@ -34,7 +34,7 @@ - elsif champ.value.blank? Aucune zone tracée - elsif champ.parcelles_agricoles.blank? - = t('errors.messages.parcelles_agricoles_empty', count: champ.geo_json.size) + = t('errors.messages.parcelles_agricoles_empty', count: champ.selection_utilisateur_size) - else %ul - champ.parcelles_agricoles.each do |pa| diff --git a/spec/models/champs/carte_champ_spec.rb b/spec/models/champs/carte_champ_spec.rb index 7c8c6f0cf..8d0875a88 100644 --- a/spec/models/champs/carte_champ_spec.rb +++ b/spec/models/champs/carte_champ_spec.rb @@ -60,4 +60,38 @@ describe Champs::CarteChamp do it { is_expected.to eq(render_data) } end end + + describe '#selection_utilisateur_size' do + subject { champ.selection_utilisateur_size } + + context 'when the value is nil' do + let(:value) { nil } + + it { is_expected.to eq(0) } + end + + context 'when the value is blank' do + let(:value) { '' } + + it { is_expected.to eq(0) } + end + + context 'when the value is empty array' do + let(:value) { '[]' } + + it { is_expected.to eq(0) } + end + + context 'when the value is coordinates' do + let(:value) { coordinates.to_json } + + it { is_expected.to eq(1) } + end + + context 'when the value is geojson' do + let(:value) { geo_json } + + it { is_expected.to eq(1) } + end + end end