From d43e6a55834a1022d0b3510aba8d9e58a098748f Mon Sep 17 00:00:00 2001 From: wandji20 Date: Thu, 24 Oct 2024 11:37:40 +0100 Subject: [PATCH] Move admin remove terms and condition logic to enterprise controller --- .../admin/enterprises_controller.rb | 13 +++++++-- app/reflexes/enterprise_edit_reflex.rb | 10 ------- .../form/_business_details.html.haml | 3 +-- .../terms_and_conditions_controller.js | 11 -------- config/routes/admin.rb | 1 + .../enterprises/terms_and_conditions_spec.rb | 27 +++++++++++++++++++ 6 files changed, 40 insertions(+), 25 deletions(-) delete mode 100644 app/reflexes/enterprise_edit_reflex.rb diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 410eb7b0e3..23db3ec7ba 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -13,7 +13,9 @@ module Admin prepend_before_action :override_owner, only: :create prepend_before_action :override_sells, only: :create - before_action :load_countries, except: [:index, :register, :check_permalink, :remove_logo] + before_action :load_countries, + except: [:index, :register, :check_permalink, :remove_logo, + :remove_terms_and_conditions] before_action :load_methods_and_fees, only: [:edit, :update] before_action :load_groups, only: [:new, :edit, :update, :create] before_action :load_taxons, only: [:new, :edit, :update, :create] @@ -151,7 +153,7 @@ module Admin respond_to do |format| format.html do flash[:success] = I18n.t("admin.enterprises.form.white_label.remove_logo_success") - redirect_to edit_admin_enterprise_path + redirect_to main_app.edit_admin_enterprise_path(@enterprise) end format.turbo_stream do flash.now[:success] = I18n.t("admin.enterprises.form.white_label.remove_logo_success") @@ -169,6 +171,13 @@ module Admin end end + def remove_terms_and_conditions + authorize! :remove_terms_and_conditions, @object + + @object.terms_and_conditions.purge_later + redirect_to edit_admin_enterprise_path(@object) + end + protected def delete_custom_tab diff --git a/app/reflexes/enterprise_edit_reflex.rb b/app/reflexes/enterprise_edit_reflex.rb deleted file mode 100644 index 8de7cd87d2..0000000000 --- a/app/reflexes/enterprise_edit_reflex.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: false - -class EnterpriseEditReflex < ApplicationReflex - def remove_terms_and_conditions - @enterprise = Enterprise.find(element.dataset['enterprise-id']) - throw :forbidden unless can?(:remove_terms_and_conditions, @enterprise) - - @enterprise.terms_and_conditions.purge_later - end -end diff --git a/app/views/admin/enterprises/form/_business_details.html.haml b/app/views/admin/enterprises/form/_business_details.html.haml index 30bef3ba2f..75d5aa857b 100644 --- a/app/views/admin/enterprises/form/_business_details.html.haml +++ b/app/views/admin/enterprises/form/_business_details.html.haml @@ -40,8 +40,7 @@ - if @enterprise.terms_and_conditions.attached? = link_to "#{@enterprise.terms_and_conditions.blob.filename} #{ t('.uploaded_on') } #{@enterprise.terms_and_conditions.blob.created_at}", url_for(@enterprise.terms_and_conditions), target: '_blank' %div - %a.icon-trash{ href: '#', data: { action: 'click->terms-and-conditions#remove', "terms-and-conditions-message-value": t('js.admin.enterprises.form.images.immediate_terms_and_conditions_removal_warning'), 'enterprise-id': @enterprise.id}} - = t('.remove_terms_and_conditions') + = link_to t('.remove_terms_and_conditions'), remove_terms_and_conditions_admin_enterprise_path(@enterprise), class: 'icon-trash', data: { method: :delete, confirm: t('js.admin.enterprises.form.images.immediate_terms_and_conditions_removal_warning') } .pad-top %div .button.small{ data: { controller: 'help-modal-link', action: 'click->help-modal-link#open', "help-modal-link-target-value": "terms_and_conditions_warning_modal" } } diff --git a/app/webpacker/controllers/terms_and_conditions_controller.js b/app/webpacker/controllers/terms_and_conditions_controller.js index a98e0faa83..efaea8303d 100644 --- a/app/webpacker/controllers/terms_and_conditions_controller.js +++ b/app/webpacker/controllers/terms_and_conditions_controller.js @@ -13,17 +13,6 @@ export default class extends ApplicationController { }); } - remove(event) { - let confirmation = confirm(this.messageValue); - if (confirmation) { - location.hash = ""; - this.stimulate( - "EnterpriseEdit#remove_terms_and_conditions", - event.target - ); - } - } - add() { this.fileinputTarget.click(); } diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 0b41958e84..e52293ab4c 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -34,6 +34,7 @@ Openfoodnetwork::Application.routes.draw do get :welcome patch :register patch :remove_logo + delete :remove_terms_and_conditions end resources :connected_apps, only: [:create, :destroy] diff --git a/spec/system/admin/enterprises/terms_and_conditions_spec.rb b/spec/system/admin/enterprises/terms_and_conditions_spec.rb index 15301c3760..bfcc8ec4fd 100644 --- a/spec/system/admin/enterprises/terms_and_conditions_spec.rb +++ b/spec/system/admin/enterprises/terms_and_conditions_spec.rb @@ -55,6 +55,33 @@ RSpec.describe "Uploading Terms and Conditions PDF" do go_to_business_details expect(page).to have_selector "a[href*='Terms-of-ServiceUK.pdf']" end + + it "uploading terms and conditions" do + go_to_business_details + + # Add PDF + attach_file "enterprise[terms_and_conditions]", original_terms, make_visible: true + + time = Time.zone.local(2002, 4, 13, 0, 0, 0) + Timecop.freeze(run_time = time) do + click_button "Update" + expect(distributor.reload.terms_and_conditions_blob.created_at).to eq run_time + end + expect(page). + to have_content "Enterprise \"#{distributor.name}\" has been successfully updated!" + + go_to_business_details + expect(page).to have_selector "a[href*='Terms-of-service.pdf'][target=\"_blank\"]" + expect(page).to have_content 'Remove File' + + # Remove PDF + accept_confirm "The Terms and Conditions file will be removed immediately after you confirm." do + click_on "Remove File" + end + go_to_business_details + expect(page).not_to have_selector "a[href*='Terms-of-service.pdf'][target=\"_blank\"]" + expect(page).not_to have_content 'Remove File' + end end end end