-
Notifications
You must be signed in to change notification settings - Fork 106
ETQ usager, je peux personnaliser la liste des dossiers (3/3 - valeurs sur les cartes) #13384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kara22
wants to merge
4
commits into
feat/dossiers-personnalisation-pr2
Choose a base branch
from
feat/dossiers-personnalisation-pr3
base: feat/dossiers-personnalisation-pr2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−35
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d14558
refactor(personnalisation): extract column value formatting into a sh…
kara22 fa288e7
feat(personnalisation): add dossier card champs component
kara22 9b03b55
feat(personnalisation): display chosen champ values on the dossiers list
kara22 0852fc0
test(personnalisation): cover champ values displayed on dossier cards
kara22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Users::DossierCardChampsComponent < ApplicationComponent | ||
| Line = Data.define(:label, :value) | ||
|
|
||
| def initialize(columns:, champs_by_stable_id:) | ||
| @columns = columns | ||
| @champs_by_stable_id = champs_by_stable_id | ||
| end | ||
|
|
||
| def render? = lines.any? | ||
|
|
||
| private | ||
|
|
||
| attr_reader :columns, :champs_by_stable_id | ||
|
|
||
| def lines | ||
| @lines ||= columns.filter_map do |column| | ||
| next unless column.champ_column? | ||
|
|
||
| champ = champs_by_stable_id[column.stable_id] | ||
| raw = column.value(champ) | ||
| formatted = ColumnValueFormatter.format(column:, raw_value: raw) | ||
| next if formatted.blank? | ||
|
|
||
| Line.new(label: column.label, value: formatted) | ||
| end | ||
| end | ||
| end |
2 changes: 2 additions & 0 deletions
2
app/components/users/dossier_card_champs_component/dossier_card_champs_component.en.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| en: | ||
| label: "%{champ}:" |
2 changes: 2 additions & 0 deletions
2
app/components/users/dossier_card_champs_component/dossier_card_champs_component.fr.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| fr: | ||
| label: "%{champ} :" |
6 changes: 6 additions & 0 deletions
6
app/components/users/dossier_card_champs_component/dossier_card_champs_component.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <% lines.each do |line| %> | ||
| <p class="fr-icon--sm fr-icon-info-i fr-mb-0"> | ||
| <span class="fr-sr-only"><%= t(".label", champ: line.label) %></span> | ||
| <%= line.value %> | ||
| </p> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ColumnValueFormatter | ||
| module_function | ||
|
|
||
| def format(column:, raw_value:) | ||
| return if raw_value.nil? | ||
|
|
||
| case column.type | ||
| when :boolean | ||
| if column.type_de_champ? && column.tdc_type == 'checkbox' | ||
| raw_value ? I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_true') : '' | ||
| else | ||
| raw_value ? I18n.t('utils.yes') : I18n.t('utils.no') | ||
| end | ||
| when :attachments | ||
| raw_value.present? ? 'présent' : 'absent' | ||
| when :enum | ||
| format_enum(column:, raw_value:) | ||
| when :enums | ||
| format_enums(column:, raw_values: raw_value) | ||
| when :date | ||
| raw_value = Date.parse(raw_value) if raw_value.is_a?(String) | ||
| I18n.l(raw_value, format: :short) | ||
| when :datetime | ||
| raw_value = DateTime.parse(raw_value) if raw_value.is_a?(String) | ||
| I18n.l(raw_value, format: :short_with_time) | ||
| else | ||
| raw_value.html_safe? ? raw_value : ERB::Util.html_escape(raw_value.to_s) | ||
| end | ||
| end | ||
|
|
||
| def format_enums(column:, raw_values:) | ||
| ActionController::Base.helpers.safe_join( | ||
| raw_values.map { |v| format_enum(column:, raw_value: v) }, | ||
| ', ' | ||
| ) | ||
| end | ||
|
|
||
| def format_enum(column:, raw_value:) | ||
| column.label_for_value(raw_value) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spec/components/users/dossier_card_champs_component_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Users::DossierCardChampsComponent, type: :component do | ||
| let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :text, libelle: 'Titre' }]) } | ||
| let(:dossier) { create(:dossier, :en_construction, procedure:, populate_champs: true) } | ||
| let(:column) { procedure.personnalisable_columns.first } | ||
|
|
||
| it 'renders the formatted value with an info icon when the champ has a value' do | ||
| champ = dossier.champs.find { _1.stable_id == column.stable_id } | ||
| champ.update(value: 'Presse Océan') | ||
|
|
||
| render_inline(described_class.new(columns: [column], champs_by_stable_id: { column.stable_id => champ.reload })) | ||
|
|
||
| expect(page).to have_css('.fr-icon-info-i') | ||
| expect(page).to have_text('Presse Océan') | ||
| end | ||
|
|
||
| it 'renders the formatted value for a brouillon dossier' do | ||
| brouillon = create(:dossier, :brouillon, procedure:, populate_champs: true) | ||
| champ = brouillon.champs.find { _1.stable_id == column.stable_id } | ||
| champ.update(value: 'Valeur brouillon') | ||
|
|
||
| render_inline(described_class.new(columns: [column], champs_by_stable_id: { column.stable_id => champ.reload })) | ||
|
|
||
| expect(page).to have_css('.fr-icon-info-i') | ||
| expect(page).to have_text('Valeur brouillon') | ||
| end | ||
|
|
||
| it 'renders nothing when the champ is missing or empty' do | ||
| render_inline(described_class.new(columns: [column], champs_by_stable_id: {})) | ||
|
|
||
| expect(page).not_to have_css('.fr-icon-info-i') | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe ColumnValueFormatter do | ||
| let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :text, libelle: 'Ville' }, { type: :date, libelle: 'Date' }]) } | ||
| let(:text_column) { procedure.columns.find { |c| c.respond_to?(:tdc_type) && c.tdc_type == 'text' } } | ||
| let(:date_column) { procedure.columns.find { |c| c.respond_to?(:tdc_type) && c.tdc_type == 'date' } } | ||
|
|
||
| it 'formats a date with the short format' do | ||
| expect(described_class.format(column: date_column, raw_value: Date.new(2026, 3, 18))).to eq(I18n.l(Date.new(2026, 3, 18), format: :short)) | ||
| end | ||
|
|
||
| it 'escapes and returns text values' do | ||
| expect(described_class.format(column: text_column, raw_value: 'Presse Océan')).to eq('Presse Océan') | ||
| end | ||
|
|
||
| it 'returns nil when raw_value is nil' do | ||
| expect(described_class.format(column: text_column, raw_value: nil)).to be_nil | ||
| end | ||
|
|
||
| describe 'format_enums' do | ||
| let(:enums_column) { double('column', type: :enums) } | ||
|
|
||
| it 'returns an html_safe SafeBuffer and escapes HTML chars in labels' do | ||
| allow(enums_column).to receive(:label_for_value).with('a').and_return('A & B') | ||
| allow(enums_column).to receive(:label_for_value).with('b').and_return('C') | ||
|
|
||
| result = described_class.format(column: enums_column, raw_value: ['a', 'b']) | ||
|
|
||
| expect(result).to be_html_safe | ||
| expect(result).to include('A & B') | ||
| expect(result).to include('C') | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quitte à l'extraire ce serait plutôt dans app/services