diff --git a/app/controllers/post_history_controller.rb b/app/controllers/post_history_controller.rb index 80a37de1c..543771e02 100644 --- a/app/controllers/post_history_controller.rb +++ b/app/controllers/post_history_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 79e03f7b1..5e8b293a8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -464,13 +464,9 @@ def restore end def document - @post = Post.unscoped.where(doc_slug: params[:slug], community_id: [RequestContext.community_id, nil]).first - 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 diff --git a/app/models/post.rb b/app/models/post.rb index 54d1c1e5b..436e84812 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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| diff --git a/app/views/post_history/post.html.erb b/app/views/post_history/post.html.erb index c2a5d1e54..c31b586ea 100644 --- a/app/views/post_history/post.html.erb +++ b/app/views/post_history/post.html.erb @@ -1,8 +1,12 @@ +<% @show_content = !!defined?(show_content) ? show_content : true %> +

Post History

+<% if @show_content %>
<%= render 'posts/type_agnostic', post: @post, show_category_tag: true, show_type_tag: true, last_activity: false %>
+<% end %> <% @history.each.with_index do |event, index| %>
diff --git a/app/views/posts/document.html.erb b/app/views/posts/document.html.erb index a5cb84a16..e08d748ab 100644 --- a/app/views/posts/document.html.erb +++ b/app/views/posts/document.html.erb @@ -2,9 +2,15 @@ « 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' %> diff --git a/config/routes.rb b/config/routes.rb index f3b1c81b1..8b8b6b37b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/controllers/posts/help_test.rb b/test/controllers/posts/help_test.rb index 48ba059ab..db3e59f4d 100644 --- a/test/controllers/posts/help_test.rb +++ b/test/controllers/posts/help_test.rb @@ -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) + 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