From cfd25042b6e5ec6c7aae24059e1798a407cbcc16 Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Wed, 19 Aug 2015 18:58:52 +0200 Subject: [PATCH 1/8] Adds styles for comment as moderator --- app/assets/images/moderator_avatar.png | Bin 0 -> 1926 bytes app/assets/stylesheets/debates.scss | 5 +++++ app/assets/stylesheets/participacion.scss | 23 ++++++++++++++-------- app/assets/stylesheets/variables.scss | 3 ++- app/views/comments/_comment.html.erb | 19 +++++++++++++++--- app/views/comments/_form.html.erb | 9 ++++++++- config/locales/en.yml | 4 +++- config/locales/es.yml | 4 +++- 8 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 app/assets/images/moderator_avatar.png diff --git a/app/assets/images/moderator_avatar.png b/app/assets/images/moderator_avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..1c726e57beee873d24c8d300ce31bb149b600154 GIT binary patch literal 1926 zcmaJ?X;c$e7#$L=xJ1-qkwqCtD72bP79u1PASR?x3FoIY?>uXCQzJt?x_7aOD9w@^XVzGD2RqWO2RdTrVd0`hf^(riK;cs8c`6U28CXw z!&R6Tv??NU%!ueV9STRF2F$2e5RHqSWp$NxiAq7Y;Es<+C5?-X2`mIGrYv%&G@5QV&?V4a3@y>ZYJ)`(_kTAYHI5KA|$~% z(*A2KHsOj-XJruzQ$3|Cn`d}=@zI_azm`9%C~W`f z$C|w^q~!B$jGrqfKIoLXdCqXnvFq#L7oC;eoe{Vy^ke%)Q+ge`*bQByZ8uhpx2*3F zK`HJp|N71jXvlq*7IY=dJuFV16@c}+nEyK66f!w8%Jts5vz@vUkR?CGWK6yY) ztY^0%{MzvnO!eW|d)IqgobOva+>Q=`Ll1gLicQ>9;NzmdDHmIBX4j34ZoFz&5jF5+ z!JPJTd4-dVT3Yed<=*X1zg%Bj*LG|Nb4;UIVmGR)uONq6EpKnHhC}xuQ&BsBWGZraX4F_% z;g=>+f|t^ zrL5S!6KD(av$wV<1#Ma!Bc6572N;~`K+A5o_eQm!VH18%atbOPZ(L8VIQ18xB zn>PE8cbZu^(LZa-VVlh6#nHDdSq|bRK`Qy z{5JWddAm+REo%p! <% end %> - +
<% if comment.user.hidden? %> <%= t("debates.comment.user_deleted") %> <% else %> - <%= comment.user.name %> + + <% if comment.user.moderator? %> + <%= t("debates.comment.moderator") %> + <% else %> + <%= comment.user.name %> + <% end %> + <% if comment.user.official? %>  •  @@ -52,6 +64,8 @@

<%= comment.body %>

<% elsif comment.user.official? %>

<%= comment.body %>

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

<%= comment.body %>

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

<%= comment.body %>

<% else %> @@ -83,6 +97,5 @@
<%= render comment.children.with_hidden.reorder('id DESC, lft') %>
-
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 5e34a9f84..45cdcecab 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -4,6 +4,13 @@ <%= f.hidden_field :commentable_type, value: parent.class %> <%= f.hidden_field :commentable_id, value: parent.id %> - <%= f.submit comment_button_text(parent), class: "button radius small" %> + <%= f.submit comment_button_text(parent), class: "button radius small inline-block" %> + + <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b47d06c44..ed6f1b2ae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -49,6 +49,7 @@ en: other: "%{count} votes" comment: author: Debate's author + moderator: Moderator deleted: This comment has been deleted user_deleted: Deleted user responses: @@ -94,7 +95,8 @@ en: recommendation_three: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore" comments: form: - leave_comment: Write a comment + leave_comment: Write a comment + comment_as_moderator: Comentar como moderador comments_helper: comment_link: Comment comment_button: Publish comment diff --git a/config/locales/es.yml b/config/locales/es.yml index ef903082f..31be240ba 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -49,6 +49,7 @@ es: other: "%{count} votos" comment: author: Autor del debate + moderator: Moderador deleted: Este comentario ha sido eliminado user_deleted: Usuario eliminado responses: @@ -94,7 +95,8 @@ es: recommendation_three: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore" comments: form: - leave_comment: Deja tu comentario + leave_comment: Deja tu comentario + comment_as_moderator: Comentar como moderador comments_helper: comment_link: Comentar comment_button: Publicar comentario From dffb8e8a5eb984b39a2cadca17e519f4220d6fea Mon Sep 17 00:00:00 2001 From: Alberto Garcia Cabeza Date: Sat, 22 Aug 2015 10:32:45 +0200 Subject: [PATCH 2/8] Adds styles for comment as administrator --- app/assets/images/admin_avatar.png | Bin 0 -> 1867 bytes app/assets/stylesheets/debates.scss | 9 +++++++-- app/assets/stylesheets/participacion.scss | 2 +- app/assets/stylesheets/variables.scss | 2 +- app/views/comments/_comment.html.erb | 14 +++++++++----- app/views/comments/_form.html.erb | 6 ++++++ config/locales/en.yml | 4 +++- config/locales/es.yml | 2 ++ 8 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 app/assets/images/admin_avatar.png diff --git a/app/assets/images/admin_avatar.png b/app/assets/images/admin_avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..28ea4a143f89d279bc28ed689dc229bebf9951f4 GIT binary patch literal 1867 zcmaJ?X;2eq7~Y`La0#M9RMaIw%a!aVAtyu(IWR&HV-RqPx+EKjB-xlOB;bKs6%?_G z2UFm? zFZa>z004L?rbt!93OZl13-RrxbX5||C|s6;&%kuJ4Y8nrSc|2jpu&h`pehv6=FV?M zqX1w~uwI>lrzqov8qCN*oEV1PXeQVI5EX4VBbqD}2h-6Ey-7rUdZLaB>a`+jGDit3 z%@Q^0gm>eE2f)@_5U?vBG*$^A1v)IB2SQxEaD`hX5zS62thWRjbURmFiQr+6bJ+m%z{`fI)R{Db4@s6r<<&_0S{8tst+(}hI^N+XA zr6+xO$*kPD9hk8$dv(LKD~<~jiKn!*Tlr#>~=Xf>OA$wSB72d)3yIa21i zvU#ePt);LNY;+4CEx+-yXFE^p`+I!YrG;T5I@Xu7CqPXLpE*8saY{SF64sLzgkE@= zQ#Qo&^nz1eaxk={=oD$qJVyL=t)p~P;JCw%v#~2(hkA%AcKsUp))?OPj#Uy_8$H@q zkWK|QHCh_HJBQxgI)}sEcIQ6oe{JCnWNcHjU**)Hsv~htd){2myuXcu@pp8AQ9#vN zq3Gf?xMf*U_w$@Ff;rs=?#;5{0r~c4_sgfZUCr|;dsj%24?ZesHTeJOy*pX8uA;Tm zA0g2=958op9sl|1M=g>58Q0X7NVJ%0&JHCXe0zhm?@CM3!{CB@>}pD@t*z?5S8?xQnlnPuES}n0B4KIJ;moS3(7nX32DJ6`pC`?IilkgcC0U89&;8dD7ks zZz*>D>#)05&F|xi5@8Qgr}$QI;#LK1h&sn5Hs|~4GZS4EkFNusiQ9KM>JL3k z+E3qq<#GEdMi8HTF}2|0nL_=6*LPk;59;-s3qa)g;m;$6k8%Fq6tV
@@ -28,7 +28,9 @@ <% else %> <% if comment.user.moderator? %> - <%= t("debates.comment.moderator") %> + <%= t("debates.comment.moderator") %> #ID + <% elsif comment.user.administrator? %> + <%= t("debates.comment.admin") %> #ID <% else %> <%= comment.user.name %> <% end %> @@ -64,8 +66,10 @@

<%= comment.body %>

<% elsif comment.user.official? %>

<%= comment.body %>

- <% elsif comment.user.moderator? %> + <% 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 45cdcecab..438537627 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -12,5 +12,11 @@ <%#= f.label :, t("comments.form.comment_as_moderator"), class: "checkbox" %>
end --> + <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index ed6f1b2ae..ce3339b25 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -50,6 +50,7 @@ en: comment: author: Debate's author moderator: Moderator + admin: Administrator deleted: This comment has been deleted user_deleted: Deleted user responses: @@ -96,7 +97,8 @@ en: comments: form: leave_comment: Write a comment - comment_as_moderator: Comentar como moderador + comment_as_moderator: Comment as moderator + comment_as_admin: Comment as administrator comments_helper: comment_link: Comment comment_button: Publish comment diff --git a/config/locales/es.yml b/config/locales/es.yml index 31be240ba..871cc023c 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -50,6 +50,7 @@ es: comment: author: Autor del debate moderator: Moderador + admin: Administrador deleted: Este comentario ha sido eliminado user_deleted: Usuario eliminado responses: @@ -97,6 +98,7 @@ es: form: leave_comment: Deja tu comentario comment_as_moderator: Comentar como moderador + comment_as_admin: Comentar como administrador comments_helper: comment_link: Comentar comment_button: Publicar comentario From 3085f265d8c67a09b0a977a5e619689546388479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 13:17:58 +0200 Subject: [PATCH 3/8] adds abilities for commenting as moderator/admin --- app/models/ability.rb | 5 +++++ spec/models/ability_spec.rb | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 9032038a1..a29f09a60 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -66,10 +66,15 @@ class Ability cannot :hide, User, id: user.id end + if user.moderator? + can :comment_as_moderator, [Debate, Comment] + end + if user.administrator? can :restore, Comment can :restore, Debate can :restore, User + can :comment_as_administrator, [Debate, Comment] end end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 1ff4933b0..ff3e3800c 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -22,13 +22,15 @@ describe Ability do it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } - it { should be_able_to(:show, user) } it { should be_able_to(:edit, user) } it { should be_able_to(:create, Comment) } it { should be_able_to(:vote, Comment) } + it { should_not be_able_to(:comment_as_administrator, debate) } + it { should_not be_able_to(:comment_as_moderator, debate) } + describe 'flagging content as inappropiate' do it { should be_able_to(:flag_as_inappropiate, debate) } it { should_not be_able_to(:undo_flag_as_inappropiate, debate) } @@ -158,6 +160,9 @@ describe Ability do it { should_not be_able_to(:restore, comment) } it { should_not be_able_to(:restore, debate) } it { should_not be_able_to(:restore, other_user) } + + it { should be_able_to(:comment_as_moderator, debate) } + it { should_not be_able_to(:comment_as_administrator, debate) } end end @@ -173,5 +178,8 @@ describe Ability do it { should be_able_to(:restore, comment) } it { should be_able_to(:restore, debate) } it { should be_able_to(:restore, other_user) } + + it { should be_able_to(:comment_as_administrator, debate) } + it { should_not be_able_to(:comment_as_moderator, debate) } end end From 42348511e15d950cbd468adfd300b62c6c849b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 17:22:21 +0200 Subject: [PATCH 4/8] adds moderator_id to comments --- db/migrate/20150824111844_add_moderator_id_to_comment.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 db/migrate/20150824111844_add_moderator_id_to_comment.rb diff --git a/db/migrate/20150824111844_add_moderator_id_to_comment.rb b/db/migrate/20150824111844_add_moderator_id_to_comment.rb new file mode 100644 index 000000000..014c3ecf3 --- /dev/null +++ b/db/migrate/20150824111844_add_moderator_id_to_comment.rb @@ -0,0 +1,5 @@ +class AddModeratorIdToComment < ActiveRecord::Migration + def change + add_column :comments, :moderator_id, :integer, default: nil + end +end From 46643185f85fece6af7697dd646c71739e6bd372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 24 Aug 2015 17:22:31 +0200 Subject: [PATCH 5/8] adds administrator_id to comments --- db/migrate/20150824113326_add_administrator_id_to_comment.rb | 5 +++++ db/schema.rb | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150824113326_add_administrator_id_to_comment.rb diff --git a/db/migrate/20150824113326_add_administrator_id_to_comment.rb b/db/migrate/20150824113326_add_administrator_id_to_comment.rb new file mode 100644 index 000000000..7be8274cb --- /dev/null +++ b/db/migrate/20150824113326_add_administrator_id_to_comment.rb @@ -0,0 +1,5 @@ +class AddAdministratorIdToComment < ActiveRecord::Migration + def change + add_column :comments, :administrator_id, :integer, default: nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 040d21d10..12e457a1c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150821180155) do +ActiveRecord::Schema.define(version: 20150824113326) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -52,6 +52,8 @@ ActiveRecord::Schema.define(version: 20150821180155) do t.datetime "flagged_as_inappropiate_at" t.integer "inappropiate_flags_count", default: 0 t.datetime "reviewed_at" + t.integer "moderator_id" + t.integer "administrator_id" end add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree From 57228636c4e40e1dbbf59fb130aa1e2431fabff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 17:47:09 +0200 Subject: [PATCH 6/8] adds as_moderator/administrator methods to comment --- app/models/comment.rb | 11 ++++++++++- spec/models/comment_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 7c18efc6b..fc05349fa 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,10 +1,11 @@ class Comment < ActiveRecord::Base include ActsAsParanoidAliases acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count - acts_as_paranoid column: :hidden_at acts_as_votable + attr_accessor :comment_as_moderator, :comment_as_administrator + validates :body, presence: true validates :user, presence: true @@ -59,6 +60,14 @@ class Comment < ActiveRecord::Base reviewed_at.present? end + def as_administrator? + administrator_id.present? + end + + def as_moderator? + moderator_id.present? + end + def mark_as_reviewed update(reviewed_at: Time.now) end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 5af7afc1c..98420c000 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -38,4 +38,24 @@ describe Comment do expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) end end + + describe "#as_administrator?" do + it "should be true if comment has administrator_id, false otherway" do + expect(comment).not_to be_as_administrator + + comment.administrator_id = 33 + + expect(comment).to be_as_administrator + end + end + + describe "#as_moderator?" do + it "should be true if comment has moderator_id, false otherway" do + expect(comment).not_to be_as_moderator + + comment.moderator_id = 21 + + expect(comment).to be_as_moderator + end + end end 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 7/8] 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 From 82fb56e4c062ac46e0972ce7fd04e3f48c7a2956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 18:59:53 +0200 Subject: [PATCH 8/8] refactors comments controller ugly method --- app/controllers/comments_controller.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 18ffdbc97..798196ca0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -44,9 +44,9 @@ class CommentsController < ApplicationController end def check_for_special_comments - if ["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, debate) + if administrator_comment? @comment.administrator_id = current_user.administrator.id - elsif ["1", true].include?(comment_params[:as_moderator]) && can?(:comment_as_moderator, debate) + elsif moderator_comment? @comment.moderator_id = current_user.moderator.id end end @@ -71,4 +71,12 @@ class CommentsController < ApplicationController reply? && parent.author.email_on_comment_reply? end + def administrator_comment? + ["1", true].include?(comment_params[:as_administrator]) && can?(:comment_as_administrator, Debate) + end + + def moderator_comment? + ["1", true].include?(comment_params[:as_moderator]) && can?(:comment_as_moderator, Debate) + end + end