Active Storage削除処理のリファクタリング

before

def destroy
  @site.favicon.purge if params[:status] == 'favicon'
  @site.og_image.purge if params[:status] == 'og_image'
  @site.main_images.find(params[:id]).purge if params[:status] == 'main_images'
  redirect_to edit_admin_site_path
end


after

def destroy
  image = ActiveStorage::Attachment.find(params[:id])
  image.purge
  redirect_to edit_admin_site_path
end


条件分岐せずとも各idは一意なのでActiveStorage::Attachmentから取得すれば一行で済む。