From 4056861b55425756bfd33eacfc1d746d3cc88832 Mon Sep 17 00:00:00 2001 From: Teal Bauer <git@teal.is> Date: Fri, 24 May 2024 12:01:13 +0200 Subject: [PATCH] refactoring & styling --- .../stylesheets/application.tailwind.css | 6 ++++ app/controllers/assignments_controller.rb | 32 +++++++++++++++++++ app/views/conferences/show.html.erb | 14 +++++--- app/views/sessions/_assignment_form.html.erb | 9 ++++++ app/views/sessions/show.html.erb | 25 +++++---------- config/routes.rb | 6 ++-- 6 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 app/views/sessions/_assignment_form.html.erb diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css index 79c3fa7..6f20685 100644 --- a/app/assets/stylesheets/application.tailwind.css +++ b/app/assets/stylesheets/application.tailwind.css @@ -15,3 +15,9 @@ h1, h2, h3, h4, h5, h6 { @apply font-bold; } +input[type=submit], button { + @apply border rounded-md px-2 py-1; + &.primary { + @apply bg-teal-800 text-teal-50; + } +} diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index 8209076..aa30625 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -1,8 +1,40 @@ class AssignmentsController < ApplicationController + before_action :set_session, :set_users + def index @assignments = Assignment.all if params[:user_id] @assignments = @assignments.where(user_id: params[:user_id]) end end + + def create + @user = User.find(params[:user_id]) + @assignment = @session.assignments.new(user: @user) + + if @assignment.save + redirect_to @session, notice: "User was successfully assigned to session." + else + flash[:alert] = @assignment.errors.full_messages.join(", ") + render 'sessions/show' + end + end + + def destroy + @assignment = @session.assignments.find_by(user_id: params[:user_id]) + @assignment.destroy + respond_to do |format| + format.html { redirect_to @session, notice: "User assignment removed successfully." } + end + end + + private + + def set_session + @session = Session.find_by(ref_id: params[:session_ref_id]) + end + + def set_users + @users = User.all + end end diff --git a/app/views/conferences/show.html.erb b/app/views/conferences/show.html.erb index 911fe41..767c8a1 100644 --- a/app/views/conferences/show.html.erb +++ b/app/views/conferences/show.html.erb @@ -56,11 +56,15 @@ current_time = @sessions_by_date[@conference.days.first].first.starts_at.advance <h4><%= link_to session.title, [@conference, session] %></h4> <p class="session-time"><%= session.starts_at.strftime('%H:%M') %> - <%= session.ends_at.strftime('%H:%M') %></p> <%#<div class="session-desc"><%= session.description.html_safe %><%#/div>%> - <%= form_with url: assign_user_session_path(@conference, session), method: :post, local: true do |f| %> - <%= f.label :user_id, "Assign User" %> - <%= f.select :user_id, options_from_collection_for_select(@users, :id, :name) %> - <%= f.submit "Assign" %> - <% end %> + <ul class="list-disc"> + <% session.assignments.each do |assignment| %> + <li> + <span class="assigned-user"><%= assignment.user.name %></span> + <%= link_to '[Remove]', conference_session_assignments_path(session.conference, session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %> + </li> + <% end %> + </ul> + <%= render partial: "sessions/assignment_form", locals: { session: session } %> </div> <% end %> </div> diff --git a/app/views/sessions/_assignment_form.html.erb b/app/views/sessions/_assignment_form.html.erb new file mode 100644 index 0000000..3092c10 --- /dev/null +++ b/app/views/sessions/_assignment_form.html.erb @@ -0,0 +1,9 @@ +<%= form_with url: conference_session_assignments_path(session.conference, session), method: :post, local: true do |f| %> + <%= f.select :user_id, options_from_collection_for_select(@users, :id, :name) %> + <%= f.submit "Assign", class: 'primary' %> + <% if @assignment&.errors&.any? %> + <div class="alert alert-danger"> + <%= @assignment.errors.full_messages.join(", ") %> + </div> + <% end %> +<% end %> diff --git a/app/views/sessions/show.html.erb b/app/views/sessions/show.html.erb index 32b19c4..c77f779 100644 --- a/app/views/sessions/show.html.erb +++ b/app/views/sessions/show.html.erb @@ -8,27 +8,18 @@ <h3>Assigned Users</h3> <ul> - <% @session.users.each do |user| %> + <% @session.assignments.each do |assignment| %> <li> - <%= user.name %> - <%= link_to '[Remove]', unassign_user_session_path([@session.conference, @session], user_id: user.id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %> + <%= assignment.user.name %> + <%= link_to '[Remove]', conference_session_assignments_path(@session.conference, @session, user_id: assignment.user_id), data: { turbo_method: :delete, confirm: 'Are you sure?' } %> </li> <% end %> </ul> - <%= form_with url: assign_user_session_path([@session.conference, @session]), method: :post, local: true do |f| %> - <%= f.label :user_id, "Assign User" %> - <%= f.select :user_id, options_from_collection_for_select(@users, :id, :name) %> - <%= f.submit "Assign" %> - <div id="flash"> - <% flash.each do |key, value| %> - <div class="alert alert-<%= key %>"><%= value %></div> - <% end %> - </div> - <% if @assignment&.errors&.any? %> - <div class="alert alert-danger"> - <%= @assignment.errors.full_messages.join(", ") %> - </div> + <div id="flash"> + <% flash.each do |key, value| %> + <div class="alert alert-<%= key %>"><%= value %></div> <% end %> - <% end %> + </div> + <%= render partial: "sessions/assignment_form", locals: { session: @session } %> </div> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2521790..ffe5506 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,12 +10,12 @@ Rails.application.routes.draw do root "conferences#index" resources :conferences, param: :slug do - resources :sessions do - resource :assigments + resources :sessions, param: :ref_id do + resource :assignments, only: [:create, :destroy] end end - resources :assignments + resources :assignments, only: [:index] resources :sessions, param: :ref_id # get 'conferences/:slug', to: 'conferences#show', as: :conference -- GitLab