allows comment as moderator/administrator

Ref: #170
This commit is contained in:
Juanjo Bazán
2015-08-24 18:17:04 +02:00
parent 57228636c4
commit 030fd49120
5 changed files with 184 additions and 55 deletions

View File

@@ -35,11 +35,20 @@ class CommentsController < ApplicationController
private private
def comment_params def comment_params
params.require(:comment).permit(:commentable_type, :commentable_id, :body) params.require(:comment).permit(:commentable_type, :commentable_id, :body, :as_moderator, :as_administrator)
end end
def build_comment def build_comment
@comment = Comment.build(debate, current_user, comment_params[:body]) @comment = Comment.build(debate, current_user, comment_params[:body])
check_for_special_comments
end
def check_for_special_comments
if ["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, debate)
@comment.administrator_id = current_user.administrator.id
elsif ["1", true].include?(comment_params[:as_moderator]) && can?(:comment_as_moderator, debate)
@comment.moderator_id = current_user.moderator.id
end
end end
def debate def debate

View File

@@ -4,7 +4,7 @@ class Comment < ActiveRecord::Base
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
acts_as_votable acts_as_votable
attr_accessor :comment_as_moderator, :comment_as_administrator attr_accessor :as_moderator, :as_administrator
validates :body, presence: true validates :body, presence: true
validates :user, presence: true validates :user, presence: true

View File

@@ -5,71 +5,73 @@
<%= t("debates.comment.deleted") %> <%= t("debates.comment.deleted") %>
<% else %> <% else %>
<% if comment.user.organization? %> <% if comment.as_administrator? %>
<%= image_tag("collective_avatar.png", size: 32, class: "avatar left") %>
<% else %>
<%= avatar_image(comment.user, size: 32, class: "left") %>
<% end %>
<% if comment.user.hidden? %>
<i class="icon-deleted user-deleted"></i>
<% end %>
<!--
<% if comment.user.moderator? %> <!-- AND has activated CHECKBOX comment as moderator ->
<%= image_tag("moderator_avatar.png", size: 32, class: "moderator-avatar left") %>
<% elsif comment.user.administrator? %> <!-- AND has activated CHECKBOX comment as admin ->
<%= image_tag("admin_avatar.png", size: 32, class: "admin-avatar left") %> <%= image_tag("admin_avatar.png", size: 32, class: "admin-avatar left") %>
<% elsif comment.as_moderator? %>
<%= image_tag("moderator_avatar.png", size: 32, class: "moderator-avatar left") %>
<% else %>
<% if comment.user.organization? %>
<%= image_tag("collective_avatar.png", size: 32, class: "avatar left") %>
<% else %>
<%= avatar_image(comment.user, size: 32, class: "left") %>
<% end %>
<% if comment.user.hidden? %>
<i class="icon-deleted user-deleted"></i>
<% end %>
<% end %> <% end %>
-->
<div class="comment-body"> <div class="comment-body">
<div class="comment-info"> <div class="comment-info">
<% if comment.user.hidden? %> <% if comment.as_administrator? %>
<span class="user-name"><%= t("debates.comment.user_deleted") %></span> <span class="user-name"><%= t("debates.comment.admin") %> #<%= comment.administrator_id%></span>
<% elsif comment.as_moderator? %>
<span class="user-name"><%= t("debates.comment.moderator") %> #<%= comment.moderator_id%></span>
<% else %> <% else %>
<% if comment.user.moderator? %> <!-- AND activate moderator CHECKBOX --> <% if comment.user.hidden? %>
<span class="user-name"><%= t("debates.comment.moderator") %> #ID</span> <!-- Add ID to Moderator name --> <span class="user-name"><%= t("debates.comment.user_deleted") %></span>
<% elsif comment.user.administrator? %> <!-- AND activate administrator CHECKBOX -->
<span class="user-name"><%= t("debates.comment.admin") %> #ID</span> <!-- Add ID to Admin name -->
<% else %> <% else %>
<span class="user-name"><%= comment.user.name %></span> <span class="user-name"><%= comment.user.name %></span>
<% if comment.user.official? %>
&nbsp;&bullet;&nbsp;
<span class="label round level-<%= comment.user.official_level %>">
<%= comment.user.official_position %>
</span>
<% end %>
<% end %> <% end %>
<% if comment.user.verified_organization? %>
<% if comment.user.official? %>
&nbsp;&bullet;&nbsp; &nbsp;&bullet;&nbsp;
<span class="label round level-<%= comment.user.official_level %>"> <span class="label round is-association">
<%= comment.user.official_position %> <%= t("shared.collective") %>
</span> </span>
<% end %> <% end %>
<% if comment.user_id == @debate.author_id %>
&nbsp;&bullet;&nbsp;
<span class="label round is-author">
<%= t("debates.comment.author") %>
</span>
<% end %>
<% end %> <% end %>
<% if comment.user.verified_organization? %>
&nbsp;&bullet;&nbsp;
<span class="label round is-association">
<%= t("shared.collective") %>
</span>
<% end %>
<% if comment.user_id == @debate.author_id %>
&nbsp;&bullet;&nbsp;
<span class="label round is-author">
<%= t("debates.comment.author") %>
</span>
<% end %>
&nbsp;&bullet;&nbsp;<%= time_ago_in_words(comment.created_at) %> &nbsp;&bullet;&nbsp;<%= time_ago_in_words(comment.created_at) %>
<span class="right js-flag-as-inappropiate-actions"> <span class="right js-flag-as-inappropiate-actions">
<%= render 'comments/flag_as_inappropiate_actions', comment: comment %> <%= render 'comments/flag_as_inappropiate_actions', comment: comment %>
</span> </span>
</div> </div>
<% if comment.user.official? && comment.user_id == @debate.author_id %> <% if comment.as_administrator? %>
<p class="comment-user is-admin"><%= comment.body %></p>
<% elsif comment.as_moderator? %>
<p class="comment-user is-moderator"><%= comment.body %></p>
<% elsif comment.user.official? && comment.user_id == @debate.author_id %>
<p class="comment-user level-<%= comment.user.official_level %> is-author"><%= comment.body %></p> <p class="comment-user level-<%= comment.user.official_level %> is-author"><%= comment.body %></p>
<% elsif comment.user.official? %> <% elsif comment.user.official? %>
<p class="comment-user level-<%= comment.user.official_level %>"><%= comment.body %></p> <p class="comment-user level-<%= comment.user.official_level %>"><%= comment.body %></p>
<% elsif comment.user.moderator? %> <!-- AND has activated CHECKBOX comment as moderator (all badges are hidden: 100% anonymous) -->
<p class="comment-user is-moderator"><%= comment.body %></p>
<% elsif comment.user.administrator? %> <!-- AND has activated CHECKBOX comment as admin (all badges are hidden: 100% anonymous) -->
<p class="comment-user is-admin"><%= comment.body %></p>
<% elsif comment.user_id == @debate.author_id %> <% elsif comment.user_id == @debate.author_id %>
<p class="comment-user is-author"><%= comment.body %></p> <p class="comment-user is-author"><%= comment.body %></p>
<% else %> <% else %>

View File

@@ -6,17 +6,17 @@
<%= f.submit comment_button_text(parent), class: "button radius small inline-block" %> <%= f.submit comment_button_text(parent), class: "button radius small inline-block" %>
<!-- if user is moderator <% if can? :comment_as_moderator, @debate %>
<div class="right"> <div class="right">
<%#= f.check_box :, label: false %> <%= f.check_box :as_moderator, label: false %>
<%#= f.label :, t("comments.form.comment_as_moderator"), class: "checkbox" %> <%= f.label :as_moderator, t("comments.form.comment_as_moderator"), class: "checkbox" %>
</div> </div>
end --> <% end %>
<!-- if user is admin <% if can? :comment_as_administrator, @debate %>
<div class="right"> <div class="right">
<%#= f.check_box :, label: false %> <%= f.check_box :as_administrator, label: false %>
<%#= f.label :, t("comments.form.comment_as_admin"), class: "checkbox" %> <%= f.label :as_administrator, t("comments.form.comment_as_admin"), class: "checkbox" %>
</div> </div>
end --> <% end %>
<% end %> <% end %>
</div> </div>

View File

@@ -168,4 +168,122 @@ feature 'Comments' do
expect(InappropiateFlag.flagged?(user, comment)).to_not be expect(InappropiateFlag.flagged?(user, comment)).to_not be
end end
feature "Moderators" do
scenario "can create comment as a moderator", :js do
moderator = create(:moderator)
debate = create(:debate)
login_as(moderator.user)
visit debate_path(debate)
fill_in "comment_body", with: "I am moderating!"
check "comment_as_moderator"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am moderating!"
expect(page).to have_content "Moderator ##{moderator.id}"
expect(page).to have_css "p.is-moderator"
expect(page).to have_css "img.moderator-avatar"
end
end
scenario "can create reply as a moderator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
moderator = create(:moderator, user: manuela)
debate = create(:debate)
comment = create(:comment, commentable: debate, user: citizen)
login_as(manuela)
visit debate_path(debate)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment_body", with: "I am moderating!"
check "comment_as_moderator"
click_button 'Publish reply'
end
within "#comment_#{comment.id}" do
expect(page).to have_content "I am moderating!"
expect(page).to have_content "Moderator ##{moderator.id}"
expect(page).to have_css "p.is-moderator"
expect(page).to have_css "img.moderator-avatar"
end
expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario "can not comment as an administrator" do
moderator = create(:moderator)
debate = create(:debate)
login_as(moderator.user)
visit debate_path(debate)
expect(page).to_not have_content "Comment as administrator"
end
end
feature "Administrators" do
scenario "can create comment as an administrator", :js do
admin = create(:administrator)
debate = create(:debate)
login_as(admin.user)
visit debate_path(debate)
fill_in "comment_body", with: "I am your Admin!"
check "comment_as_administrator"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "p.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "can create reply as an administrator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
admin = create(:administrator, user: manuela)
debate = create(:debate)
comment = create(:comment, commentable: debate, user: citizen)
login_as(manuela)
visit debate_path(debate)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment_body", with: "Top of the world!"
check "comment_as_administrator"
click_button 'Publish reply'
end
within "#comment_#{comment.id}" do
expect(page).to have_content "Top of the world!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "p.is-admin"
expect(page).to have_css "img.admin-avatar"
end
expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario "can not comment as a moderator" do
admin = create(:administrator)
debate = create(:debate)
login_as(admin.user)
visit debate_path(debate)
expect(page).to_not have_content "Comment as moderator"
end
end
end end