From e9d9789c25a27957fc3f419710c4f07a279d82e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 30 Dec 2021 13:21:02 +0100 Subject: [PATCH] Move users moderation index view to a component This way it's easier to change the logic and write tests for it. --- .../moderation/users/index_component.html.erb | 47 ++++++++++++++++++ .../moderation/users/index_component.rb | 7 +++ app/views/moderation/users/index.html.erb | 48 +------------------ .../moderation/users/index_component_spec.rb | 30 ++++++++++++ 4 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 app/components/moderation/users/index_component.html.erb create mode 100644 app/components/moderation/users/index_component.rb create mode 100644 spec/components/moderation/users/index_component_spec.rb diff --git a/app/components/moderation/users/index_component.html.erb b/app/components/moderation/users/index_component.html.erb new file mode 100644 index 000000000..d5b995825 --- /dev/null +++ b/app/components/moderation/users/index_component.html.erb @@ -0,0 +1,47 @@ +
+

<%= t("moderation.users.index.title") %>

+ + <%= render Admin::SearchComponent.new(label: t("moderation.users.index.search_placeholder")) %> + + <% if users.present? %> +

<%= page_entries_info users %>

+ + + + + + + + <% users.each do |user| %> + + + + + <% end %> + +
<%= t("admin.hidden_users.index.user") %><%= t("admin.actions.actions") %>
+ <%= user.name %> + + <% 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 %> +
+ + <%= paginate users %> + <% end %> +
diff --git a/app/components/moderation/users/index_component.rb b/app/components/moderation/users/index_component.rb new file mode 100644 index 000000000..8e738032c --- /dev/null +++ b/app/components/moderation/users/index_component.rb @@ -0,0 +1,7 @@ +class Moderation::Users::IndexComponent < ApplicationComponent + attr_reader :users + + def initialize(users) + @users = users + end +end diff --git a/app/views/moderation/users/index.html.erb b/app/views/moderation/users/index.html.erb index 2e85185e3..39b526378 100644 --- a/app/views/moderation/users/index.html.erb +++ b/app/views/moderation/users/index.html.erb @@ -1,47 +1 @@ -
-

<%= t("moderation.users.index.title") %>

- - <%= render Admin::SearchComponent.new(label: t("moderation.users.index.search_placeholder")) %> - - <% if @users.present? %> -

<%= page_entries_info @users %>

- - - - - - - - <% @users.each do |user| %> - - - - - <% end %> - -
<%= t("admin.hidden_users.index.user") %><%= t("admin.actions.actions") %>
- <%= user.name %> - - <% 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 %> -
- - <%= paginate @users %> - <% end %> -
+<%= render Moderation::Users::IndexComponent.new(@users) %> diff --git a/spec/components/moderation/users/index_component_spec.rb b/spec/components/moderation/users/index_component_spec.rb new file mode 100644 index 000000000..49648ff7d --- /dev/null +++ b/spec/components/moderation/users/index_component_spec.rb @@ -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