From fe2106d321467743b8a69182a9ee6c4156f47c43 Mon Sep 17 00:00:00 2001 From: Felipe Murakami <13895820+fhmurakami@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:48:10 +0000 Subject: [PATCH] Add option to remove participant from group and remove module name from participants for cleaner URLs --- app/controllers/groups_controller.rb | 13 +++++++-- .../user/participants_controller.rb | 27 ++++++++++--------- app/models/group.rb | 6 ++++- app/models/user.rb | 4 +++ app/views/groups/_group.html.erb | 25 +++++++++++------ app/views/home/index.html.erb | 4 +-- app/views/user/participants/_form.html.erb | 1 + app/views/user/participants/edit.html.erb | 2 +- app/views/user/participants/index.html.erb | 10 +++---- app/views/user/participants/new.html.erb | 2 +- app/views/user/participants/show.html.erb | 4 +-- config/locales/pt-BR.yml | 5 +++- config/routes.rb | 16 ++++++++--- 13 files changed, 81 insertions(+), 38 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 56a7877..ebe0480 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -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 @@ -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! @@ -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. diff --git a/app/controllers/user/participants_controller.rb b/app/controllers/user/participants_controller.rb index 9d537cb..44eb2d3 100644 --- a/app/controllers/user/participants_controller.rb +++ b/app/controllers/user/participants_controller.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index c8e0d49..a8b9499 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index dacd8b5..fc115b7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,8 @@ module User + def self.use_relative_model_naming? + true + end + def self.table_name_prefix "user_" end diff --git a/app/views/groups/_group.html.erb b/app/views/groups/_group.html.erb index 2b6ff80..7223978 100644 --- a/app/views/groups/_group.html.erb +++ b/app/views/groups/_group.html.erb @@ -5,21 +5,21 @@
+ | <%= t("activerecord.models.user/participant").pluralize %> | |||||||
---|---|---|---|---|---|---|---|---|
<%= t "helpers.label.user_participant.id" %> | -<%= t "helpers.label.user_participant.full_name" %> | -<%= t "helpers.label.user_participant.birth_date" %> | -<%= t "participants.show" %> | +<%= t "helpers.label.participant.id" %> | +<%= t "helpers.label.participant.full_name" %> | +<%= t "helpers.label.participant.birth_date" %> | +<%= t "helpers.actions" %> | |
<%= participant.id %> @@ -33,13 +33,22 @@ | <%= link_to t("participants.show"), participant, class: "text-xs" %> | ++ <%= + 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?") } + %> + | ||||||
- <%= link_to t("participants.new"), new_user_participant_path(group_id: group.id), class: "btn btn-small text-s pumpkin" %> + | + <%= link_to t("participants.new"), new_participant_path(group_id: group.id), class: "btn btn-small text-s pumpkin" %> |