diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb new file mode 100644 index 00000000..7ddba3bf --- /dev/null +++ b/app/controllers/memberships_controller.rb @@ -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 diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb new file mode 100644 index 00000000..b8ca7535 --- /dev/null +++ b/app/views/groups/_modal-add-member.html.erb @@ -0,0 +1,23 @@ + diff --git a/app/views/groups/_modal-delete.html.erb b/app/views/groups/_modal-delete.html.erb index ec2f1b87..b3f9581c 100644 --- a/app/views/groups/_modal-delete.html.erb +++ b/app/views/groups/_modal-delete.html.erb @@ -12,7 +12,6 @@ <%= button_to t(:delete_group), group, method: :delete, class: "btn btn-primary" %> - - \ No newline at end of file + diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index 584a8060..2d105525 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -24,19 +24,26 @@ <%= user.full_name %> <%= user.email %> + <% if not user.admin_in?(@group) %> <%= mail_to user.email, class: "btn btn-sm btn-secondary" do %> <% end %> - + <%= 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 %> + + <% end %> + <% end %> <% end %>
- <%= link_to t(:group_add_member), "", class: "btn btn-secondary mx-1" %> + + <%= render "modal-add-member", group: @group %>