Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user membership management to group management #219

Merged
merged 29 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c62f2ee
feat: add ui for adding users to groups
kbroschke Jan 8, 2023
990082c
Merge branch 'dev' into ba/group-add-user
RogerAK Jan 10, 2023
8d1a02e
feat: adding user to group
KiruChaff Jan 10, 2023
71081b7
fix: reverted one line in schema
KiruChaff Jan 10, 2023
56e7636
fix: change route from PUT to PATCH and use field input
TobiPeterG Jan 11, 2023
0054387
fix: fix comments
TobiPeterG Jan 11, 2023
6429ed8
fix: fix rubocop offenses
TobiPeterG Jan 11, 2023
e8ce15e
feat: hide remove user and chat button when user is admin
TobiPeterG Jan 11, 2023
53fb5e9
fix: add_user somewhat works *comments*
KiruChaff Jan 13, 2023
132b087
fix: revert schema
KiruChaff Jan 13, 2023
41372bc
feat: add working remove button
DieKautz Jan 16, 2023
a3f587e
fix: add user through modal
DieKautz Jan 16, 2023
c1d559b
refactor: apply lint suggestion
DieKautz Jan 16, 2023
2bb989d
feat: worked on edit group page
KiruChaff Jan 20, 2023
3c1d706
fix: small refactoring
KiruChaff Jan 20, 2023
5347c50
Merge branch 'dev' into ba/group-add-user
kbroschke Jan 22, 2023
aaf2b72
refactor: remove redundant group admin check
kbroschke Jan 22, 2023
eaad988
fix: add missing div closer
kbroschke Jan 22, 2023
ff4899e
feat: internationalize placeholder email
kbroschke Jan 22, 2023
42860a3
refactor: remove empty line
kbroschke Jan 22, 2023
f4f01f2
refactor: remove another redundant admin check
kbroschke Jan 22, 2023
f700677
refactor: move user managedment to own controller
kbroschke Jan 22, 2023
d2b2e45
fix: refactor German localization
TobiPeterG Jan 25, 2023
5332798
Merge branch 'dev' into ba/group-add-user
TobiPeterG Jan 25, 2023
c822634
fix: show alert when user is not found
TobiPeterG Jan 25, 2023
43c5675
fix: remove unused function
TobiPeterG Jan 25, 2023
dd2fe09
Merge branch 'dev' into ba/group-add-user
DieKautz Jan 27, 2023
bc5eaaa
Update button text in add user modal
kbroschke Jan 29, 2023
9b640c1
Merge branch 'dev' into ba/group-add-user
DieKautz Jan 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class GroupsController < ApplicationController
before_action :assure_signed_in
before_action :set_user_group
before_action :set_group, only: %i[ edit update destroy ]
before_action :assure_admin, only: %i[ edit update destroy ]
before_action :set_group_from_id, only: %i[ remove_user add_user ]
before_action :assure_admin, only: %i[ edit update destroy remove_user add_user ]

# GET /groups or /groups.json
def index
Expand Down Expand Up @@ -61,6 +62,32 @@ def leave
end
end

# PATCH /groups/1/remove_user or /groups/1/remove_user.json
def remove_user
user = User.find(params[:user])
respond_to do |format|
if user.memberships.destroy_by(group: @group)
respond_with_notice(format, redirect: edit_group_path(@group), notice: t(:group_user_removed))
else
unprocessable_response(format, redirect: :edit, entity: @group)
end
end
end

# PATCH /groups/1/add_user or /groups/1/add_user.json
def add_user
respond_to do |format|
user = User.where(email: params[:user][:email]).first
if user.blank?
respond_with_notice(format, redirect: edit_group_url(@group), notice: t(:group_user_not_found))
elsif Membership.where(user: user, group: @group, role: :member).first_or_create
respond_with_notice(format, redirect: edit_group_url(@group), notice: t(:group_user_added))
else
unprocessable_response(format, redirect: :edit, entity: @group)
end
end
end

private

def assure_signed_in
Expand Down Expand Up @@ -90,11 +117,20 @@ def set_group
@group = Group.find(params[:id])
end

def set_group_from_id
@group = Group.find(params[:group_id])
end
DieKautz marked this conversation as resolved.
Show resolved Hide resolved

def unprocessable_response(format, redirect:, entity:)
format.html { render redirect, status: :unprocessable_entity }
format.json { render json: entity.errors, status: :unprocessable_entity }
end

def respond_with_notice(format, redirect:, notice:)
format.html { redirect_to redirect, notice: notice }
format.json { head :no_content }
end

# Only allow a list of trusted parameters through.
def group_params
params.require(:group).permit(:name)
Expand Down
22 changes: 22 additions & 0 deletions app/views/groups/_modal-add-member.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="modal fade" id="addMemberGroupModal" tabindex="-1" aria-labelledby="addMemberGroupModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header border-0">
<h5 class="modal-title" id="addMemberGroupModalLabel"><%= t(:group_add_member) %></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= t(:cancel) %>"></button>
</div>
<div class="modal-body">
<%= form_with(model: User.new, url: group_add_user_path(@group), method: :patch, local: true) do |form| %>
<div>
<%= form.label :email, t(:group_add_member_prompt), class: "form-label" %>
<%= form.email_field :email, placeholder: "[email protected]", class: "form-control" %>

<div class="modal-footer border-0 justify-content-evenly">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><%= t(:cancel) %></button>
<%= form.submit t(:save), :class => "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
33 changes: 22 additions & 11 deletions app/views/groups/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,31 @@
<tr>
<td><%= user.full_name %></td>
<td><%= user.email %></td>
<td>
<a href="#" class="btn btn-sm btn-warning text-light"><i class="bi bi-chat-fill"></i></a>
<a href="#" class="btn btn-sm btn-primary"><i class="bi bi-person-dash-fill"></i></a>
</td>
<% if not user.admin_in?(@group) %>
<td>
<a href="#" class="btn btn-sm btn-warning text-light"><i class="bi bi-chat-fill"></i></a>
<% if @user.admin_in?(@group) %>
DieKautz marked this conversation as resolved.
Show resolved Hide resolved
<%= button_to group_remove_user_path(@group), method: :patch, params: {group: @group, user: user}, class: "btn btn-sm btn-primary", form: {class: "d-inline"} do %>
<i class="bi bi-person-dash-fill"></i>
<% end %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

<div class="d-flex justify-content-center">
<%= link_to t(:group_add_member), "", class: "btn btn-secondary mx-1" %>
<% if @user.admin_in?(@group) %>
<div class="d-flex justify-content-center">
<button type="button" class="btn btn-secondary mx-1" data-bs-toggle="modal" data-bs-target="#addMemberGroupModal">
<%= t(:group_add_member) %>
</button>
<%= render "modal-add-member", group: @group %>

<button type="button" class="btn btn-primary mx-1" data-bs-toggle="modal" data-bs-target="#deleteGroupModal">
<%= t(:delete_group) %>
</button>
<%= render "modal-delete", group: @group %>
</div>
<button type="button" class="btn btn-primary mx-1" data-bs-toggle="modal" data-bs-target="#deleteGroupModal">
<%= t(:delete_group) %>
</button>
<%= render "modal-delete", group: @group %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions config/locales/views/groups/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ de:
delete_group: "Gruppe löschen"
leave_group: "Gruppe verlassen"
edit_group: "Gruppe bearbeiten"

group_add_member: "Nutzer hinzufügen"
group_add_member_prompt: "Bitte gib die E-Mail-Adresse des Nutzers ein, den du zur Gruppe hinzufügen möchtest."

group_name : "Gruppenname"
group_name_prompt: "Bitte gib deiner Gruppe einen Namen"
Expand All @@ -28,3 +30,7 @@ de:
group_new: "Die Gruppe wurde erfolgreich erstellt"
group_update: "Die Gruppe wurde erfolgreich aktualisiert"
group_destroy: "Die Gruppe wurde wurde erfolgreich gelöscht"

group_user_not_found: "Dieser Nutzer konnte nicht gefunden werden"
group_user_added: "Nutzer wurde hinzugefügt"
group_user_removed: "Nutzer wurde entfernt"
6 changes: 6 additions & 0 deletions config/locales/views/groups/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ en:
delete_group: "Delete group"
leave_group: "Leave group"
edit_group: "Edit group"

group_add_member: "Add user"
group_add_member_prompt: "Please provide the email address of the user you'd like to add to this group."

group_name : "Groupname"
group_name_prompt: "Please provide a name for your group"
Expand All @@ -28,3 +30,7 @@ en:
group_new: "Group has been created successfully"
group_update: "Group has been updated successfully"
group_destroy: "Group has been deleted successfully"

group_user_not_found: "This user could not be found"
group_user_added: "User was successfully added"
group_user_removed: "User was successfully removed"
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
resources :groups, except: [:show, :new] do
post :leave
patch :add_user, :remove_user
end
resources :items do
patch :reserve
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "provider", limit: 50, default: "", null: false
t.string "uid", limit: 50, default: "", null: false
t.string "full_name", default: "", null: false
t.string "description", default: "", null: false
t.string "provider", limit: 50, default: "", null: false
t.string "uid", limit: 50, default: "", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Expand Down