diff --git a/app/controllers/moderation/comments_controller.rb b/app/controllers/moderation/comments_controller.rb index 781f6b1e7..e44021e72 100644 --- a/app/controllers/moderation/comments_controller.rb +++ b/app/controllers/moderation/comments_controller.rb @@ -1,10 +1,12 @@ class Moderation::CommentsController < Moderation::BaseController - + before_filter :set_valid_filters, only: :index + before_filter :parse_filter, only: :index before_filter :load_comments, only: :index load_and_authorize_resource def index + @comments = @comments.send(@filter) @comments = @comments.page(params[:page]) end @@ -14,18 +16,27 @@ class Moderation::CommentsController < Moderation::BaseController def hide_in_moderation_screen @comment.hide - redirect_to action: :index + redirect_to request.query_parameters.merge(action: :index) end def mark_as_reviewed @comment.mark_as_reviewed - redirect_to action: :index + redirect_to request.query_parameters.merge(action: :index) end private def load_comments - @comments = Comment.accessible_by(current_ability, :hide).where('inappropiate_flags_count > 0').includes(:commentable) + @comments = Comment.accessible_by(current_ability, :hide).flagged_as_inappropiate.sorted_for_moderation.includes(:commentable) + end + + def set_valid_filters + @valid_filters = %w{all pending_review reviewed} + end + + def parse_filter + @filter = params[:filter] + @filter = 'all' unless @valid_filters.include?(@filter) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 5056414b2..7c18efc6b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -16,6 +16,11 @@ class Comment < ActiveRecord::Base default_scope { includes(:user) } scope :recent, -> { order(id: :desc) } + scope :sorted_for_moderation, -> { order(inappropiate_flags_count: :desc, updated_at: :desc) } + scope :pending_review, -> { where(reviewed_at: nil, hidden_at: nil) } + scope :reviewed, -> { where("reviewed_at IS NOT NULL AND hidden_at IS NULL") } + scope :flagged_as_inappropiate, -> { where("inappropiate_flags_count > 0") } + def self.build(commentable, user, body) new commentable: commentable, user_id: user.id, diff --git a/app/views/moderation/comments/index.html.erb b/app/views/moderation/comments/index.html.erb index 1d3a0e2f3..4393f4cb3 100644 --- a/app/views/moderation/comments/index.html.erb +++ b/app/views/moderation/comments/index.html.erb @@ -1,5 +1,17 @@
+ <%= t('moderation.comments.index.filter') %>: + <% @valid_filters.each do |filter| %> + <% if @filter == filter %> + <%= t("moderation.comments.index.filters.#{filter}") %> + <% else %> + <%= link_to t("moderation.comments.index.filters.#{filter}"), + moderation_comments_path(filter: filter) %> + <% end %> + <% end %> +
+| <%= link_to comment.commentable.title, comment.commentable %> | <%= comment.body %> | - <%= link_to t('moderation.comments.index.hide'), hide_in_moderation_screen_moderation_comment_path(comment), method: :put %> + <%= link_to t('moderation.comments.index.hide'), hide_in_moderation_screen_moderation_comment_path(comment, request.query_parameters), method: :put %> | <% if can? :mark_as_reviewed, comment %>- <%= link_to t('moderation.comments.index.mark_as_reviewed'), mark_as_reviewed_moderation_comment_path(comment), method: :put %> + <%= link_to t('moderation.comments.index.mark_as_reviewed'), mark_as_reviewed_moderation_comment_path(comment, request.query_parameters), method: :put %> | <% end %> <% if comment.reviewed? %> diff --git a/config/locales/moderation.en.yml b/config/locales/moderation.en.yml index 192da6fe3..107d95518 100644 --- a/config/locales/moderation.en.yml +++ b/config/locales/moderation.en.yml @@ -14,4 +14,9 @@ en: hide: Hide mark_as_reviewed: Mark as reviewed reviewed: Reviewed + filter: Filter + filters: + all: All + pending_review: Pending + reviewed: Reviewed diff --git a/config/locales/moderation.es.yml b/config/locales/moderation.es.yml index 421b50a78..7f53cdb3d 100644 --- a/config/locales/moderation.es.yml +++ b/config/locales/moderation.es.yml @@ -14,4 +14,10 @@ es: hide: Ocultar mark_as_reviewed: Marcar como revisado reviewed: Revisado + filter: Filtrar + filters: + all: Todos + pending_review: Pendientes + reviewed: Revisados + diff --git a/spec/features/moderation/comments_spec.rb b/spec/features/moderation/comments_spec.rb index ef3970b20..b6cf463c1 100644 --- a/spec/features/moderation/comments_spec.rb +++ b/spec/features/moderation/comments_spec.rb @@ -120,6 +120,64 @@ feature 'Moderate Comments' do expect(@comment.reload).to be_reviewed end + + scenario "Current filter is properly highlighted" do + visit moderation_comments_path + expect(page).to_not have_link('All') + expect(page).to have_link('Pending') + expect(page).to have_link('Reviewed') + + visit moderation_comments_path(filter: 'all') + expect(page).to_not have_link('All') + expect(page).to have_link('Pending') + expect(page).to have_link('Reviewed') + + visit moderation_comments_path(filter: 'pending_review') + expect(page).to have_link('All') + expect(page).to_not have_link('Pending') + expect(page).to have_link('Reviewed') + + visit moderation_comments_path(filter: 'reviewed') + expect(page).to have_link('All') + expect(page).to have_link('Pending') + expect(page).to_not have_link('Reviewed') + end + + scenario "Filtering comments" do + create(:comment, :flagged_as_inappropiate, body: "Pending comment") + create(:comment, :flagged_as_inappropiate, :hidden, body: "Hidden comment") + create(:comment, :flagged_as_inappropiate, :reviewed, body: "Reviewed comment") + + visit moderation_comments_path(filter: 'all') + expect(page).to have_content('Pending comment') + expect(page).to_not have_content('Hidden comment') + expect(page).to have_content('Reviewed comment') + + visit moderation_comments_path(filter: 'pending_review') + expect(page).to have_content('Pending comment') + expect(page).to_not have_content('Hidden comment') + expect(page).to_not have_content('Reviewed comment') + + visit moderation_comments_path(filter: 'reviewed') + expect(page).to_not have_content('Pending comment') + expect(page).to_not have_content('Hidden comment') + expect(page).to have_content('Reviewed comment') + end + + scenario "Reviewing links remember the pagination setting and the filter" do + per_page = Kaminari.config.default_per_page + (per_page + 2).times { create(:comment, :flagged_as_inappropiate) } + + visit moderation_comments_path(filter: 'pending_review', page: 2) + + click_link('Mark as reviewed', match: :first) + + uri = URI.parse(current_url) + query_params = Rack::Utils.parse_nested_query(uri.query).symbolize_keys + + expect(query_params[:filter]).to eq('pending_review') + expect(query_params[:page]).to eq('2') + end end end