Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 42 additions & 0 deletions app/controllers/concerns/admin/has_move_action.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/helpers/admin/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<button class=\"btn btn-light mb-2\"
type=\"button\"
Expand Down
2 changes: 2 additions & 0 deletions app/models/communication/website/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Communication::Website::Post < ApplicationRecord
include GeneratesGitFiles
include Lifecyclable
include Localizable
include MovableToWebsite
include Sanitizable
include Searchable
include WithMenuItemTarget
Expand Down Expand Up @@ -125,4 +126,5 @@ def update_authors_status_if_necessary!
def list_blocks_template_kind
:posts
end

end
49 changes: 49 additions & 0 deletions app/models/concerns/movable_to_website.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module MovableToWebsite
extend ActiveSupport::Concern

# NOTE: this method does not handle move across universities, as we currently don't have any use case for it.
# If we need to support it in the future, we will need to update the method to also update the university_id of the object and its associations, and handle potential conflicts (e.g. slug) that may arise from the move.
def move_to!(website)
source_website = self.website
transaction do
update(communication_website_id: website.id)
clean_categories_after_move
clean_content_federations_after_move
move_localizations_to(website)
after_moved_to_website(source_website, website)
source_website.clean
end
self
end

protected

def clean_categories_after_move
return unless respond_to?(:categories)
categories.clear
end

def clean_content_federations_after_move
return unless respond_to?(:content_federations)
content_federations.destroy_all
end

def move_localizations_to(website)
localizations.each do |l10n|
l10n.update(communication_website_id: website.id)
move_blocks(l10n, website)
end
end

def move_blocks(l10n, website)
return unless l10n.respond_to?(:blocks)
l10n.blocks.ordered.each do |block|
block.update(communication_website_id: website.id)
end
end

def after_moved_to_website(source_website, target_website)
# Overridable method to implement specific logic
end

end
12 changes: 12 additions & 0 deletions app/models/user/with_roles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ def managed_roles
end.compact
end

def managed_websites
if server_admin?
Communication::Website.all
elsif admin?
Communication::Website.where(university_id: university_id)
elsif website_manager?
Communication::Website.where(id: managed_websites_ids)
else
Communication::Website.none
end
end

def can_display_global_menu?
User.roles_with_access_to_global_menu.include?(role)
end
Expand Down
16 changes: 16 additions & 0 deletions app/views/admin/application/move/move.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%
# communication_website_post
class_path = @object.model_name.singular_route_key
# do_move_admin_communication_website_post_path
do_move_path = "do_move_admin_#{class_path}_path"
%>

<% content_for :title, t('admin.move.cta') %>

<p><%= t('admin.move.description') %></p>

<ul>
<% @target_websites.ordered(current_language).each do |website| %>
<li><%= link_to website.to_s_in(current_language), send(do_move_path, @object, new_website_id: website.id), method: :post %></li>
<% end %>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<% content_for :title_right do %>
<%= button_advanced do %>
<%= destroy_link @post %>
<%= move_link @post %>
<%= duplicate_link @post %>
<% end %>
<% post_url = @l10n.current_permalink_url_in_website(@website) %>
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ en:
meta_description:
hint: Plain text without HTML, dedicated to Search Engine Optimization (SEO).
label: Meta description
move:
cta: Move
description: "Move this item to another website you manage:"
permission_denied: You cannot move this item.
successfully_moved: "<i>%{model}</i> was successfully moved."
password_hint: Leave blank if you do not wish to change the password.
pending_tasks:
tasks: Background tasks pending
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ fr:
meta_description:
hint: Texte simple, dédié au à l'optimisation pour les moteurs de recherche (Search Engine Optimization, SEO).
label: Description pour les moteurs de recherche (meta)
move:
cta: Déplacer
description: "Déplacez cet élément vers un autre site web que vous gérez :"
permission_denied: Vous ne pouvez pas déplacer cet élément.
successfully_moved: "<i>%{model}</i> a bien été déplacé(e)."
password_hint: Laissez vide si vous ne souhaitez pas modifier le mot de passe.
pending_tasks:
tasks: Tâches en cours de traitement
Expand Down
2 changes: 2 additions & 0 deletions config/routes/admin/communication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
post :duplicate
post :publish
post :restore
get :move
post :do_move
end
end
namespace :agenda do
Expand Down
Loading
Loading