Skip to content

Commit

Permalink
Namespace Attachments: Attachment Actions Concern (#781)
Browse files Browse the repository at this point in the history
* migrate namespace attachment controller actions to concern

* fix translations

* undo change

* fix rebase

* change function name
  • Loading branch information
ChrisHuynh333 authored Sep 25, 2024
1 parent 8fe7b8f commit adaac51
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 237 deletions.
130 changes: 130 additions & 0 deletions app/controllers/concerns/attachment_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

# Common Attachment Actions
module AttachmentActions # rubocop:disable Metrics/ModuleLength
extend ActiveSupport::Concern

included do
before_action proc { current_page }
before_action proc { set_namespace }
before_action proc { set_authorization_object }
before_action :new_destroy_params, only: %i[new_destroy]
before_action :attachment, only: %i[destroy]
end

def index
authorize! @authorize_object, to: :view_attachments?

@render_individual_attachments = filter_requested?
@q = build_ransack_query
set_default_sort
@pagy, @attachments = pagy_with_metadata_sort(@q.result)
end

def new
authorize! @authorize_object, to: :create_attachment?

render turbo_stream: turbo_stream.update('attachment_modal',
partial: 'new_attachment_modal',
locals: {
open: true,
attachment: Attachment.new(attachable: @namespace),
namespace: @namespace
}), status: :ok
end

def create
@attachments = ::Attachments::CreateService.new(current_user, @namespace, attachment_params).execute

status = if !@attachments.count.positive?
:unprocessable_entity
elsif @attachments.count(&:persisted?) == @attachments.count
:ok
else
:multi_status
end

respond_to do |format|
format.turbo_stream do
render status:, locals: { attachment: Attachment.new(attachable: @namespace),
attachments: @attachments }
end
end
end

def new_destroy
authorize! @authorize_object, to: :destroy_attachment?
render turbo_stream: turbo_stream.update('attachment_modal',
partial: 'delete_attachment_modal',
locals: {
open: true,
attachment: @attachment,
namespace: @namespace
}), status: :ok
end

def destroy # rubocop:disable Metrics/MethodLength
@destroyed_attachments = ::Attachments::DestroyService.new(@namespace, @attachment, current_user).execute
respond_to do |format|
if @destroyed_attachments.count.positive?
status = destroy_status(@attachment, @destroyed_attachments.length)
format.turbo_stream do
render status:, locals: { destroyed_attachments: @destroyed_attachments }
end
else
format.turbo_stream do
render status: :unprocessable_entity,
locals: { message: t('.error',
filename: @attachment.file.filename,
errors: @attachment.errors.full_messages.first),
destroyed_attachments: nil }
end
end
end
end

private

def filter_requested?
params.dig(:q, :puid_or_file_blob_filename_cont).present?
end

def build_ransack_query
if @render_individual_attachments
@namespace.attachments.all.ransack(params[:q])
else
@namespace.attachments
.where.not(Attachment.arel_table[:metadata].contains({ direction: 'reverse' }))
.ransack(params[:q])
end
end

def new_destroy_params
@attachment = Attachment.find_by(id: params[:attachment_id])
end

def attachment
@attachment = Attachment.find_by(id: params[:id])
end

def destroy_status(attachment, count)
return count == 2 ? :ok : :multi_status if attachment.associated_attachment

count == 1 ? :ok : :unprocessable_entity
end

def layout_fixed
super
return unless action_name == 'index'

@fixed = false
end

def set_default_sort
@q.sorts = 'updated_at desc' if @q.sorts.empty?
end

def attachment_params
params.require(:attachment).permit(:attachable_id, :attachable_type, files: [])
end
end
126 changes: 10 additions & 116 deletions app/controllers/groups/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,24 @@

module Groups
# Controller actions for Group Attachments
class AttachmentsController < Groups::ApplicationController # rubocop:disable Metrics/ClassLength
class AttachmentsController < Groups::ApplicationController
include Metadata
before_action :group, :current_page
before_action :new_destroy_params, only: %i[new_destroy]
before_action :attachment, only: %i[destroy]
include AttachmentActions

def index
authorize! @group, to: :view_attachments?
before_action :group

@render_individual_attachments = filter_requested?
@q = build_ransack_query
set_default_sort
@pagy, @attachments = pagy_with_metadata_sort(@q.result)
end

def new
authorize! @group, to: :create_attachment?

render turbo_stream: turbo_stream.update('attachment_modal',
partial: 'new_attachment_modal',
locals: {
open: true,
attachment: Attachment.new(attachable: @group),
namespace: @group
}), status: :ok
end

def create
@attachments = ::Attachments::CreateService.new(current_user, @group, attachment_params).execute

status = if !@attachments.count.positive?
:unprocessable_entity
elsif @attachments.count(&:persisted?) == @attachments.count
:ok
else
:multi_status
end

respond_to do |format|
format.turbo_stream do
render status:, locals: { attachment: Attachment.new(attachable: @group),
attachments: @attachments }
end
end
end
private

def new_destroy
authorize! @group, to: :destroy_attachment?
render turbo_stream: turbo_stream.update('attachment_modal',
partial: 'delete_attachment_modal',
locals: {
open: true,
attachment: @attachment,
namespace: @group
}), status: :ok
def group
@group = Group.find_by_full_path(params[:group_id]) # rubocop:disable Rails/DynamicFindBy
end

def destroy # rubocop:disable Metrics/MethodLength
@destroyed_attachments = ::Attachments::DestroyService.new(@group, @attachment, current_user).execute
respond_to do |format|
if @destroyed_attachments.count.positive?
status = destroy_status(@attachment, @destroyed_attachments.length)
format.turbo_stream do
render status:, locals: { destroyed_attachments: @destroyed_attachments }
end
else
format.turbo_stream do
render status: :unprocessable_entity,
locals: { message: t('.error',
filename: @attachment.file.filename,
errors: @attachment.errors.full_messages.first),
destroyed_attachments: nil }
end
end
end
def set_namespace
@namespace = group
end

private

def group
@group = Group.find_by_full_path(params[:group_id]) # rubocop:disable Rails/DynamicFindBy
def set_authorization_object
@authorize_object = group
end

def current_page
Expand All @@ -97,48 +34,5 @@ def context_crumbs
path: group_attachments_path(@group)
}]
end

def filter_requested?
params.dig(:q, :puid_or_file_blob_filename_cont).present?
end

def build_ransack_query
if @render_individual_attachments
@group.attachments.all.ransack(params[:q])
else
@group.attachments
.where.not(Attachment.arel_table[:metadata].contains({ direction: 'reverse' }))
.ransack(params[:q])
end
end

def layout_fixed
super
return unless action_name == 'index'

@fixed = false
end

def set_default_sort
@q.sorts = 'updated_at desc' if @q.sorts.empty?
end

def attachment_params
params.require(:attachment).permit(:attachable_id, :attachable_type, files: [])
end

def new_destroy_params
@attachment = Attachment.find_by(id: params[:attachment_id])
end

def attachment
@attachment = Attachment.find_by(id: params[:id])
end

def destroy_status(attachment, count)
return count == 2 ? :ok : :multi_status if attachment.associated_attachment

count == 1 ? :ok : :unprocessable_entity
end
end
end
Loading

0 comments on commit adaac51

Please sign in to comment.