Add 'post as moderator' option for comments.

* Add 'post as moderator' option to comment form. This creates a so-called sticky comment.
* Downvotes have no effect on stickied comments; they're always visible, regardless of comment thresholds.
* Only mods may sticky comments.
* Mods may sticky comments by other users.
This commit is contained in:
evazion 2016-12-26 22:24:01 -06:00
parent 390524c7f5
commit 1257639109
9 changed files with 44 additions and 32 deletions

View File

@ -48,10 +48,6 @@ div.comments-for-post {
opacity: 1.0;
}
}
div.comment-preview {
margin-bottom: 2em;
}
}
div#c-posts {
@ -146,3 +142,12 @@ div#c-comments {
}
}
}
form.edit_comment div.input.boolean {
display: inline-block;
label {
font-weight: normal;
vertical-align: initial;
}
}

View File

@ -23,12 +23,12 @@ class CommentsController < ApplicationController
def update
@comment = Comment.find(params[:id])
check_privilege(@comment)
@comment.update_attributes(params[:comment].permit(:body))
@comment.update(update_params, :as => CurrentUser.role)
respond_with(@comment, :location => post_path(@comment.post_id))
end
def create
@comment = Comment.create(params[:comment])
@comment = Comment.create(create_params, :as => CurrentUser.role)
respond_with(@comment) do |format|
format.html do
if @comment.errors.any?
@ -110,4 +110,12 @@ private
raise User::PrivilegeError
end
end
def create_params
params.require(:comment).permit(:post_id, :body, :do_not_bump_post, :is_sticky)
end
def update_params
params.require(:comment).permit(:body, :is_deleted, :is_sticky)
end
end

View File

@ -12,7 +12,8 @@ class Comment < ActiveRecord::Base
before_validation :initialize_updater
after_create :update_last_commented_at_on_create
after_destroy :update_last_commented_at_on_destroy
attr_accessible :body, :post_id, :do_not_bump_post, :is_deleted
attr_accessible :body, :post_id, :do_not_bump_post, :is_deleted, :as => [:member, :gold, :platinum, :builder, :janitor, :moderator, :admin]
attr_accessible :is_sticky, :as => [:moderator, :admin]
mentionable(
:message_field => :body,
:user_field => :creator_id,
@ -34,11 +35,11 @@ class Comment < ActiveRecord::Base
end
def hidden(user)
where("score < ?", user.comment_threshold)
where("score < ? and is_sticky = false", user.comment_threshold)
end
def visible(user)
where("score >= ?", user.comment_threshold)
where("score >= ? or is_sticky = true", user.comment_threshold)
end
def deleted
@ -208,11 +209,11 @@ class Comment < ActiveRecord::Base
end
def delete!
update_attributes(:is_deleted => true)
update({ :is_deleted => true }, :as => CurrentUser.role)
end
def undelete!
update_attributes(:is_deleted => false)
update({ :is_deleted => false }, :as => CurrentUser.role)
end
end

View File

@ -1,5 +1,14 @@
<%= error_messages_for :comment %>
<%= simple_form_for(comment, :html => {:class => "edit_comment"}) do |f| %>
<%= f.hidden_field :post_id %>
<%= dtext_field "comment", "body", :value => comment.body, :input_id => "comment_body_for_#{comment.id}", :preview_id => "dtext-preview-for-#{comment.id}" %>
<%= f.button :submit, "Submit" %>
<%= f.button :submit, "Submit", :data => { :disable_with => "Submitting..." } %>
<%= dtext_preview_button "comment", "body", :input_id => "comment_body_for_#{comment.id}", :preview_id => "dtext-preview-for-#{comment.id}" %>
<% if comment.new_record? %>
<%= f.input :do_not_bump_post, :label => "No bump" %>
<% end %>
<% if CurrentUser.is_moderator? %>
<%= f.input :is_sticky, :label => "Post as moderator" %>
<% end %>
<% end %>

View File

@ -2,13 +2,7 @@
<div id="a-edit">
<h1>Edit Comment</h1>
<%= error_messages_for "comment" %>
<%= simple_form_for(@comment) do |f| %>
<%= dtext_field "comment", "body" %>
<%= f.button :submit, "Submit" %>
<%= dtext_preview_button "comment", "body" %>
<% end %>
<%= render "comments/form", :post => @comment.post, :comment => @comment %>
</div>
</div>

View File

@ -28,7 +28,7 @@
<% if CurrentUser.is_member? %>
<div class="new-comment">
<p><%= link_to "Post comment", new_comment_path, :class => "expand-comment-response" %></p>
<%= render "comments/partials/new/form", :post => post %>
<%= render "comments/form", :post => post, :comment => post.comments.new %>
</div>
<% end %>
</div>

View File

@ -1,10 +0,0 @@
<div class="comment-preview dtext dtext-preview">
</div>
<%= form_tag(comments_path, :class => "simple_form comment-form") do %>
<%= hidden_field "comment", "post_id", :value => post.id %>
<%= dtext_field "comment", "body", :input_id => "comment_response_for_#{post.id}", :preview_id => "dtext-preview-for-#{post.id}" %>
<%= submit_tag "Post", :data => { :disable_with => "Submitting..." } %>
<%= dtext_preview_button "comment", "body", :input_id => "comment_response_for_#{post.id}", :preview_id => "dtext-preview-for-#{post.id}" %>
<%= check_box "comment", "do_not_bump_post", :id => "comment_do_not_bump_post_#{post.id}" %> <label for="comment_do_not_bump_post_<%= post.id %>">No bump</label>
<% end %>

View File

@ -1,6 +1,6 @@
<% if CurrentUser.is_moderator? || !comment.is_deleted? %>
<a name="comment-<%= comment.id %>"></a>
<article class="comment" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator.name %>">
<article class="comment" data-post-id="<%= comment.post_id %>" data-comment-id="<%= comment.id %>" data-score="<%= comment.score %>" data-creator="<%= comment.creator.name %>" data-is-sticky="<%= comment.is_sticky %>">
<div class="author">
<h1>
<%= link_to_user comment.creator %>
@ -39,7 +39,7 @@
<% end %>
</menu>
<% if comment.editable_by?(CurrentUser.user) %>
<%= render "comments/form", :comment => comment %>
<%= render "comments/form", :post => comment.post, :comment => comment %>
<% end %>
<% end %>
</div>

View File

@ -0,0 +1,5 @@
class AddStickyToComments < ActiveRecord::Migration
def change
add_column :comments, :is_sticky, :boolean, null: false, default: false
end
end