Skip to content

Commit

Permalink
refactor(attachment): simplify find champ
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Dec 16, 2024
1 parent 3ee386e commit c989738
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/components/attachment/edit_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def attachment_path(**args)

def destroy_attachment_path
if champ.present?
attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id)
attachment_path
else
attachment_path(auto_attach_url: @auto_attach_url, view_as: @view_as, direct_upload: @direct_upload)
end
Expand Down
26 changes: 18 additions & 8 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def show

def destroy
@attachment = @blob.attachments.find(params[:id])
@attachment.purge_later
flash.notice = 'La pièce jointe a bien été supprimée.'

if params[:dossier_id]
@champ = find_champ
if champ?
@attachment = champ.piece_justificative_file.find { _1.blob.id == @blob.id }
else
@attachment_options = attachment_options
end

@attachment.purge_later
flash.notice = 'La pièce jointe a bien été supprimée.'

respond_to do |format|
format.turbo_stream
format.html { redirect_back(fallback_location: root_url) }
Expand All @@ -37,14 +38,23 @@ def destroy

private

def find_champ
dossier = policy_scope(Dossier).includes(:champs).find(params[:dossier_id])
dossier.champs.find_by(stable_id: params[:stable_id], row_id: params[:row_id])
def record
@attachment.record
end

def champ?
record.is_a?(Champ)
end

def champ
@champ ||= if champ?
record.dossier.champ_for_update(record.type_de_champ, row_id: record.row_id, updated_by: current_user.email)
end
end

def attachment_options
{
attached_file: @attachment.record.public_send(@attachment.name),
attached_file: record.public_send(@attachment.name),
view_as: params[:view_as]&.to_sym,
direct_upload: params[:direct_upload] == "true",
auto_attach_url: params[:direct_upload] == "true" ? params[:auto_attach_url] : nil
Expand Down

0 comments on commit c989738

Please sign in to comment.