diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index a10d80da9..e1d94f9b0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -22,6 +22,16 @@ class CommentsController < ApplicationController respond_with @comment end + def flag_as_inappropiate + InappropiateFlag.flag!(current_user, @comment) + respond_with @comment + end + + def undo_flag_as_inappropiate + InappropiateFlag.unflag!(current_user, @comment) + respond_with @comment + end + private def comment_params diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index d0317d52f..3d5b470d5 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -37,6 +37,11 @@ <% end %>  • <%= time_ago_in_words(comment.created_at) %> + + + <%= render 'comments/flag_as_inappropiate_actions', comment: comment %> + + <% if comment.user.official? %> diff --git a/app/views/comments/_flag_as_inappropiate_actions.html.erb b/app/views/comments/_flag_as_inappropiate_actions.html.erb new file mode 100644 index 000000000..fad83d4d6 --- /dev/null +++ b/app/views/comments/_flag_as_inappropiate_actions.html.erb @@ -0,0 +1,6 @@ +<% if can? :flag_as_inappropiate, comment %> + <%= link_to t('shared.flag_as_inappropiate'), flag_as_inappropiate_comment_path(comment), method: :put, remote: true %> +<% end %> +<% if can? :undo_flag_as_inappropiate, comment %> + <%= link_to t('shared.undo_flag_as_inappropiate'), undo_flag_as_inappropiate_comment_path(comment), method: :put, remote: true %> +<% end %> diff --git a/app/views/comments/flag_as_inappropiate.js.erb b/app/views/comments/flag_as_inappropiate.js.erb new file mode 100644 index 000000000..58f356f2b --- /dev/null +++ b/app/views/comments/flag_as_inappropiate.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment) %> .js-flag-as-inappropiate-actions").html('<%= j render("comments/flag_as_inappropiate_actions", comment: @comment) %>'); diff --git a/app/views/comments/undo_flag_as_inappropiate.js.erb b/app/views/comments/undo_flag_as_inappropiate.js.erb new file mode 100644 index 000000000..58f356f2b --- /dev/null +++ b/app/views/comments/undo_flag_as_inappropiate.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment) %> .js-flag-as-inappropiate-actions").html('<%= j render("comments/flag_as_inappropiate_actions", comment: @comment) %>'); diff --git a/config/routes.rb b/config/routes.rb index 66b74546b..ebeb85df1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,10 +13,18 @@ Rails.application.routes.draw do root 'welcome#index' resources :debates do - member { post :vote } + member do + post :vote + put :flag_as_inappropiate + put :undo_flag_as_inappropiate + end resources :comments, only: :create, shallow: true do - member { post :vote } + member do + post :vote + put :flag_as_inappropiate + put :undo_flag_as_inappropiate + end end end diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index b820fffcd..50083e236 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -133,4 +133,39 @@ feature 'Comments' do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end + scenario "Flagging as inappropiate", :js do + user = create(:user) + debate = create(:debate) + comment = create(:comment, commentable: debate) + + login_as(user) + visit debate_path(debate) + + within "#comment_#{comment.id}" do + expect(page).to_not have_link "Undo flag as inappropiate" + click_on 'Flag as inappropiate' + expect(page).to have_link "Undo flag as inappropiate" + end + + expect(InappropiateFlag.flagged?(user, comment)).to be + end + + scenario "Undoing flagging as inappropiate", :js do + user = create(:user) + debate = create(:debate) + comment = create(:comment, commentable: debate) + InappropiateFlag.flag!(user, comment) + + login_as(user) + visit debate_path(debate) + + within "#comment_#{comment.id}" do + expect(page).to_not have_link("Flag as inappropiate", exact: true) + click_on 'Undo flag as inappropiate' + expect(page).to have_link("Flag as inappropiate", exact: true) + end + + expect(InappropiateFlag.flagged?(user, comment)).to_not be + end + end