From b340d7741ad8454c9fe384d755813cf06560ad73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 20 Aug 2015 13:04:31 +0200 Subject: [PATCH] adds hiding users by moderators --- .../moderation/debates_controller.rb | 1 + .../moderation/users_controller.rb | 15 +++++++ app/models/comment.rb | 6 ++- app/models/debate.rb | 2 +- app/views/comments/_actions.html.erb | 7 +++- app/views/comments/_comment.html.erb | 12 +++--- app/views/debates/_actions.html.erb | 6 +++ app/views/debates/show.html.erb | 6 +-- config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + config/routes.rb | 4 ++ lib/acts_as_paranoid_aliases.rb | 10 +++++ spec/features/moderation/users_spec.rb | 42 +++++++++++++++++++ 13 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 app/controllers/moderation/users_controller.rb create mode 100644 spec/features/moderation/users_spec.rb diff --git a/app/controllers/moderation/debates_controller.rb b/app/controllers/moderation/debates_controller.rb index cb5599092..622116765 100644 --- a/app/controllers/moderation/debates_controller.rb +++ b/app/controllers/moderation/debates_controller.rb @@ -4,4 +4,5 @@ class Moderation::DebatesController < Moderation::BaseController @debate = Debate.find(params[:id]) @debate.hide end + end \ No newline at end of file diff --git a/app/controllers/moderation/users_controller.rb b/app/controllers/moderation/users_controller.rb new file mode 100644 index 000000000..5dc2178a6 --- /dev/null +++ b/app/controllers/moderation/users_controller.rb @@ -0,0 +1,15 @@ +class Moderation::UsersController < Moderation::BaseController + + def hide + user = User.find(params[:id]) + debates_ids = Debate.where(author_id: user.id).pluck(:id) + comments_ids = Comment.where(user_id: user.id).pluck(:id) + + user.hide + Debate.hide_all debates_ids + Comment.hide_all comments_ids + + redirect_to debates_path + end + +end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index 0141c1ffa..e9f45edc3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,7 @@ class Comment < ActiveRecord::Base validates :user, presence: true belongs_to :commentable, polymorphic: true - belongs_to :user + belongs_to :user, -> { with_deleted } default_scope { includes(:user) } scope :recent, -> { order(id: :desc) } @@ -36,6 +36,10 @@ class Comment < ActiveRecord::Base votes_for.size end + def not_visible? + hidden? || user.hidden? + end + # TODO: faking counter cache since there is a bug with acts_as_nested_set :counter_cache # Remove when https://github.com/collectiveidea/awesome_nested_set/issues/294 is fixed # and reset counters using diff --git a/app/models/debate.rb b/app/models/debate.rb index bd7936e73..6a411c346 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -11,7 +11,7 @@ class Debate < ActiveRecord::Base acts_as_taggable acts_as_paranoid column: :hidden_at - belongs_to :author, class_name: 'User', foreign_key: 'author_id' + belongs_to :author, -> {with_deleted}, class_name: 'User', foreign_key: 'author_id' validates :title, presence: true validates :description, presence: true diff --git a/app/views/comments/_actions.html.erb b/app/views/comments/_actions.html.erb index ecc9bc9c0..8cc1b114f 100644 --- a/app/views/comments/_actions.html.erb +++ b/app/views/comments/_actions.html.erb @@ -1,5 +1,10 @@  |  <%= link_to t("admin.actions.hide").capitalize, hide_moderation_comment_path(comment), - method: :put, remote: true, data: { confirm: t('admin.actions.confirm') } %> + method: :put, remote: true, data: { confirm: t('admin.actions.confirm') } %> + <% unless comment.user.hidden? %> +  |  + <%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(comment.user_id, debate_id: @debate.id), + method: :put, data: { confirm: t('admin.actions.confirm') } %> + <% end %> \ No newline at end of file diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index d0317d52f..eb6e1722d 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,21 +1,21 @@
- <% if comment.hidden? %> + <% if comment.not_visible? %> <%= t("debates.comment.deleted") %> <% else %> <%= avatar_image(comment.user, size: 32, class: 'left') %> - + <% end %>
- + <% else %> <%= comment.user.name %> <% if comment.user.official? %>  •  @@ -23,7 +23,7 @@ <%= comment.user.official_position %> <% end %> - + <% end %> <% if comment.user.verified_organization? %>  •  diff --git a/app/views/debates/_actions.html.erb b/app/views/debates/_actions.html.erb index 3300a7b1d..5a51749dc 100644 --- a/app/views/debates/_actions.html.erb +++ b/app/views/debates/_actions.html.erb @@ -1,2 +1,8 @@ <%= link_to t("admin.actions.hide").capitalize, hide_moderation_debate_path(debate), method: :put, remote: true, data: { confirm: t('admin.actions.confirm') } %> + +<% unless debate.author.hidden? %> +  |  + <%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(debate.author_id), + method: :put, data: { confirm: t('admin.actions.confirm') } %> +<% end %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 3a29ec96f..03c7e2c8c 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -14,12 +14,12 @@
<%= avatar_image(@debate.author, size: 32, class: 'author-photo') %> - + <% else %> <%= @debate.author.name %> @@ -29,7 +29,7 @@ <%= @debate.author.official_position %> <% end %> - + <% end %> <% if @debate.author.verified_organization? %>  •  diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 7a3c6969b..0848824aa 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -31,6 +31,7 @@ en: rejected: Rejected actions: hide: Hide + hide_author: Ban author restore: Restore confirm: 'Are you sure?' tags: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index da29cff28..d10999291 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -31,6 +31,7 @@ es: rejected: Rechazadas actions: hide: Ocultar + hide_author: Bloquear al autor restore: Permitir confirm: '¿Estás seguro?' tags: diff --git a/config/routes.rb b/config/routes.rb index 66b74546b..120470ef0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -55,6 +55,10 @@ Rails.application.routes.draw do namespace :moderation do root to: "dashboard#index" + resources :users, only: [] do + member { put :hide } + end + resources :debates, only: [] do member { put :hide } end diff --git a/lib/acts_as_paranoid_aliases.rb b/lib/acts_as_paranoid_aliases.rb index 066ce3e29..dbb0d8350 100644 --- a/lib/acts_as_paranoid_aliases.rb +++ b/lib/acts_as_paranoid_aliases.rb @@ -20,6 +20,16 @@ module ActsAsParanoidAliases def only_hidden only_deleted end + + def hide_all(ids) + return if ids.blank? + where(id: ids).update_all(hidden_at: Time.now) + end + + def restore_all(ids) + return if ids.blank? + only_hidden.where(id: ids).update_all(hidden_at: nil) + end end end diff --git a/spec/features/moderation/users_spec.rb b/spec/features/moderation/users_spec.rb new file mode 100644 index 000000000..256ba43ea --- /dev/null +++ b/spec/features/moderation/users_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +feature 'Moderate users' do + + scenario 'Hide', :js do + citizen = create(:user) + moderator = create(:moderator) + + debate1 = create(:debate, author: citizen) + comment1 = create(:comment, user: citizen, commentable: debate1, body: 'SPAM') + debate2 = create(:debate, author: citizen) + comment2 = create(:comment, user: citizen, commentable: debate2, body: 'Hello') + + login_as(moderator.user) + + visit debates_path + + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) + + visit debate_path(debate1) + + within("#debate_#{debate1.id}") do + click_link 'Ban author' + end + + expect(current_path).to eq(debates_path) + expect(page).to_not have_content(debate1.title) + expect(page).to_not have_content(debate2.title) + + click_link("Logout") + + click_link 'Log in' + fill_in 'user_email', with: citizen.email + fill_in 'user_password', with: citizen.password + click_button 'Log in' + + expect(page).to have_content 'Invalid email or password' + expect(current_path).to eq(new_user_session_path) + end + +end \ No newline at end of file