diff --git a/app/helpers/commentaire_helper.rb b/app/helpers/commentaire_helper.rb index e4f457dfb..2518c8506 100644 --- a/app/helpers/commentaire_helper.rb +++ b/app/helpers/commentaire_helper.rb @@ -24,6 +24,7 @@ module CommentaireHelper end def pretty_commentaire(commentaire) + return t('views.shared.commentaires.destroy.deleted_body') if commentaire.discarded? body_formatted = commentaire.sent_by_system? ? commentaire.body : simple_format(commentaire.body) sanitize(body_formatted) end diff --git a/app/jobs/notify_new_answer_with_delay_job.rb b/app/jobs/notify_new_answer_with_delay_job.rb index a633b5709..60a0e3e6a 100644 --- a/app/jobs/notify_new_answer_with_delay_job.rb +++ b/app/jobs/notify_new_answer_with_delay_job.rb @@ -4,7 +4,7 @@ class NotifyNewAnswerWithDelayJob < ApplicationJob discard_on ActiveRecord::RecordNotFound def perform(dossier, body, commentaire) - return if commentaire.soft_deleted? + return if commentaire.discarded? DossierMailer.notify_new_answer(dossier, body).deliver_now end end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 27cec3258..13c09345f 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,7 +4,7 @@ # # id :integer not null, primary key # body :string -# deleted_at :datetime +# discarded_at :datetime # email :string # created_at :datetime not null # updated_at :datetime not null @@ -14,6 +14,7 @@ # class Commentaire < ApplicationRecord include FileValidationConcern + include Discard::Model self.ignored_columns = [:user_id] belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false @@ -47,10 +48,6 @@ class Commentaire < ApplicationRecord end end - def soft_deleted? - !!deleted_at - end - def header "#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}" end @@ -84,7 +81,7 @@ class Commentaire < ApplicationRecord end def soft_deletable?(connected_user) - sent_by?(connected_user) && sent_by_instructeur? && !soft_deleted? + sent_by?(connected_user) && sent_by_instructeur? && !discarded? end def file_url diff --git a/app/services/commentaire_service.rb b/app/services/commentaire_service.rb index 7ea4233c8..224323af7 100644 --- a/app/services/commentaire_service.rb +++ b/app/services/commentaire_service.rb @@ -27,9 +27,8 @@ class CommentaireService .find(params[:id]) if commentaire.sent_by?(user) commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached? - commentaire.update!(body: I18n.t('views.shared.commentaires.destroy.deleted_body'), - deleted_at: Time.zone.now) - OpenStruct.new(status: true) + commentaire.body = '' + OpenStruct.new(status: commentaire.discard, error_message: commentaire.errors.full_messages.join(', ')) else OpenStruct.new(status: false, error_message: I18n.t('views.shared.commentaires.destroy.alert_reasons.acl')) diff --git a/db/migrate/20211115112933_add_deleted_at_to_commentaires.rb b/db/migrate/20211115112933_add_deleted_at_to_commentaires.rb deleted file mode 100644 index 1e48f5b89..000000000 --- a/db/migrate/20211115112933_add_deleted_at_to_commentaires.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddDeletedAtToCommentaires < ActiveRecord::Migration[6.1] - def change - add_column :commentaires, :deleted_at, :datetime - end -end diff --git a/db/migrate/20211115112933_add_discarded_at_to_commentaires.rb b/db/migrate/20211115112933_add_discarded_at_to_commentaires.rb new file mode 100644 index 000000000..ff01d319f --- /dev/null +++ b/db/migrate/20211115112933_add_discarded_at_to_commentaires.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToCommentaires < ActiveRecord::Migration[6.1] + def change + add_column :commentaires, :discarded_at, :datetime + # add_index :commentaires, :discarded_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 81c23a529..58b08f7f5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -219,7 +219,7 @@ ActiveRecord::Schema.define(version: 2021_11_15_112933) do t.bigint "user_id" t.bigint "instructeur_id" t.bigint "expert_id" - t.datetime "deleted_at" + t.datetime "discarded_at" t.index ["dossier_id"], name: "index_commentaires_on_dossier_id" t.index ["expert_id"], name: "index_commentaires_on_expert_id" t.index ["instructeur_id"], name: "index_commentaires_on_instructeur_id" diff --git a/spec/jobs/notify_new_answer_with_delay_job_spec.rb b/spec/jobs/notify_new_answer_with_delay_job_spec.rb index 1a87ab841..dd2235131 100644 --- a/spec/jobs/notify_new_answer_with_delay_job_spec.rb +++ b/spec/jobs/notify_new_answer_with_delay_job_spec.rb @@ -2,7 +2,7 @@ describe NotifyNewAnswerWithDelayJob, type: :job do let(:dossier) { double } let(:body) { "bim un body" } - context 'when commentaire not soft_deleted?' do + context 'when commentaire not discarded?' do let(:commentaire) { create(:commentaire) } it 'call DossierMailer.notify_new_answer' do @@ -11,8 +11,8 @@ describe NotifyNewAnswerWithDelayJob, type: :job do end end - context 'when commentaire is soft_deleted?' do - let(:commentaire) { create(:commentaire, deleted_at: 2.hours.ago) } + context 'when commentaire is discarded?' do + let(:commentaire) { create(:commentaire, discarded_at: 2.hours.ago) } it 'skips DossierMailer.notify_new_anser' do expect(DossierMailer).not_to receive(:notify_new_answer).with(dossier, body) diff --git a/spec/services/commentaire_service_spec.rb b/spec/services/commentaire_service_spec.rb index 2f881d0c6..7d4d03c34 100644 --- a/spec/services/commentaire_service_spec.rb +++ b/spec/services/commentaire_service_spec.rb @@ -96,15 +96,14 @@ describe CommentaireService do expect(subject.status).to eq(true) end it 'sets commentaire.body to deleted message' do - allow(commentaire.piece_jointe).to receive(:purge_later) - expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("Message supprimé") + expect_any_instance_of(ActiveStorage::Attached::One).to receive(:purge_later) + subject end it 'sets commentaire.body to deleted message' do - expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("Message supprimé") + expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("") end it 'sets deleted_at' do - subject - expect(commentaire.reload.deleted_at).not_to be_nil + expect {subject }.to change { commentaire.reload.discarded?}.from(false).to(true) end end end diff --git a/spec/views/shared/dossiers/messages/message.html.haml_spec.rb b/spec/views/shared/dossiers/messages/message.html.haml_spec.rb index 5828b3611..00744a27a 100644 --- a/spec/views/shared/dossiers/messages/message.html.haml_spec.rb +++ b/spec/views/shared/dossiers/messages/message.html.haml_spec.rb @@ -60,10 +60,11 @@ describe 'shared/dossiers/messages/message.html.haml', type: :view do it { is_expected.to have_selector("form[action=\"#{form_url}\"]") } end - context 'on a procedure where commentaire had been written by connected instructeur and deleted' do - let(:commentaire) { create(:commentaire, instructeur: instructeur, body: 'Second message', deleted_at: 2.days.ago) } + context 'on a procedure where commentaire had been written by connected instructeur and discarded' do + let(:commentaire) { create(:commentaire, instructeur: instructeur, body: 'Second message', discarded_at: 2.days.ago) } it { is_expected.not_to have_selector("form[action=\"#{form_url}\"]") } + it { is_expected.not_to have_selector(".rich-text", text: I18n.t(t('views.shared.commentaires.destroy.deleted_body'))) } end context 'on a procedure where commentaire had been written by connected an user' do