refactor(Commentarie.soft_delete): use discard

This commit is contained in:
Martin 2021-11-16 14:28:38 +01:00
parent 988fe34d0a
commit 0aecc301c9
10 changed files with 24 additions and 26 deletions

View file

@ -24,6 +24,7 @@ module CommentaireHelper
end end
def pretty_commentaire(commentaire) 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) body_formatted = commentaire.sent_by_system? ? commentaire.body : simple_format(commentaire.body)
sanitize(body_formatted) sanitize(body_formatted)
end end

View file

@ -4,7 +4,7 @@ class NotifyNewAnswerWithDelayJob < ApplicationJob
discard_on ActiveRecord::RecordNotFound discard_on ActiveRecord::RecordNotFound
def perform(dossier, body, commentaire) def perform(dossier, body, commentaire)
return if commentaire.soft_deleted? return if commentaire.discarded?
DossierMailer.notify_new_answer(dossier, body).deliver_now DossierMailer.notify_new_answer(dossier, body).deliver_now
end end
end end

View file

@ -4,7 +4,7 @@
# #
# id :integer not null, primary key # id :integer not null, primary key
# body :string # body :string
# deleted_at :datetime # discarded_at :datetime
# email :string # email :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
@ -14,6 +14,7 @@
# #
class Commentaire < ApplicationRecord class Commentaire < ApplicationRecord
include FileValidationConcern include FileValidationConcern
include Discard::Model
self.ignored_columns = [:user_id] self.ignored_columns = [:user_id]
belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false belongs_to :dossier, inverse_of: :commentaires, touch: true, optional: false
@ -47,10 +48,6 @@ class Commentaire < ApplicationRecord
end end
end end
def soft_deleted?
!!deleted_at
end
def header def header
"#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}" "#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
end end
@ -84,7 +81,7 @@ class Commentaire < ApplicationRecord
end end
def soft_deletable?(connected_user) def soft_deletable?(connected_user)
sent_by?(connected_user) && sent_by_instructeur? && !soft_deleted? sent_by?(connected_user) && sent_by_instructeur? && !discarded?
end end
def file_url def file_url

View file

@ -27,9 +27,8 @@ class CommentaireService
.find(params[:id]) .find(params[:id])
if commentaire.sent_by?(user) if commentaire.sent_by?(user)
commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached? commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached?
commentaire.update!(body: I18n.t('views.shared.commentaires.destroy.deleted_body'), commentaire.body = ''
deleted_at: Time.zone.now) OpenStruct.new(status: commentaire.discard, error_message: commentaire.errors.full_messages.join(', '))
OpenStruct.new(status: true)
else else
OpenStruct.new(status: false, OpenStruct.new(status: false,
error_message: I18n.t('views.shared.commentaires.destroy.alert_reasons.acl')) error_message: I18n.t('views.shared.commentaires.destroy.alert_reasons.acl'))

View file

@ -1,5 +0,0 @@
class AddDeletedAtToCommentaires < ActiveRecord::Migration[6.1]
def change
add_column :commentaires, :deleted_at, :datetime
end
end

View file

@ -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

View file

@ -219,7 +219,7 @@ ActiveRecord::Schema.define(version: 2021_11_15_112933) do
t.bigint "user_id" t.bigint "user_id"
t.bigint "instructeur_id" t.bigint "instructeur_id"
t.bigint "expert_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 ["dossier_id"], name: "index_commentaires_on_dossier_id"
t.index ["expert_id"], name: "index_commentaires_on_expert_id" t.index ["expert_id"], name: "index_commentaires_on_expert_id"
t.index ["instructeur_id"], name: "index_commentaires_on_instructeur_id" t.index ["instructeur_id"], name: "index_commentaires_on_instructeur_id"

View file

@ -2,7 +2,7 @@ describe NotifyNewAnswerWithDelayJob, type: :job do
let(:dossier) { double } let(:dossier) { double }
let(:body) { "bim un body" } let(:body) { "bim un body" }
context 'when commentaire not soft_deleted?' do context 'when commentaire not discarded?' do
let(:commentaire) { create(:commentaire) } let(:commentaire) { create(:commentaire) }
it 'call DossierMailer.notify_new_answer' do it 'call DossierMailer.notify_new_answer' do
@ -11,8 +11,8 @@ describe NotifyNewAnswerWithDelayJob, type: :job do
end end
end end
context 'when commentaire is soft_deleted?' do context 'when commentaire is discarded?' do
let(:commentaire) { create(:commentaire, deleted_at: 2.hours.ago) } let(:commentaire) { create(:commentaire, discarded_at: 2.hours.ago) }
it 'skips DossierMailer.notify_new_anser' do it 'skips DossierMailer.notify_new_anser' do
expect(DossierMailer).not_to receive(:notify_new_answer).with(dossier, body) expect(DossierMailer).not_to receive(:notify_new_answer).with(dossier, body)

View file

@ -96,15 +96,14 @@ describe CommentaireService do
expect(subject.status).to eq(true) expect(subject.status).to eq(true)
end end
it 'sets commentaire.body to deleted message' do it 'sets commentaire.body to deleted message' do
allow(commentaire.piece_jointe).to receive(:purge_later) expect_any_instance_of(ActiveStorage::Attached::One).to receive(:purge_later)
expect { subject }.to change { commentaire.reload.body }.from(an_instance_of(String)).to("Message supprimé") subject
end end
it 'sets commentaire.body to deleted message' do 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 end
it 'sets deleted_at' do it 'sets deleted_at' do
subject expect {subject }.to change { commentaire.reload.discarded?}.from(false).to(true)
expect(commentaire.reload.deleted_at).not_to be_nil
end end
end end
end end

View file

@ -60,10 +60,11 @@ describe 'shared/dossiers/messages/message.html.haml', type: :view do
it { is_expected.to have_selector("form[action=\"#{form_url}\"]") } it { is_expected.to have_selector("form[action=\"#{form_url}\"]") }
end end
context 'on a procedure where commentaire had been written by connected instructeur and deleted' do context 'on a procedure where commentaire had been written by connected instructeur and discarded' do
let(:commentaire) { create(:commentaire, instructeur: instructeur, body: 'Second message', deleted_at: 2.days.ago) } 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("form[action=\"#{form_url}\"]") }
it { is_expected.not_to have_selector(".rich-text", text: I18n.t(t('views.shared.commentaires.destroy.deleted_body'))) }
end end
context 'on a procedure where commentaire had been written by connected an user' do context 'on a procedure where commentaire had been written by connected an user' do