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
This commit is contained in:
Diogo Osório 2020-10-01 19:55:25 +01:00 committed by GitHub
parent c2e61074e1
commit 3bc51a13cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 33 deletions

View file

@ -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)

View file

@ -19,40 +19,43 @@
<%= f.submit "Search", class: "btn btn-light" %>
<% end %>
</div>
<% if @mods.empty? %>
<div class="alert alert-warning">There are no mods matching your search criteria.</div>
<% else %>
<%= paginate @mods, theme: "internal" %>
<%= paginate @mods, theme: "internal" %>
<table class="crayons-table" width="100%">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Profile</th>
<th scope="col">Comments</th>
<th scope="col">Badges</th>
<th scope="col">Last Comment</th>
<% if params[:state] == "potential" %>
<th scope="col">Action</th>
<% end %>
</tr>
</thead>
<tbody class="crayons-card">
<% @mods.each do |mod| %>
<table class="crayons-table" width="100%">
<thead>
<tr>
<td><%= mod.id %></td>
<td><%= link_to mod.username, admin_user_path(mod.id) %></td>
<td><%= mod.comments_count %></td>
<td><%= mod.badge_achievements_count %></td>
<td><%= time_ago_in_words mod.last_comment_at %> ago</td>
<th scope="col">ID</th>
<th scope="col">Profile</th>
<th scope="col">Comments</th>
<th scope="col">Badges</th>
<th scope="col">Last Comment</th>
<% if params[:state] == "potential" %>
<td>
<%= 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 %>
</td>
<th scope="col">Action</th>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</thead>
<tbody class="crayons-card">
<% @mods.each do |mod| %>
<tr>
<td><%= mod.id %></td>
<td><%= link_to mod.username, admin_user_path(mod.id) %></td>
<td><%= mod.comments_count %></td>
<td><%= mod.badge_achievements_count %></td>
<td><%= time_ago_in_words mod.last_comment_at %> ago</td>
<% if params[:state] == "potential" %>
<td>
<%= 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 %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= paginate @mods, theme: "internal" %>
<%= paginate @mods, theme: "internal" %>
<% end %>

View file

@ -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

View file

@ -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)