Skip to content

Commit 7c888fd

Browse files
authored
Merge pull request #1907 from codidact/cellio/1496-edits-deleted-posts
show pending edits on deleted posts (and the indicator) IFF current user can handle them
2 parents c4ed5b2 + cf2766a commit 7c888fd

8 files changed

Lines changed: 73 additions & 15 deletions

File tree

app/controllers/categories_controller.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ def create
4747
AuditLog.admin_audit(event_type: 'category_create', related: @category, user: current_user,
4848
comment: "<<Category #{before}>>")
4949
flash[:success] = 'Your category was created.'
50-
Rails.cache.delete "#{RequestContext.community_id}/header_categories"
51-
Rails.cache.delete 'categories/by_lowercase_name'
52-
Rails.cache.delete 'categories/by_id'
50+
clear_categories_cache
5351
redirect_to category_path(@category)
5452
else
5553
flash[:danger] = 'There were some errors while trying to save your category.'
@@ -69,9 +67,7 @@ def update
6967
AuditLog.admin_audit(event_type: 'category_update', related: @category, user: current_user,
7068
comment: "from <<Category #{before}>>\nto <<Category #{after}>>")
7169
flash[:success] = 'Your category was updated.'
72-
Rails.cache.delete "#{RequestContext.community_id}/header_categories"
73-
Rails.cache.delete 'categories/by_lowercase_name'
74-
Rails.cache.delete 'categories/by_id'
70+
clear_categories_cache
7571
redirect_to category_path(@category)
7672
else
7773
flash[:danger] = 'There were some errors while trying to save your category.'
@@ -207,6 +203,12 @@ def set_list_posts
207203
@posts = @posts.paginate(page: params[:page], per_page: 50).order(sort_param)
208204
end
209205

206+
def clear_categories_cache
207+
Rails.cache.delete 'header_categories'
208+
Rails.cache.delete 'categories/by_lowercase_name'
209+
Rails.cache.delete 'categories/by_id'
210+
end
211+
210212
# Updates last visit cache for a given category
211213
# @param category [Category] category to update
212214
# @return [Boolean] whether the cache entry is deleted

app/controllers/suggested_edit_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def category_index
1414

1515
@category = params[:category].present? ? Category.find(params[:category]) : nil
1616

17-
@edits = SuggestedEdit.where(post: Post.undeleted.in(@category),
18-
active: !@show_decided)
17+
@edits = SuggestedEdit.accessible_to(current_user, @category)
18+
.where(active: !@show_decided)
1919
.order(@sort_type => @sort_order)
2020
end
2121

app/helpers/categories_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ def pending_suggestions?
4646
SuggestedEdit.where(post: Post.undeleted.in(current_category), active: true).any?
4747
end
4848
end
49+
50+
##
51+
# Checks if there are any pending edit suggestions in the current category,
52+
# including on deleted posts (for viewers who can both edit and see deleted).
53+
# @note Cached - cache is manually broken when new suggestions are created.
54+
# @return [Boolean]
55+
def pending_suggestions_with_deleted?
56+
Rails.cache.fetch "pending_suggestions_with_deleted/#{current_category.id}" do
57+
SuggestedEdit.where(post: Post.in(current_category), active: true).any?
58+
end
59+
end
4960
end

app/models/suggested_edit.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ class SuggestedEdit < ApplicationRecord
2222
scope :by, ->(user) { where(user: user) }
2323
scope :rejected, -> { where(active: false, accepted: false) }
2424

25+
# Gets suggested edits scoped for a given user and category
26+
# @param user [User] user to check
27+
# @param category [Category] category to check
28+
# @return [ActiveRecord::Relation<SuggestedEdit>]
29+
def self.accessible_to(user, category)
30+
posts = user&.can_see_deleted_posts? ? Post.in(category) : Post.undeleted.in(category)
31+
SuggestedEdit.where(post: posts)
32+
end
33+
2534
def clear_pending_cache
2635
Rails.cache.delete "pending_suggestions/#{post.category_id}"
36+
Rails.cache.delete "pending_suggestions_with_deleted/#{post.category_id}"
2737
end
2838

2939
def pending?

app/views/layouts/_header.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
<%= link_to suggested_edits_queue_path(current_cat),
281281
class: "category-header--nav-item #{active?(current_cat) && controller_name == 'suggested_edit' ? 'is-active' : ''}" do %>
282282
Edits
283-
<% if pending_suggestions? && check_your_privilege('edit_posts') %>
283+
<% if check_your_privilege('edit_posts') && (pending_suggestions? || (pending_suggestions_with_deleted? && check_your_privilege('flag_curate'))) %>
284284
<span class="badge is-status" title="Suggested edits pending"></span>
285285
<% end %>
286286
<% end %>

app/views/suggested_edit/category_index.html.erb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<h1>Suggested Edits</h1>
22
<p class="is-lead">Suggested edits for review.</p>
33

4-
<% categories = Category.unscoped.where(community: @community).order(sequence: :asc, id: :asc) %>
4+
<% categories = Category.unscoped.accessible_to(current_user)
5+
.where(community: @community)
6+
.order(sequence: :asc, id: :asc) %>
57
<% current_cat = current_category %>
68

79
<div class="suggested-edits-list-header">
@@ -34,8 +36,7 @@
3436
<th>pending edits</th>
3537
</tr>
3638
<% categories.each do |cat| %>
37-
<% next if (cat.min_view_trust_level || -1) > (current_user&.trust_level || 0) %>
38-
<% sug_edits = SuggestedEdit.where(post: Post.undeleted.in(cat), active: !@show_decided).count %>
39+
<% sug_edits_count = SuggestedEdit.accessible_to(current_user, cat).where(active: !@show_decided).count %>
3940
<% queue_url = suggested_edits_queue_url(cat, params.permit(:sort, :order, :show_decided)) %>
4041
<tr>
4142
<td>
@@ -47,9 +48,9 @@
4748
</td>
4849
<td>
4950
<% if cat == current_cat %>
50-
<%= sug_edits %>
51+
<%= sug_edits_count %>
5152
<% else %>
52-
<%= link_to sug_edits, queue_url %>
53+
<%= link_to sug_edits_count, queue_url %>
5354
<% end %>
5455
</td>
5556
</tr>
@@ -60,7 +61,7 @@
6061
<% if @edits.any? %>
6162
<div class="widget">
6263
<% @edits.each do |edit| %>
63-
<div class="widget--body h-p-b-0" data-ckb-list-item data-ckb-item-type="link">
64+
<div class="widget--body h-p-b-0 <%= 'deleted-content' if edit.post.deleted? %>" data-ckb-list-item data-ckb-item-type="link">
6465
<div>
6566
<%= link_to suggested_edit_path(edit.id), class: 'has-font-size-larger', data: {'ckb-item-link' => ''} do %>
6667
<%= edit.post.post_type.name %>: <strong><%= edit.post.post_type.is_top_level ? edit.post.title : edit.post.parent.title %></strong>

test/fixtures/suggested_edits.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,14 @@ approved_edit_scorer:
8585
decided_at: 2000-01-01T00:00:00.000000Z
8686
decided_by: editor
8787
title: approved suggested edit by the user used for edit score tests
88+
89+
on_deleted_post:
90+
post: deleted
91+
user: standard_user
92+
community: sample
93+
body: This suggested edit is on a deleted post
94+
title: Edit on a deleted post
95+
body_markdown: <p>This suggested edit is on a deleted post</p>
96+
comment: for users with access only
97+
active: true
98+
accepted: false

test/models/suggested_edit_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,27 @@ class SuggestedEditTest < ActiveSupport::TestCase
1616
assert suggested_edits(:accepted_suggested_edit).approved?
1717
assert suggested_edits(:rejected_suggested_edit).rejected?
1818
end
19+
20+
test 'accessible_to should correctly scope suggested edits' do
21+
categories_with_edits_on_deleted_posts = suggested_edits
22+
.select { |edit| edit.post.deleted? }
23+
.map { |edit| edit.post.category.id }
24+
.uniq
25+
26+
assert categories_with_edits_on_deleted_posts.any?
27+
28+
users.each do |user|
29+
categories_with_edits_on_deleted_posts.each do |category|
30+
edits = SuggestedEdit.accessible_to(user, category)
31+
32+
assert edits.any?
33+
34+
if user.can_see_deleted_posts?
35+
assert(edits.any? { |edit| edit.post.deleted? })
36+
else
37+
assert(edits.none? { |edit| edit.post.deleted? })
38+
end
39+
end
40+
end
41+
end
1942
end

0 commit comments

Comments
 (0)