Skip to content

Commit

Permalink
Merge pull request #7 from fhmurakami/feat/remove-participant-from-group
Browse files Browse the repository at this point in the history
Add option to remove participant from group
  • Loading branch information
fhmurakami authored Nov 28, 2024
2 parents a16901a + fe2106d commit c27a821
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 38 deletions.
13 changes: 11 additions & 2 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GroupsController < ApplicationController
before_action :set_group, only: %i[ show edit update destroy ]
before_action :set_group, only: %i[ show edit update destroy remove_participant ]

# GET /groups or /groups.json
def index
Expand Down Expand Up @@ -47,6 +47,11 @@ def update
end
end

def remove_participant
@group.remove_participant(participant)
redirect_to @group
end

# DELETE /groups/1 or /groups/1.json
def destroy
@group.destroy!
Expand All @@ -60,7 +65,11 @@ def destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
@group ||= Group.find(params[:id])
end

def participant
@participant ||= User::Participant.find(params[:participant_id])
end

# Only allow a list of trusted parameters through.
Expand Down
27 changes: 15 additions & 12 deletions app/controllers/user/participants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class User::ParticipantsController < ApplicationController
before_action :set_user_participant, only: %i[ show edit update destroy ]
before_action :set_participant, only: %i[ show edit update destroy ]

# GET /user/participants or /user/participants.json
def index
Expand All @@ -21,9 +21,8 @@ def edit

# POST /user/participants or /user/participants.json
def create
@user_participant = User::Participant.new(user_participant_params)
# TODO: Rever lógica para adicionar apenas novos grupos e remover dos antigos
@user_participant.groups << Group.find(group_ids)
@user_participant = User::Participant.new(participant_params)
add_participant_to_groups(group_ids)

respond_to do |format|
if @user_participant.save
Expand All @@ -38,10 +37,9 @@ def create

# PATCH/PUT /user/participants/1 or /user/participants/1.json
def update
# TODO: Rever lógica para adicionar apenas novos grupos e remover dos antigos
@user_participant.groups << Group.find(group_ids)
add_participant_to_groups(group_ids)
respond_to do |format|
if @user_participant.update(user_participant_params)
if @user_participant.update(participant_params)
format.html { redirect_to @user_participant, notice: "Participant was successfully updated." }
format.json { render :show, status: :ok, location: @user_participant }
else
Expand All @@ -56,23 +54,28 @@ def destroy
@user_participant.destroy!

respond_to do |format|
format.html { redirect_to user_participants_path, status: :see_other, notice: "Participant was successfully destroyed." }
format.html { redirect_to participants_path, status: :see_other, notice: "Participant was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user_participant
def set_participant
@user_participant = User::Participant.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_participant_params
params.require(:user_participant).permit(:first_name, :last_name, :birth_date, :user_admin_id)
def participant_params
params.require(:participant).permit(:first_name, :last_name, :birth_date, :user_admin_id)
end

def group_ids
@group_ids ||= params[:user_participant][:group_ids].compact_blank
@group_ids ||= params[:participant]&.delete(:group_ids)&.compact_blank
end

def add_participant_to_groups(group_ids)
participant_groups = @user_participant.groups
@user_participant.groups = Group.where(id: group_ids) unless participant_groups.ids.include?(group_ids)
end
end
6 changes: 5 additions & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Group < ApplicationRecord
belongs_to :user_admin, class_name: "User::Admin", dependent: :destroy
has_and_belongs_to_many :user_participants, class_name: "User::Participant", association_foreign_key: :user_participant_id, inverse_of: :user_participant
has_and_belongs_to_many :participants, class_name: "User::Participant", association_foreign_key: :user_participant_id

def remove_participant(participant)
participants.delete(participant)
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module User
def self.use_relative_model_naming?
true
end

def self.table_name_prefix
"user_"
end
Expand Down
25 changes: 17 additions & 8 deletions app/views/groups/_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
<table>
<thead>
<tr>
<th colspan="4">
<th colspan="5">
<strong>
<%= t("activerecord.models.user/participant").pluralize %>
</strong>
</th>
</tr>
<tr>
<th><%= t "helpers.label.user_participant.id" %></th>
<th><%= t "helpers.label.user_participant.full_name" %></th>
<th><%= t "helpers.label.user_participant.birth_date" %></th>
<th><%= t "participants.show" %></th>
<th><%= t "helpers.label.participant.id" %></th>
<th><%= t "helpers.label.participant.full_name" %></th>
<th><%= t "helpers.label.participant.birth_date" %></th>
<th colspan="2"><%= t "helpers.actions" %></th>
</tr>
</thead>
<tbody>
<% group.user_participants.each do |participant| %>
<% group.participants.each do |participant| %>
<tr>
<td>
<%= participant.id %>
Expand All @@ -33,13 +33,22 @@
<td>
<%= link_to t("participants.show"), participant, class: "text-xs" %>
</td>
<td>
<%=
button_to t("participants.remove_from_group"),
remove_participant_group_path(participant, group),
class: "btn btn-small crimson",
method: :patch,
data: { turbo_method: :patch, confirm: t("participants.remove_from_group?") }
%>
</td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<th colspan="4">
<%= link_to t("participants.new"), new_user_participant_path(group_id: group.id), class: "btn btn-small text-s pumpkin" %>
<th colspan="5">
<%= link_to t("participants.new"), new_participant_path(group_id: group.id), class: "btn btn-small text-s pumpkin" %>
</th>
</tr>
</tfoot>
Expand Down
4 changes: 2 additions & 2 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<%= link_to t("groups.show_all"), groups_path, class: "btn btn-large lavender", method: :get, data: { turbo_method: :get } %>
</div>
<!--<div class="buttons participants">-->
<%#= link_to t("participants.register"), new_user_participant_path, class: "btn btn-large green", method: :get, data: { turbo_method: :get } %>
<%#= link_to t("participants.show_all"), user_participants_path, class: "btn btn-large crimson", method: :get, data: { turbo_method: :get } %>
<%#= link_to t("participants.register"), new_participant_path, class: "btn btn-large green", method: :get, data: { turbo_method: :get } %>
<%#= link_to t("participants.show_all"), participants_path, class: "btn btn-large crimson", method: :get, data: { turbo_method: :get } %>
<!--</div>-->
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/user/participants/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<%#= form.label :group_id %>
<%#= form.select :group_id, current_admin.groups.collect { |g| [ g.name, g.id ] } %>
<%= form.label :group_ids %>
<!-- Verificar se utilizando Group.all irá retornar grupos de outros admins -->
<%=
form.select :group_ids,
options_from_collection_for_select(Group.all, :id, :name),
Expand Down
2 changes: 1 addition & 1 deletion app/views/user/participants/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%= render "form", user_participant: @user_participant %>
<div class="actions">
<%= link_to t("participants.show"), @user_participant %>
<%= link_to t("participants.back"), user_participants_path %>
<%= link_to t("participants.back"), participants_path %>
</div>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions app/views/user/participants/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<%= render 'shared/header' %>
<h2><%= t("activerecord.models.user/participant").pluralize %></h2>
<div class="participants">
<%= link_to t("participants.new"), new_user_participant_path, class: "btn pumpkin" %>
<div id="user_participants">
<% @user_participants.each do |user_participant| %>
<%= link_to t("participants.new"), new_participant_path, class: "btn pumpkin" %>
<div id="participants">
<% @user_participants.each do |participant| %>
<div class="card participant">
<%= render user_participant %>
<%= link_to t("participants.show"), user_participant %>
<%= render participant %>
<%= link_to t("participants.show"), participant %>
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/user/participants/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%= render "form", user_participant: @user_participant, group_id: @group_id %>
<br>
<div class="actions">
<%= link_to t("participants.show_all"), user_participants_path %>
<%= link_to t("participants.show_all"), participants_path %>
<%= link_to t("home.back"), root_path %>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/user/participants/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<div class="panel participant">
<%= render @user_participant %>
<div class="actions">
<%= link_to t("participants.edit"), edit_user_participant_path(@user_participant) %>
<%= link_to t("participants.back"), user_participants_path %>
<%= link_to t("participants.edit"), edit_participant_path(@user_participant) %>
<%= link_to t("participants.back"), participants_path %>
<%= button_to t("participants.delete"), @user_participant, class: "btn crimson", method: :delete, data: { turbo_method: :delete } %>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pt-BR:
delete: "Excluir participante"
edit: "Editar participante"
editing: "Editando participante"
remove_from_group: "Remover do grupo"
remove_from_group?: "Realmente deseja remover o participante do grupo?"
new: "Novo participante"
register: "Cadastrar participante"
show: "Ver participante"
Expand Down Expand Up @@ -204,6 +206,7 @@ pt-BR:
one: "Não foi possível gravar %{model}: %{count} erro"
other: "Não foi possível gravar %{model}: %{count} erros"
helpers:
actions: "Ações"
label:
collection:
name: "Nome"
Expand All @@ -213,7 +216,7 @@ pt-BR:
user_admin:
first_name: "Nome"
last_name: "Sobrenome"
user_participant:
participant:
first_name: "Nome"
last_name: "Sobrenome"
full_name: "Nome completo"
Expand Down
16 changes: 13 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
Rails.application.routes.draw do
scope "(:locale)", locale: /pt-BR|en/ do
resources :collections
resources :groups
namespace :user do
resources :participants
# resources :groups do
# resources :participants, module: :user do
# patch "remove", to: "groups#remove_participant", as: :remove
# end
# end
resources :participants, module: :user, only: [] do
resources :groups, only: [] do
member do
patch "remove", to: "/groups#remove_participant", as: :remove
end
end
end
resources :groups
resources :participants, module: :user
devise_for :admins, class_name: "User::Admin"
# Defines the root path route ("/")
root "home#index"
Expand Down

0 comments on commit c27a821

Please sign in to comment.