Move users moderation index view to a component
This way it's easier to change the logic and write tests for it.
This commit is contained in:
47
app/components/moderation/users/index_component.html.erb
Normal file
47
app/components/moderation/users/index_component.html.erb
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<main class="moderation-users-index">
|
||||||
|
<h2><%= t("moderation.users.index.title") %></h2>
|
||||||
|
|
||||||
|
<%= render Admin::SearchComponent.new(label: t("moderation.users.index.search_placeholder")) %>
|
||||||
|
|
||||||
|
<% if users.present? %>
|
||||||
|
<h3><%= page_entries_info users %></h3>
|
||||||
|
|
||||||
|
<table id="moderation_users" class="moderation-users">
|
||||||
|
<thead>
|
||||||
|
<th><%= t("admin.hidden_users.index.user") %></th>
|
||||||
|
<th><%= t("admin.actions.actions") %></th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<% users.each do |user| %>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<%= user.name %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<% if user.hidden? %>
|
||||||
|
<%= t("moderation.users.index.hidden") %>
|
||||||
|
<% else %>
|
||||||
|
<%= render Admin::TableActionsComponent.new(user, actions: []) do |actions| %>
|
||||||
|
<%= actions.action(
|
||||||
|
:hide,
|
||||||
|
text: t("moderation.users.index.hide"),
|
||||||
|
confirm: ->(name) { t("moderation.users.index.confirm_hide", name: name) },
|
||||||
|
method: :put
|
||||||
|
) %>
|
||||||
|
<%= actions.action(
|
||||||
|
:block,
|
||||||
|
text: t("moderation.users.index.block"),
|
||||||
|
confirm: ->(name) { t("moderation.users.index.confirm_block", name: name) },
|
||||||
|
method: :put
|
||||||
|
) %>
|
||||||
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<%= paginate users %>
|
||||||
|
<% end %>
|
||||||
|
</main>
|
||||||
7
app/components/moderation/users/index_component.rb
Normal file
7
app/components/moderation/users/index_component.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
class Moderation::Users::IndexComponent < ApplicationComponent
|
||||||
|
attr_reader :users
|
||||||
|
|
||||||
|
def initialize(users)
|
||||||
|
@users = users
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,47 +1 @@
|
|||||||
<main class="moderation-users-index">
|
<%= render Moderation::Users::IndexComponent.new(@users) %>
|
||||||
<h2><%= t("moderation.users.index.title") %></h2>
|
|
||||||
|
|
||||||
<%= render Admin::SearchComponent.new(label: t("moderation.users.index.search_placeholder")) %>
|
|
||||||
|
|
||||||
<% if @users.present? %>
|
|
||||||
<h3><%= page_entries_info @users %></h3>
|
|
||||||
|
|
||||||
<table id="moderation_users" class="moderation-users">
|
|
||||||
<thead>
|
|
||||||
<th><%= t("admin.hidden_users.index.user") %></th>
|
|
||||||
<th><%= t("admin.actions.actions") %></th>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<% @users.each do |user| %>
|
|
||||||
<tr>
|
|
||||||
<td>
|
|
||||||
<%= user.name %>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<% if user.hidden? %>
|
|
||||||
<%= t("moderation.users.index.hidden") %>
|
|
||||||
<% else %>
|
|
||||||
<%= render Admin::TableActionsComponent.new(user, actions: []) do |actions| %>
|
|
||||||
<%= actions.action(
|
|
||||||
:hide,
|
|
||||||
text: t("moderation.users.index.hide"),
|
|
||||||
confirm: ->(name) { t("moderation.users.index.confirm_hide", name: name) },
|
|
||||||
method: :put
|
|
||||||
) %>
|
|
||||||
<%= actions.action(
|
|
||||||
:block,
|
|
||||||
text: t("moderation.users.index.block"),
|
|
||||||
confirm: ->(name) { t("moderation.users.index.confirm_block", name: name) },
|
|
||||||
method: :put
|
|
||||||
) %>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<% end %>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<%= paginate @users %>
|
|
||||||
<% end %>
|
|
||||||
</main>
|
|
||||||
|
|||||||
30
spec/components/moderation/users/index_component_spec.rb
Normal file
30
spec/components/moderation/users/index_component_spec.rb
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe Moderation::Users::IndexComponent, controller: Moderation::UsersController do
|
||||||
|
describe "actions or status" do
|
||||||
|
let(:component) { Moderation::Users::IndexComponent.new(User.with_hidden.page(1)) }
|
||||||
|
|
||||||
|
it "shows actions to block or hide users" do
|
||||||
|
create(:user)
|
||||||
|
|
||||||
|
render_inline component
|
||||||
|
|
||||||
|
page.find("table") do |table|
|
||||||
|
expect(table).to have_button "Hide"
|
||||||
|
expect(table).to have_button "Block"
|
||||||
|
expect(table).not_to have_content "Blocked"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shows 'blocked' for hidden users" do
|
||||||
|
create(:user, :hidden)
|
||||||
|
|
||||||
|
render_inline component
|
||||||
|
|
||||||
|
page.find("table") do |table|
|
||||||
|
expect(table).to have_content "Blocked"
|
||||||
|
expect(table).not_to have_button
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user