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 @@ - - - - - + + + + - <% group.user_participants.each do |participant| %> + <% group.participants.each do |participant| %> + <% end %> - diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 7c5a3e0..081035f 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -18,8 +18,8 @@ <%= link_to t("groups.show_all"), groups_path, class: "btn btn-large lavender", method: :get, data: { turbo_method: :get } %> - <%#= 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 } %> diff --git a/app/views/user/participants/_form.html.erb b/app/views/user/participants/_form.html.erb index 47d7ed3..50676e5 100644 --- a/app/views/user/participants/_form.html.erb +++ b/app/views/user/participants/_form.html.erb @@ -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 %> + <%= form.select :group_ids, options_from_collection_for_select(Group.all, :id, :name), diff --git a/app/views/user/participants/edit.html.erb b/app/views/user/participants/edit.html.erb index af6facc..5a2b470 100644 --- a/app/views/user/participants/edit.html.erb +++ b/app/views/user/participants/edit.html.erb @@ -7,7 +7,7 @@ <%= render "form", user_participant: @user_participant %>
<%= link_to t("participants.show"), @user_participant %> - <%= link_to t("participants.back"), user_participants_path %> + <%= link_to t("participants.back"), participants_path %>
diff --git a/app/views/user/participants/index.html.erb b/app/views/user/participants/index.html.erb index 53f99d3..fc6d6d9 100644 --- a/app/views/user/participants/index.html.erb +++ b/app/views/user/participants/index.html.erb @@ -4,12 +4,12 @@ <%= render 'shared/header' %>

<%= t("activerecord.models.user/participant").pluralize %>

- <%= link_to t("participants.new"), new_user_participant_path, class: "btn pumpkin" %> -
- <% @user_participants.each do |user_participant| %> + <%= link_to t("participants.new"), new_participant_path, class: "btn pumpkin" %> +
+ <% @user_participants.each do |participant| %>
- <%= render user_participant %> - <%= link_to t("participants.show"), user_participant %> + <%= render participant %> + <%= link_to t("participants.show"), participant %>
<% end %>
diff --git a/app/views/user/participants/new.html.erb b/app/views/user/participants/new.html.erb index 1c2b603..3e708c9 100644 --- a/app/views/user/participants/new.html.erb +++ b/app/views/user/participants/new.html.erb @@ -7,7 +7,7 @@ <%= render "form", user_participant: @user_participant, group_id: @group_id %>
- <%= link_to t("participants.show_all"), user_participants_path %> + <%= link_to t("participants.show_all"), participants_path %> <%= link_to t("home.back"), root_path %>
diff --git a/app/views/user/participants/show.html.erb b/app/views/user/participants/show.html.erb index fd0dac9..51a4dbf 100644 --- a/app/views/user/participants/show.html.erb +++ b/app/views/user/participants/show.html.erb @@ -4,8 +4,8 @@
<%= render @user_participant %>
- <%= 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 } %>
diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 352f62d..12cb3e7 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -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" @@ -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" @@ -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" diff --git a/config/routes.rb b/config/routes.rb index c0f8a97..3717c29 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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"
+ <%= 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" %>