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 28 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
63 changes: 63 additions & 0 deletions app/controllers/memberships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class MembershipsController < ApplicationController
before_action :set_group
before_action :assure_admin

# 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_alert(format, redirect: edit_group_url(@group), alert: 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
unless user_signed_in?
redirect_to new_user_session_path, notice: t(:login_first)
return false
end
true
end

def assure_admin
assure_signed_in
unless current_user.admin_in? @group
redirect_to groups_url, notice: t(:only_admins)
return false
end
true
end

def set_group
@group = Group.find(params[:group_id])
end

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

def respond_with_alert(format, redirect:, alert:)
format.html { redirect_to redirect, alert: alert }
format.json { head :no_content }
end
end
23 changes: 23 additions & 0 deletions app/views/groups/_modal-add-member.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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: t(:member_email_placeholder), 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(:group_add_member), :class => "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
3 changes: 1 addition & 2 deletions app/views/groups/_modal-delete.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><%= t(:cancel) %></button>
<%= button_to t(:delete_group), group, method: :delete, class: "btn btn-primary" %>
</div>

</div>
</div>
</div>
</div>
11 changes: 9 additions & 2 deletions app/views/groups/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@
<tr>
<td><%= user.full_name %></td>
<td><%= user.email %></td>
<% if not user.admin_in?(@group) %>
<td>
<%= mail_to user.email, class: "btn btn-sm btn-secondary" do %>
<i class="bi bi-chat-fill"></i>
<% end %>
<a href="#" class="btn btn-sm btn-primary"><i class="bi bi-person-dash-fill"></i></a>
<%= 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 %>
</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" %>
<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) %>
Expand Down
7 changes: 7 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 geben Sie die E-Mail-Adresse des Nutzers ein, den Sie zur Gruppe hinzufügen möchten."
group_no_items: "Keine Items vorhanden"

group_name : "Gruppenname"
Expand All @@ -22,10 +24,15 @@ de:

member_name: "Name"
member_email: "E-Mail"
member_email_placeholder: "[email protected]"

login_first: "Du musst dich erst anmelden"
only_admins: "Nur Administratoren dürfen diese Gruppe bearbeiten"

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"
8 changes: 8 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_no_items: "No items present"

group_name : "Groupname"
Expand All @@ -22,10 +24,16 @@ en:

member_name: "Name"
member_email: "E-mail"
member_email_placeholder: "[email protected]"

login_first: "You have to log in first"
only_admins: "Only admins are allowed to edit this group"

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"

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# 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, to: "memberships#add_user"
patch :remove_user, to: "memberships#remove_user"
end

resources :items do
patch :reserve
patch :borrow
Expand Down