From 030fd491201a97a80c20683a75880e3d8c512216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 18:17:04 +0200 Subject: [PATCH] allows comment as moderator/administrator Ref: #170 --- app/controllers/comments_controller.rb | 11 ++- app/models/comment.rb | 2 +- app/views/comments/_comment.html.erb | 84 +++++++++--------- app/views/comments/_form.html.erb | 24 ++--- spec/features/comments_spec.rb | 118 +++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 55 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 20df3bf2d..18ffdbc97 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -35,11 +35,20 @@ class CommentsController < ApplicationController private 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 def build_comment @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 def debate diff --git a/app/models/comment.rb b/app/models/comment.rb index fc05349fa..824f1dbf0 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,7 +4,7 @@ class Comment < ActiveRecord::Base acts_as_paranoid column: :hidden_at acts_as_votable - attr_accessor :comment_as_moderator, :comment_as_administrator + attr_accessor :as_moderator, :as_administrator validates :body, presence: true validates :user, presence: true diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 449619c9d..9e1c8485c 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -5,71 +5,73 @@ <%= t("debates.comment.deleted") %> <% 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? %> - - <% end %> - +
- <% if comment.user.hidden? %> - <%= t("debates.comment.user_deleted") %> + <% if comment.as_administrator? %> + <%= t("debates.comment.admin") %> #<%= comment.administrator_id%> + <% elsif comment.as_moderator? %> + <%= t("debates.comment.moderator") %> #<%= comment.moderator_id%> <% else %> - <% if comment.user.moderator? %> - <%= t("debates.comment.moderator") %> #ID - <% elsif comment.user.administrator? %> - <%= t("debates.comment.admin") %> #ID + <% if comment.user.hidden? %> + <%= t("debates.comment.user_deleted") %> <% else %> <%= comment.user.name %> + <% if comment.user.official? %> +  •  + + <%= comment.user.official_position %> + + <% end %> <% end %> - - <% if comment.user.official? %> + <% if comment.user.verified_organization? %>  •  - - <%= comment.user.official_position %> + + <%= t("shared.collective") %> <% end %> + <% if comment.user_id == @debate.author_id %> +  •  + + <%= t("debates.comment.author") %> + + <% end %> + <% end %> - <% if comment.user.verified_organization? %> -  •  - - <%= t("shared.collective") %> - - <% end %> - <% if comment.user_id == @debate.author_id %> -  •  - - <%= t("debates.comment.author") %> - - <% end %> +  • <%= time_ago_in_words(comment.created_at) %> <%= render 'comments/flag_as_inappropiate_actions', comment: comment %> -
- <% if comment.user.official? && comment.user_id == @debate.author_id %> + <% if comment.as_administrator? %> +

<%= comment.body %>

+ <% elsif comment.as_moderator? %> +

<%= comment.body %>

+ <% elsif comment.user.official? && comment.user_id == @debate.author_id %>

<%= comment.body %>

<% elsif comment.user.official? %>

<%= comment.body %>

- <% elsif comment.user.moderator? %> -

<%= comment.body %>

- <% elsif comment.user.administrator? %> -

<%= comment.body %>

<% elsif comment.user_id == @debate.author_id %>

<%= comment.body %>

<% else %> diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 438537627..b73023c7a 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -6,17 +6,17 @@ <%= f.submit comment_button_text(parent), class: "button radius small inline-block" %> - - + <% if can? :comment_as_moderator, @debate %> +
+ <%= f.check_box :as_moderator, label: false %> + <%= f.label :as_moderator, t("comments.form.comment_as_moderator"), class: "checkbox" %> +
+ <% end %> + <% if can? :comment_as_administrator, @debate %> +
+ <%= f.check_box :as_administrator, label: false %> + <%= f.label :as_administrator, t("comments.form.comment_as_admin"), class: "checkbox" %> +
+ <% end %> <% end %>
diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index 10a919a07..39babcda7 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -168,4 +168,122 @@ feature 'Comments' do expect(InappropiateFlag.flagged?(user, comment)).to_not be 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