diff --git a/app/controllers/admin/communication/websites/posts_controller.rb b/app/controllers/admin/communication/websites/posts_controller.rb index 4eba0bf3b..8f84f631a 100644 --- a/app/controllers/admin/communication/websites/posts_controller.rb +++ b/app/controllers/admin/communication/websites/posts_controller.rb @@ -4,6 +4,7 @@ class Admin::Communication::Websites::PostsController < Admin::Communication::We except: :restore include Admin::HasPreview + include Admin::HasMoveAction include Admin::HasStaticAction include Admin::Localizable diff --git a/app/controllers/concerns/admin/has_move_action.rb b/app/controllers/concerns/admin/has_move_action.rb new file mode 100644 index 000000000..2b9fa55e4 --- /dev/null +++ b/app/controllers/concerns/admin/has_move_action.rb @@ -0,0 +1,42 @@ +module Admin::HasMoveAction + extend ActiveSupport::Concern + + included do + before_action :load_object, :load_target_websites, :ensure_access, only: [:move, :do_move] + end + + def move + breadcrumb + add_breadcrumb t('admin.move.cta') + render 'admin/application/move/move' + end + + def do_move + new_website = @target_websites.find(params[:new_website_id]) + redirect_to [:admin, @object], alert: t('admin.move.permission_denied') and return unless new_website + redirect_to [:admin, @object], alert: t('admin.move.permission_denied') and return if new_website.university_id != @object.university_id + @object.move_to!(new_website) + redirect_to [:admin, @object, website_id: new_website.id], notice: t('admin.move.successfully_moved', model: @object.to_s_in(current_language)) + end + + protected + + def load_object + # resource is inherited from localizable concern, only working because "movable" object are also localizable. + # If we want to use this concern for non-localizable objects, we would need to define a method to load the object instead of relying on resource. + @object = resource + end + + def load_target_websites + @target_websites = current_user.managed_websites + .where(university_id: @object.university_id) + .where.not(id: @object.communication_website_id) + end + + def ensure_access + unless can?(:move, @object) && @target_websites.count > 0 + redirect_to [:admin, @object], alert: t('admin.move.permission_denied') + end + end + +end \ No newline at end of file diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb index 924dbc198..dfcdafd53 100644 --- a/app/helpers/admin/application_helper.rb +++ b/app/helpers/admin/application_helper.rb @@ -49,6 +49,13 @@ def duplicate_link(object, html_classes: nil) class: html_classes end + def move_link(object, html_classes: button_classes_danger) + return unless can?(:move, object) && current_user.managed_websites.count > 1 + link_to t('admin.move.cta'), + [:move, :admin, object], + class: html_classes + end + def preview_link raw "