refactor(Commentarie.soft_delete): use discard
This commit is contained in:
parent
988fe34d0a
commit
0aecc301c9
10 changed files with 24 additions and 26 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
class AddDeletedAtToCommentaires < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :commentaires, :deleted_at, :datetime
|
||||
end
|
||||
end
|
|
@ -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
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue