Skip to content

Commit fc4f20c

Browse files
committed
Restrain move to same university websites
1 parent 9ab7ef5 commit fc4f20c

5 files changed

Lines changed: 27 additions & 51 deletions

File tree

app/controllers/concerns/admin/has_move_action.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Admin::HasMoveAction
22
extend ActiveSupport::Concern
33

44
included do
5-
before_action :ensure_access, :load_object, only: [:move, :do_move]
5+
before_action :load_object, :load_target_websites, :ensure_access, only: [:move, :do_move]
66
end
77

88
def move
@@ -12,30 +12,31 @@ def move
1212
end
1313

1414
def do_move
15-
new_website = current_user.managed_websites.find(params[:new_website_id])
15+
new_website = @target_websites.find(params[:new_website_id])
1616
redirect_to [:admin, @object], alert: t('admin.move.permission_denied') and return unless new_website
17-
old_university = @object.university
18-
new_university = new_website.university
17+
redirect_to [:admin, @object], alert: t('admin.move.permission_denied') and return if new_website.university_id != @object.university_id
1918
@object.move_to!(new_website)
20-
if old_university != new_university
21-
redirect_to admin_communication_website_posts_path(new_website), notice: t('admin.move.successfully_moved', model: @object.to_s_in(current_language))
22-
else
23-
redirect_to [:admin, @object, website_id: new_website.id], notice: t('admin.move.successfully_moved', model: @object.to_s_in(current_language))
24-
end
19+
redirect_to [:admin, @object, website_id: new_website.id], notice: t('admin.move.successfully_moved', model: @object.to_s_in(current_language))
2520
end
2621

2722
protected
28-
29-
def ensure_access
30-
unless can?(:move, @object) && current_user.managed_websites.count > 1
31-
redirect_to [:admin, @object], alert: t('admin.move.permission_denied')
32-
end
33-
end
3423

3524
def load_object
3625
# resource is inherited from localizable concern, only working because "movable" object are also localizable.
3726
# 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.
3827
@object = resource
3928
end
4029

30+
def load_target_websites
31+
@target_websites = current_user.managed_websites
32+
.where(university_id: @object.university_id)
33+
.where.not(id: @object.communication_website_id)
34+
end
35+
36+
def ensure_access
37+
unless can?(:move, @object) && @target_websites.count > 0
38+
redirect_to [:admin, @object], alert: t('admin.move.permission_denied')
39+
end
40+
end
41+
4142
end

app/models/communication/website/post.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,5 @@ def update_authors_status_if_necessary!
126126
def list_blocks_template_kind
127127
:posts
128128
end
129-
130-
def after_moved_to_website(source_website, target_website)
131-
authors.clear if source_website.university_id != target_website.university_id
132-
end
129+
133130
end

app/models/concerns/movable_to_website.rb

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module MovableToWebsite
22
extend ActiveSupport::Concern
33

4+
# NOTE: this method does not handle move across universities, as we currently don't have any use case for it.
5+
# 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.
46
def move_to!(website)
57
source_website = self.website
68
transaction do
7-
update(communication_website_id: website.id, university_id: website.university_id)
9+
update(communication_website_id: website.id)
810
clean_categories_after_move
911
clean_content_federations_after_move
1012
move_localizations_to(website)
@@ -28,29 +30,15 @@ def clean_content_federations_after_move
2830

2931
def move_localizations_to(website)
3032
localizations.each do |l10n|
31-
l10n.update(communication_website_id: website.id, university_id: website.university_id)
32-
move_featured_image(l10n, website)
33+
l10n.update(communication_website_id: website.id)
3334
move_blocks(l10n, website)
3435
end
3536
end
3637

37-
def move_featured_image(l10n, website)
38-
return unless l10n.respond_to?(:featured_image) && l10n.featured_image.attached?
39-
return if l10n.featured_image.blob.university_id == website.university_id
40-
blob = l10n.featured_image.blob
41-
if blob.attachments.count > 1
42-
# duplicate blob
43-
ActiveStorage::Utils.duplicate(l10n.featured_image, l10n.featured_image)
44-
else
45-
blob.update(university_id: website.university_id)
46-
end
47-
end
48-
4938
def move_blocks(l10n, website)
5039
return unless l10n.respond_to?(:blocks)
5140
l10n.blocks.ordered.each do |block|
52-
block.update(communication_website_id: website.id, university_id: website.university_id)
53-
# TODO: il faut aussi vérifier les attachments des blocks et les dupliquer si besoin, comme pour les featured_image.
41+
block.update(communication_website_id: website.id)
5442
end
5543
end
5644

app/views/admin/application/move/_list_sites.html.erb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/views/admin/application/move/move.html.erb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ do_move_path = "do_move_admin_#{class_path}_path"
99

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

12-
<% unless current_user.server_admin?%>
13-
<%= render 'admin/application/move/list_sites', websites: current_user.managed_websites, do_move_path: do_move_path %>
14-
<% else %>
15-
<% University.ordered.each do |university| %>
16-
<% next if university.websites.empty? %>
17-
<h2><%= university %></h2>
18-
<%= render 'admin/application/move/list_sites', websites: university.websites, do_move_path: do_move_path %>
19-
<% end %>
20-
<% end %>
12+
<ul>
13+
<% @target_websites.ordered(current_language).each do |website| %>
14+
<li><%= link_to website.to_s_in(current_language), send(do_move_path, @object, new_website_id: website.id), method: :post %></li>
15+
<% end %>
16+
</ul>

0 commit comments

Comments
 (0)