messagerie: better format message dates

This commit is contained in:
Pierre de La Morinerie 2018-09-06 13:04:43 +00:00 committed by gregoirenovel
parent fe13871380
commit 211674435e
5 changed files with 57 additions and 1 deletions

View file

@ -4,4 +4,10 @@ module CommentaireHelper
"from-me"
end
end
def commentaire_date(commentaire)
is_current_year = (commentaire.created_at.year == Date.current.year)
template = is_current_year ? :message_date : :message_date_with_year
I18n.l(commentaire.created_at.localtime, format: template)
end
end

View file

@ -7,7 +7,7 @@
- if ![user_email, commentaire.dossier.user.email, OLD_CONTACT_EMAIL, CONTACT_EMAIL].include?(commentaire.email)
%span.guest Invité
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
= I18n.l(commentaire.created_at.localtime, format: '%d/%m/%Y à %H:%M ')
= commentaire_date(commentaire)
.rich-text= sanitize(commentaire.body)
- if commentaire.piece_justificative

View file

@ -0,0 +1,10 @@
{
:en => {
:time => {
:formats => {
:message_date => lambda { |time, _| "%B #{time.day.ordinalize} at %H:%M" },
:message_date_with_year => lambda { |time, _| "%B #{time.day.ordinalize} %Y at %H:%M" }
}
}
}
}

View file

@ -0,0 +1,10 @@
{
:fr => {
:time => {
:formats => {
:message_date => lambda { |time, _| "le #{time.day == 1 ? '1er' : time.day} %B à %H h %M" },
:message_date_with_year => lambda { |time, _| "le #{time.day == 1 ? '1er' : time.day} %B %Y à %H h %M" }
}
}
}
}

View file

@ -18,4 +18,34 @@ RSpec.describe CommentaireHelper, type: :helper do
it { is_expected.to eq nil }
end
end
describe '.commentaire_date' do
let(:present_date) { Time.local(2018, 9, 2, 10, 5, 0) }
let(:creation_date) { present_date }
let(:commentaire) do
Timecop.freeze(creation_date) { create(:commentaire, email: "michel@pref.fr") }
end
subject do
Timecop.freeze(present_date) { commentaire_date(commentaire) }
end
it 'doesnt include the creation year' do
expect(subject).to eq 'le 2 septembre à 10 h 05'
end
context 'when displaying a commentaire created on a previous year' do
let(:creation_date) { present_date.prev_year }
it 'includes the creation year' do
expect(subject).to eq 'le 2 septembre 2017 à 10 h 05'
end
end
context 'when formatting the first day of the month' do
let(:present_date) { Time.local(2018, 9, 1, 10, 5, 0) }
it 'includes the ordinal' do
expect(subject).to eq 'le 1er septembre à 10 h 05'
end
end
end
end