Skip to content

Commit

Permalink
Edit groups and participants to have a HABTM association
Browse files Browse the repository at this point in the history
  • Loading branch information
fhmurakami committed Nov 25, 2024
1 parent 1a31bcc commit 23fc209
Show file tree
Hide file tree
Showing 25 changed files with 341 additions and 172 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"esbenp.prettier-vscode",
"ms-vscode.live-server",
"setobiralo.erb-commenter",
"vortizhe.simple-ruby-erb"
"vortizhe.simple-ruby-erb",
"eamodio.gitlens"
]
}
}
Expand Down
163 changes: 106 additions & 57 deletions app/assets/stylesheets/main.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import url(https://fonts.bunny.net/css?family=coiny:400|coming-soon:400|gamja-flower:400|gochi-hand:400|marck-script:400|neucha:400|pacifico:400|patrick-hand:400);
@import url(https://fonts.bunny.net/css?family=coiny:400);

:root {
--black: #2e2f30;
Expand Down Expand Up @@ -188,40 +188,6 @@ nav {
-webkit-text-stroke: 2px var(--white);
}

.btn {
width: 100%;
text-align: center;
font-size: 1rem;
color: var(--white);

border: 2px solid var(--black);
border-radius: 2vmin;

margin-top: 0.5rem;
padding: 0.5rem 1rem;
}

.btn-small {
border: 1px solid var(--black);
padding: 0.25rem 1rem;
border-radius: 1.5vmin;
}

.btn-large {
font-size: 1.5rem;
border: 3px solid var(--black);
margin-top: 2rem;
padding: 1rem 2rem;
}

.btn-header {
margin: 0;
}

.btn-text-black {
color: var(--black);
}

.buttons {
width: 80%;
display: flex;
Expand All @@ -231,11 +197,9 @@ nav {
font-size: 1.2rem;
}

.buttons .sets {
width: 100%;
}

.buttons .participants {
.buttons .sets,
.buttons .participants,
.buttons .groups {
width: 100%;
}

Expand Down Expand Up @@ -288,23 +252,26 @@ nav {
gap: 5vmin;
}

.participants {
.participants,
.groups {
width: 90%;
display: flex;
flex-direction: column;
/* align-items: center; */
/* gap: 5vmin; */
}

.participant h3 {
.participant h3,
.group h3 {
color: var(--pumpkin);
-webkit-text-stroke: 1px var(--black);
text-align: center;
font-size: 2.5rem;
justify-self: flex-start;
}

.participant p {
.participant p,
.group p {
-webkit-text-stroke: unset;
font-size: 1.5rem;
}
Expand All @@ -317,7 +284,14 @@ nav {
gap: 2vmin;
}

.card.participant {
#groups {
width: 100%;
display: flex;
flex-direction: column;
gap: 2vmin;
}

.card {
display: flex;
flex-direction: column;
justify-content: space-between;
Expand All @@ -327,21 +301,15 @@ nav {
padding: 2.5vmin;
}

.card.participant h3 {
.card h3 {
font-size: 2rem;
}

.card.participant p {
.card p {
font-size: 1.25rem;
color: var(--raspberry);
}

.participants a {
text-align: center;
font-size: 1.5rem;
margin: 2vmin auto;
}

.card a {
font-size: 1.5rem;
margin: 2vmin auto;
Expand All @@ -351,18 +319,36 @@ nav {
color: var(--blue);
}

.participants a,
.groups a {
text-align: center;
font-size: 1.5rem;
margin: 2vmin auto;
}

.form form {
display: flex;
flex-direction: column;
justify-content: center;
}

.form .field input {
.form .field input,
.form .field select {
display: block;
width: 100%;
}

.form .field input[type="checkbox"] {
display: inline-block;
width: fit-content;
}

.form .field select[multiple] {
height: 6rem;
}

.form .field select[multiple] option {
white-space: normal;
}

.actions {
Expand All @@ -382,10 +368,6 @@ nav {
color: var(--blue);
}

.actions a.btn:hover {
color: var(--white);
}

.actions input {
font-size: 1.2rem;
cursor: pointer;
Expand All @@ -407,3 +389,70 @@ nav {
a.forgot-password:hover {
color: var(--raspberry);
}

.btn {
width: 100%;
text-align: center;
color: var(--white);

border: 2px solid var(--black);
border-radius: 2vmin;

margin-top: 0.5rem;
padding: 0.5rem 1rem;
}

.btn-small {
font-size: 1rem;
border: 1px solid var(--black);
padding: 0.25rem 1rem;
border-radius: 1.5vmin;
}

.btn-large {
font-size: 1.5rem;
border: 3px solid var(--black);
margin-top: 2rem;
padding: 1rem 2rem;
}

.btn-header {
margin: 0;
}

.btn-text-black {
color: var(--black);
}

a.btn:hover {
color: var(--white);
}

a.text-xs {
font-size: 1rem;
}

a.text-s {
font-size: 1.2rem;
}

a.text-normal {
font-size: 1.5rem;
}

table {
width: 100%;
border-collapse: collapse;
}

th,
td {
border: 1px solid var(--black);
padding: 0.5rem;
}

td.id-cell,
td.name-cell,
td.link-cell {
text-align: center;
}
22 changes: 11 additions & 11 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create

respond_to do |format|
if @group.save
format.html { redirect_to @group, notice: "Group was successfully created." }
format.html { redirect_to @group, notice: t("groups.created") }
format.json { render :show, status: :created, location: @group }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -38,7 +38,7 @@ def create
def update
respond_to do |format|
if @group.update(group_params)
format.html { redirect_to @group, notice: "Group was successfully updated." }
format.html { redirect_to @group, notice: t("groups.updated") }
format.json { render :show, status: :ok, location: @group }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -52,19 +52,19 @@ def destroy
@group.destroy!

respond_to do |format|
format.html { redirect_to groups_path, status: :see_other, notice: "Group was successfully destroyed." }
format.html { redirect_to groups_path, status: :see_other, notice: t("groups.destroyed") }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
end
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
end

# Only allow a list of trusted parameters through.
def group_params
params.require(:group).permit(:name, :user_admin_id)
end
# Only allow a list of trusted parameters through.
def group_params
params.require(:group).permit(:name, :user_admin_id)
end
end
6 changes: 6 additions & 0 deletions app/controllers/user/participants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def edit
# POST /user/participants or /user/participants.json
def create
@user_participant = User::Participant.new(user_participant_params)
@user_participant.groups << Group.find(group_ids)

respond_to do |format|
if @user_participant.save
Expand All @@ -36,6 +37,7 @@ def create

# PATCH/PUT /user/participants/1 or /user/participants/1.json
def update
@user_participant.groups << Group.find(group_ids)
respond_to do |format|
if @user_participant.update(user_participant_params)
format.html { redirect_to @user_participant, notice: "Participant was successfully updated." }
Expand Down Expand Up @@ -67,4 +69,8 @@ def set_user_participant
def user_participant_params
params.require(:user_participant).permit(:first_name, :last_name, :birth_date, :user_admin_id)
end

def group_ids
@group_ids ||= params[:user_participant][:group_ids].compact_blank
end
end
5 changes: 3 additions & 2 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Group < ApplicationRecord
belongs_to :user_admin, class_name: "User::Admin"
has_many :participants
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
# Rever associação
end
7 changes: 4 additions & 3 deletions app/models/user/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class User::Admin < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

validates :first_name, :last_name, presence: true
# has_many :user_participants, class_name: "User::Participant", foreign_key: :user_admin_id, inverse_of: :user_admin, dependent: :destroy
has_many :groups
has_many :participants, through: :groups
has_many :groups, foreign_key: :user_admin_id
has_many :user_participants, class_name: "User::Participant", foreign_key: :user_admin_id, inverse_of: :user_admin, dependent: :destroy

validates :first_name, :last_name, presence: true
end
10 changes: 7 additions & 3 deletions app/models/user/participant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class User::Participant < ApplicationRecord
belongs_to :group
module User
class Participant < ApplicationRecord
belongs_to :user_admin, class_name: "User::Admin"
has_and_belongs_to_many :groups, foreign_key: :user_participant_id, inverse_of: :user_participant
# has_many :user_admins, through: :groups

validates :first_name, :last_name, :birth_date, presence: true
validates :first_name, :last_name, :birth_date, presence: true
end
end
Loading

0 comments on commit 23fc209

Please sign in to comment.