Skip to content

Commit

Permalink
Umm... maybe don't show all users ☠️
Browse files Browse the repository at this point in the history
  • Loading branch information
northeastprince committed Nov 11, 2023
1 parent 69be6c5 commit e056477
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
15 changes: 13 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ class Admin::UsersController < Admin::BaseController
before_action :set_user, except: :index

def index
@pagy, @users = pagy User.all.order(created_at: :desc)
@email_address = params[:email_address]

if (user = User.find_by_email_address @email_address)
redirect_to admin_user_path(user)
else
flash.now[:notice] = "User not found."

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("flash", partial: "shared/flash") }

format.html { render :edit, status: :unprocessable_entity }
end
end

def show
Expand All @@ -12,7 +23,7 @@ def update
if @user.update(user_params)
redirect_to admin_user_path(@user)
else
flash.now[:notice] = @user.errors.full_messages.to_sentence
flash.now[:notice] = @user.errors.full_messages.first

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("flash", partial: "shared/flash") }
Expand Down
6 changes: 0 additions & 6 deletions app/views/admin/users/_snippet.html.erb

This file was deleted.

8 changes: 5 additions & 3 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<% @nav_active_item = admin_users_path %>
<h1 class="title">Users</h1>

<%= render partial: "snippet", collection: @users, as: :user %>

<%== pagy_nav(@pagy) %>
<%= form_with method: :get, class: "simple_form mt2" do |form| %>
<div style="display: flex">
<%= form.email_field :email_address, placeholder: "Email address", value: @email_address, style: "flex: 1" %>
</div>
<% end %>
10 changes: 6 additions & 4 deletions test/system/admin/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class Admin::UsersTest < ApplicationSystemTestCase
@user = User.first
end

test "listing users" do
test "searching for a user" do
visit admin_users_path

User.all.each do |user|
assert_text user.email_address
end
fill_in :email_address, with: "[email protected]\n"
assert_text /not found/i

fill_in :email_address, with: "#{@user.email_address}\n"
assert_redirected_to admin_user_path(@user)
end

test "editing a user's name" do
Expand Down

0 comments on commit e056477

Please sign in to comment.