Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add alert when missing attachment #1220 #1273

Merged
merged 10 commits into from
Dec 26, 2024
21 changes: 20 additions & 1 deletion app/controllers/concerns/attachment_manageable.rb
kasugaijin marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@ module AttachmentManageable
def allow_only_one_attachment
return unless params[:files].is_a?(Array)

flash.now[:alert] = t(".attachment_manageable.upload_limit_exceeded")
flash.now[:alert] = t("attachment_manageable.upload_limit_exceeded")
render turbo_stream: turbo_stream.replace("flash", partial: "layouts/shared/flash_messages")
end

def show_alert_if_attachment_missing
# images and files are arrays that sometimes populated with empty strings, so we want to remove
# those empty strings before checking whether or no "real" images / files are being attached
if params[:pet][:images].present?
images = params[:pet][:images].reject { |image| image == "" }
elsif params[:pet][:files].present?
files = params[:pet][:files].reject { |file| file == "" }
end

if params[:action] == "attach_images" && images.length >= 1
nil
elsif params[:action] == "attach_files" && files.length >= 1
nil
else
flash.now[:alert] = t("attachment_manageable.attachment_missing")
render turbo_stream: turbo_stream.replace("flash", partial: "layouts/shared/flash_messages")
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/organizations/staff/pets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Organizations::Staff::PetsController < Organizations::BaseController
before_action :set_pet, only: [:show, :edit, :update, :destroy, :attach_images, :attach_files]
before_action :show_alert_if_attachment_missing, only: [:attach_images, :attach_files]
include ::Pagy::Backend
include AttachmentManageable

layout "dashboard"

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,4 @@ en:
more_questions: "More Questions? Contact us"
attachment_manageable:
upload_limit_exceeded: "Upload limit exceeded: Only one file allowed."
attachment_missing: "Please select a file to attach."
Loading