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:
Javi Martín
2021-12-30 13:21:02 +01:00
parent a31e73bf23
commit e9d9789c25
4 changed files with 85 additions and 47 deletions

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

View File

@@ -0,0 +1,7 @@
class Moderation::Users::IndexComponent < ApplicationComponent
attr_reader :users
def initialize(users)
@users = users
end
end

View File

@@ -1,47 +1 @@
<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>
<%= render Moderation::Users::IndexComponent.new(@users) %>

View 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