diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index c30f02233..7de7cad0e 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -51,6 +51,16 @@ class DebatesController < ApplicationController set_debate_votes(@debate) end + def flag_as_inappropiate + InappropiateFlag.flag!(current_user, @debate) + respond_with @debate, template: 'debates/_refresh_flag_as_inappropiate_actions' + end + + def undo_flag_as_inappropiate + InappropiateFlag.unflag!(current_user, @debate) + respond_with @debate, template: 'debates/_refresh_flag_as_inappropiate_actions' + end + private def debate_params diff --git a/app/views/debates/_flag_as_inappropiate_actions.html.erb b/app/views/debates/_flag_as_inappropiate_actions.html.erb new file mode 100644 index 000000000..8003600e0 --- /dev/null +++ b/app/views/debates/_flag_as_inappropiate_actions.html.erb @@ -0,0 +1,6 @@ +<% if can? :flag_as_inappropiate, debate %> + <%= link_to t('shared.flag_as_inappropiate'), flag_as_inappropiate_debate_path(debate), method: :put, remote: true %> +<% end %> +<% if can? :undo_flag_as_inappropiate, debate %> + <%= link_to t('shared.undo_flag_as_inappropiate'), undo_flag_as_inappropiate_debate_path(debate), method: :put, remote: true %> +<% end %> diff --git a/app/views/debates/_refresh_flag_as_inappropiate_actions.js.erb b/app/views/debates/_refresh_flag_as_inappropiate_actions.js.erb new file mode 100644 index 000000000..7be008e35 --- /dev/null +++ b/app/views/debates/_refresh_flag_as_inappropiate_actions.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@debate) %> .js-flag-as-inappropiate-actions").html('<%= j render("debates/flag_as_inappropiate_actions", debate: @debate) %>'); diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 03c7e2c8c..71a8aa59d 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -41,6 +41,10 @@  •    <%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %> + + + <%= render 'debates/flag_as_inappropiate_actions', debate: @debate %> + <%= @debate.description %> diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index eade338f5..c685725d8 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -315,4 +315,37 @@ feature 'Debates' do end end + scenario "Flagging as inappropiate", :js do + user = create(:user) + debate = create(:debate) + + login_as(user) + visit debate_path(debate) + + within "#debate_#{debate.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, debate)).to be + end + + scenario "Undoing flagging as inappropiate", :js do + user = create(:user) + debate = create(:debate) + InappropiateFlag.flag!(user, debate) + + login_as(user) + visit debate_path(debate) + + within "#debate_#{debate.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, debate)).to_not be + end + end