From 86e441eebde24ff42fccd3e5806dd120981969a8 Mon Sep 17 00:00:00 2001 From: Bilal Date: Wed, 22 Jul 2020 18:16:14 +0200 Subject: [PATCH 1/4] add delete button to the files in broadcast page --- app/channels/broadcasts_channel.rb | 2 +- app/controllers/broadcasts_controller.rb | 22 +++++++--- app/policies/broadcast_policy.rb | 4 ++ app/views/broadcasts/_file.html.erb | 31 ++++++++++---- app/views/broadcasts/_files_section.html.erb | 2 +- app/views/broadcasts/_update_file_list.js.erb | 9 ++++ app/views/broadcasts/destroy_file.js.erb | 1 + app/views/broadcasts/update.js.erb | 10 +---- config/locales/en.yml | 4 ++ config/locales/es.yml | 4 ++ config/routes.rb | 3 ++ spec/factories/broadcasts.rb | 8 +++- .../features/user_managing_broadcasts_spec.rb | 42 +++++++++++++++++++ 13 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 app/views/broadcasts/_update_file_list.js.erb create mode 100644 app/views/broadcasts/destroy_file.js.erb diff --git a/app/channels/broadcasts_channel.rb b/app/channels/broadcasts_channel.rb index ad96756..f503c01 100644 --- a/app/channels/broadcasts_channel.rb +++ b/app/channels/broadcasts_channel.rb @@ -37,7 +37,7 @@ class BroadcastsChannel < ApplicationCable::Channel end def self.broadcast_file_upload_updates(broadcast, files, pagination_content) - files_content = ApplicationController.render partial: "broadcasts/file", collection: files + files_content = ApplicationController.render partial: "broadcasts/file", locals: { broadcast: broadcast }, collection: files broadcast_to broadcast, { event: :file_upload_update, diff --git a/app/controllers/broadcasts_controller.rb b/app/controllers/broadcasts_controller.rb index d778ba8..6f886b7 100644 --- a/app/controllers/broadcasts_controller.rb +++ b/app/controllers/broadcasts_controller.rb @@ -3,7 +3,7 @@ class BroadcastsController < ApplicationController before_action :set_project before_action :build_broadcast, only: [:new, :create] - before_action :set_broadcast, only: [:show, :destroy, :update] + before_action :set_broadcast, only: [:show, :destroy, :update, :destroy_file] before_action :set_multi_view_broadcasts, only: [:show] before_action :show_splash_screen, only: :index @@ -39,10 +39,7 @@ class BroadcastsController < ApplicationController end @broadcast.update(broadcast_params) - @files = @broadcast.files.order("created_at DESC").paginate(page: 1) - - pagination_content = ApplicationController.render html: helpers.will_paginate(@files, params: { active_tab: params[:active_tab], page: params[:page], active_files_tab: params[:active_files_tab] }) - BroadcastsChannel.broadcast_file_upload_updates(@broadcast, @files, pagination_content) + update_files_section end def destroy @@ -53,8 +50,23 @@ class BroadcastsController < ApplicationController end end + def destroy_file + authorize Broadcast + file = ActiveStorage::Attachment.find(params[:file_id]) + file.purge + + update_files_section + end + private + def update_files_section + @files = @broadcast.files.order("created_at DESC").paginate(page: 1) + + pagination_content = ApplicationController.render html: helpers.will_paginate(@files, params: { active_tab: params[:active_tab], page: params[:page], active_files_tab: params[:active_files_tab] }) + BroadcastsChannel.broadcast_file_upload_updates(@broadcast, @files, pagination_content) + end + def show_splash_screen render :splash if broadcasts.count.zero? end diff --git a/app/policies/broadcast_policy.rb b/app/policies/broadcast_policy.rb index 1a9d9be..15e7524 100644 --- a/app/policies/broadcast_policy.rb +++ b/app/policies/broadcast_policy.rb @@ -18,4 +18,8 @@ class BroadcastPolicy < ApplicationPolicy def update? true end + + def destroy_file? + user.manager? || user.account_manager? + end end diff --git a/app/views/broadcasts/_file.html.erb b/app/views/broadcasts/_file.html.erb index b0c90a8..670050d 100644 --- a/app/views/broadcasts/_file.html.erb +++ b/app/views/broadcasts/_file.html.erb @@ -1,12 +1,25 @@ -
  • - <% if file.variable? %> - <%= link_to image_tag(file.variant(resize_and_pad: [300, 300, background: "#F7F8F9"]), class: "bg-light img-thumbnail img-fluid"), file, target: "_blank" %> - <% else %> -
    - <%= link_to file, target: "_blank" do %> - <%= fa_icon("file", style: "font-size: 2rem") %> -
    <%= file.filename %>
    +
    + <% show_delete = policy(broadcast).destroy_file? %> + <% file_class = show_delete ? "col-8" : "col-12" %> +
    +
  • + <% if file.variable? %> + <%= link_to image_tag(file.variant(resize_and_pad: [300, 300, background: "#F7F8F9"]), class: "bg-light img-thumbnail img-fluid"), file, target: "_blank" %> + <% else %> +
    + <%= link_to file, target: "_blank" do %> + <%= fa_icon("file", style: "font-size: 2rem") %> +
    <%= file.filename %>
    + <% end %> +
    <% end %> +
  • + + <% if show_delete %> +
    + <% broadcast = local_assigns[:broadcast] %> + <% url = url_for [:destroy_file, broadcast.project, broadcast, { file_id: file.id }] %> + <%= link_to fa_icon("trash fw", text: t('.actions.delete_file')), url, class: "btn btn-danger", remote: true, method: :delete, data: { confirm: t('.confirm_delete') } %>
    <% end %> - + \ No newline at end of file diff --git a/app/views/broadcasts/_files_section.html.erb b/app/views/broadcasts/_files_section.html.erb index 86865c8..17e9ee3 100644 --- a/app/views/broadcasts/_files_section.html.erb +++ b/app/views/broadcasts/_files_section.html.erb @@ -8,7 +8,7 @@