From 3bc51a13cc0ffde28a51e2601fe448b9eead3614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Os=C3=B3rio?= Date: Thu, 1 Oct 2020 19:55:25 +0100 Subject: [PATCH] Fixes 500 error when viewing the "Tag Mods" page without having the role on the database (#10515) * Makes the ModeratorsQuery not return an exception on invalid role The current implementation of the query would yield an exception when recieving an invalid state parameter. As per the [GH issue discussion](https://github.com/forem/forem/issues/10060#issuecomment-692295217), Zhao recommended to change its behaviour and not trigger an exception in this condition. This commit does just that. If the state argument is invalid, the query now returns an empty result set. * Adds a warning when there are no matching mods * Re-trigger the build --- app/queries/admin/moderators_query.rb | 2 +- app/views/admin/mods/index.html.erb | 65 +++++++++++---------- spec/queries/admin/moderators_query_spec.rb | 2 +- spec/requests/admin/mods_spec.rb | 7 +++ 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/app/queries/admin/moderators_query.rb b/app/queries/admin/moderators_query.rb index b8b95d9ab..2de7bb2a9 100644 --- a/app/queries/admin/moderators_query.rb +++ b/app/queries/admin/moderators_query.rb @@ -24,7 +24,7 @@ module Admin end def self.role_id_for(role) - Role.find_by!(name: role).id + Role.find_by(name: role)&.id end def self.search_relation(relation, search) diff --git a/app/views/admin/mods/index.html.erb b/app/views/admin/mods/index.html.erb index 8790623a2..5163d61d9 100644 --- a/app/views/admin/mods/index.html.erb +++ b/app/views/admin/mods/index.html.erb @@ -19,40 +19,43 @@ <%= f.submit "Search", class: "btn btn-light" %> <% end %> +<% if @mods.empty? %> +
There are no mods matching your search criteria.
+<% else %> + <%= paginate @mods, theme: "internal" %> -<%= paginate @mods, theme: "internal" %> - - - - - - - - - - <% if params[:state] == "potential" %> - - <% end %> - - - - <% @mods.each do |mod| %> +
IDProfileCommentsBadgesLast CommentAction
+ - - - - - + + + + + <% if params[:state] == "potential" %> - + <% end %> - <% end %> - -
<%= mod.id %><%= link_to mod.username, admin_user_path(mod.id) %><%= mod.comments_count %><%= mod.badge_achievements_count %><%= time_ago_in_words mod.last_comment_at %> agoIDProfileCommentsBadgesLast Comment - <%= form_with model: [:admin, mod], url: admin_mod_path(mod.id), method: :patch, local: true do |f| %> - <%= f.submit "Make Trusted Mod", class: "btn btn-light js-add-to-mod-channel" %> - <% end %> - Action
+ + + <% @mods.each do |mod| %> + + <%= mod.id %> + <%= link_to mod.username, admin_user_path(mod.id) %> + <%= mod.comments_count %> + <%= mod.badge_achievements_count %> + <%= time_ago_in_words mod.last_comment_at %> ago + <% if params[:state] == "potential" %> + + <%= form_with model: [:admin, mod], url: admin_mod_path(mod.id), method: :patch, local: true do |f| %> + <%= f.submit "Make Trusted Mod", class: "btn btn-light js-add-to-mod-channel" %> + <% end %> + + <% end %> + + <% end %> + + -<%= paginate @mods, theme: "internal" %> + <%= paginate @mods, theme: "internal" %> +<% end %> diff --git a/spec/queries/admin/moderators_query_spec.rb b/spec/queries/admin/moderators_query_spec.rb index 0a4afbed4..89269c38a 100644 --- a/spec/queries/admin/moderators_query_spec.rb +++ b/spec/queries/admin/moderators_query_spec.rb @@ -38,7 +38,7 @@ RSpec.describe Admin::ModeratorsQuery, type: :query do context "when state does not exist" do let(:options) { { state: "non_existent_role" } } - it { within_block_is_expected.to raise_error(ActiveRecord::RecordNotFound) } + it { is_expected.to match_array([]) } end end end diff --git a/spec/requests/admin/mods_spec.rb b/spec/requests/admin/mods_spec.rb index 6d24703c6..28d626016 100644 --- a/spec/requests/admin/mods_spec.rb +++ b/spec/requests/admin/mods_spec.rb @@ -35,6 +35,13 @@ RSpec.describe "/admin/mods", type: :request do end end + context "when the are no matching mods" do + it "displays an warning" do + get "/admin/mods?search=no-results&state=tag_moderator" + expect(response.body).to include("There are no mods matching your search criteria") + end + end + it "displays mod user" do get "/admin/mods" expect(response.body).to include(moderator.username)