Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post history for help center and policy posts #1487

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions app/controllers/post_history_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,28 @@ def post

@history = PostHistory.where(post_id: params[:id])
.includes(:post_history_type, :user, post_history_tags: [:tag])
.order(created_at: :desc, id: :desc).paginate(per_page: 20, page: params[:page])
render layout: 'without_sidebar'
.order(created_at: :desc, id: :desc)
.paginate(per_page: 20, page: params[:page])

if @post&.help_category.nil?
render layout: 'without_sidebar'
else
render 'post_history/post', layout: 'without_sidebar', locals: { show_content: false }
end
end

def slug_post
@post = Post.by_slug(params[:slug], current_user)

if @post.nil?
return not_found
end

@history = PostHistory.where(post_id: @post.id)
.includes(:post_history_type, :user)
.order(created_at: :desc, id: :desc)
.paginate(per_page: 20, page: params[:page])

render 'post_history/post', layout: 'without_sidebar', locals: { show_content: false }
end
end
8 changes: 2 additions & 6 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,9 @@ def restore
end

def document
@post = Post.unscoped.where(doc_slug: params[:slug], community_id: [RequestContext.community_id, nil]).first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to post.rb -> self.by_slug, no change other than taking parameter

not_found && return if @post.nil?
@post = Post.by_slug(params[:slug], current_user)

if @post&.help_category == '$Disabled'
not_found
end
if @post&.help_category == '$Moderator' && !current_user&.is_moderator
if @post.nil?
not_found
end

Expand Down
17 changes: 17 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ def self.search(term)
match_search term, posts: :body_markdown
end

def self.by_slug(slug, user)
post = Post.unscoped.where(
doc_slug: slug,
community_id: [RequestContext.community_id, nil]
).first

if post&.help_category == '$Disabled'
return nil
end

if post&.help_category == '$Moderator' && !user&.is_moderator
return nil
end

post
end

# Double-define: initial definitions are less efficient, so if we have a record of the post type we'll
# override them later with more efficient methods.
['Question', 'Answer', 'PolicyDoc', 'HelpDoc', 'Article'].each do |pt|
Expand Down
4 changes: 4 additions & 0 deletions app/views/post_history/post.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<% @show_content = !!defined?(show_content) ? show_content : true %>
cellio marked this conversation as resolved.
Show resolved Hide resolved

<h1>Post History</h1>

<% if @show_content %>
<div class="item-list">
<%= render 'posts/type_agnostic', post: @post, show_category_tag: true, show_type_tag: true, last_activity: false %>
</div>
<% end %>

<% @history.each.with_index do |event, index| %>
<details class="history-event" id="<%= @history.size - index %>">
Expand Down
8 changes: 7 additions & 1 deletion app/views/posts/document.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
&laquo; Back to help center
<% end %>
<% unless @post.nil? %>
<% if (moderator? && @post.post_type_id == HelpDoc.post_type_id) || (admin? && @post.post_type_id == PolicyDoc.post_type_id) %>
<%
is_hc = @post.post_type_id == HelpDoc.post_type_id
is_policy = @post.post_type_id == PolicyDoc.post_type_id
history_path = is_hc ? help_post_history_path(@post.doc_slug) : policy_post_history_path(@post.doc_slug)
%>
<% if (moderator? && is_hc) || (admin? && is_policy) %>
<%= link_to 'edit', edit_post_path(@post), class: "button is-outlined is-muted" %>
<% end %>
<%= link_to 'history', history_path, class: "button is-outlined is-muted" %>
<% end %>

<% if @post.help_category == '$Moderator' %>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@
get ':id/:answer', to: 'posts#show', as: :answer_post
end

get 'policy/:slug/history', to: 'post_history#slug_post', as: :policy_post_history, constraints: { slug: /.*/ }
get 'policy/:slug', to: 'posts#document', as: :policy, constraints: { slug: /.*/ }
get 'help/:slug/history', to: 'post_history#slug_post', as: :help_post_history, constraints: { slug: /.*/ }
get 'help/:slug', to: 'posts#document', as: :help, constraints: { slug: /.*/ }

get 'tags', to: 'tags#index', as: :tags
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/posts/help_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ class PostsControllerTest < ActionController::TestCase
test 'moderator help requires authentication' do
get :document, params: { slug: posts(:mod_help_article).doc_slug }
assert_response 404
assert_not_nil assigns(:post)
cellio marked this conversation as resolved.
Show resolved Hide resolved
assert_nil assigns(:post)
end

test 'regular user cannot get mod help' do
sign_in users(:standard_user)
get :document, params: { slug: posts(:mod_help_article).doc_slug }
assert_response 404
assert_not_nil assigns(:post)
assert_nil assigns(:post)
end

test 'cannot get disabled help article' do
sign_in users(:moderator)
get :document, params: { slug: posts(:disabled_help_article).doc_slug }
assert_response 404
assert_not_nil assigns(:post)
assert_nil assigns(:post)
end
end
Loading