Skip to content

Commit

Permalink
Simple widget to rename users
Browse files Browse the repository at this point in the history
We've had a request by one user so far, and it would be nicer to not
have a dev mess around the DB manually
  • Loading branch information
PencilAmazing committed Jan 18, 2025
1 parent f147e6f commit 13d9d1f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,28 @@ def manage_roles
end
end

def rename_user
user = User.find(params[:id])
if user.blank?
head status: :not_found
return
end

unless params[:rename].present?
flash[:alert] = "No username provided"
redirect_to user_path(user.username)
return
end

if user.update(username: params[:rename])
flash[:notice] = "User renamed successfully"
else
flash[:alert] = "Username invalid: " +
user.errors.full_messages.join("</br>")
end
redirect_to user_path(user.reload.username)
end

private

def user_params
Expand Down
6 changes: 6 additions & 0 deletions app/views/users/_tablist.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
<a class="dropdown-item dropdown-item-danger" id="quick-access-tab" data-bs-toggle="tab" href="#quick-access-manager" role="tab"
aria-controls="profile" aria-selected="false">Quick Access</a>
</li>
<li class="nav-item">
<a class="dropdown-item dropdown-item-danger" id="user-rename-tab" data-bs-toggle="tab" href="#user-rename"
role="tab" aria-controls="profile" aria-selected="false">
Rename User
</a>
</li>
<li class="nav-item">
<a class="dropdown-item dropdown-item-danger" id="users-tab" data-bs-toggle="tab" href="#user-delete"
role="tab" aria-controls="profile" aria-selected="false">
Expand Down
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
<div class="tab-pane fade" id="quick-access-manager" role="tabpanel" aria-labelledby="quick-access-tab">
<%= render partial: 'users/tabs/quick_access', locals: { user: @user, repo_user: @repo_user } %>
</div>
<div class="tab-pane fade" id="user-rename" role="tabpanel" aria-labelledby="user-rename-tab">
<%= render partial: 'users/tabs/rename', locals: { user: @user, repo_user: @repo_user } %>
</div>
<% unless @repo_user.deleted? %>
<div class="tab-pane fade" id="user-delete" role="tabpanel" aria-labelledby="profile-tab">
<%= render partial: 'users/tabs/delete' %>
Expand Down
14 changes: 14 additions & 0 deletions app/views/users/tabs/_rename.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="tab-content">
<div class="tab-pane active" role="tabpanel">
<%= form_with url: rename_user_admin_users_path, method: :patch, autocomplete: :off do |f| %>
<%= f.hidden_field :id, value: @repo_user.id %>
<div class="row">
<%= f.label :rename, 'Rename user to:', class: 'col-form-label col-lg-auto' %>
<div class="input-group col-lg-4">
<%= f.text_field :rename, placeholder: @repo_user.username, autocomplete: :off, class: 'form-control' %>
<%= f.submit 'Update username', class: 'btn btn-primary' %>
</div>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
patch "restore_user"
put "toggle_lock_user"
get "manage_roles"
patch :rename_user
# patch "mass_update_roles", to: "users#mass_update_roles"
end
# member do
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,28 @@
expect(@user_two.staff_spaces.pluck :space_id).to eq []
end
end

context "renaming users" do
it "should rename users" do
@user = create :user
new_name = "newName"
patch :rename_user, params: { id: @user.id, rename: new_name }
expect(response).not_to have_http_status :not_found
expect(response).to redirect_to user_path(new_name)
expect(flash[:alert]).to be_nil
expect(@user.reload.username).to eq new_name
end

it "should error if username is already in use" do
@user = create :user
existing_username = "existingUsername"
@prev_user = create :user, username: existing_username
patch :rename_user, params: { id: @user.id, rename: existing_username }
expect(response).to redirect_to(user_path(@user.username))
expect(flash[:alert]).not_to be_nil
expect(flash[:notice]).to be_nil
expect(@user.reload.username).not_to eq existing_username
end
end
end
end

0 comments on commit 13d9d1f

Please sign in to comment.