Skip to content

Commit

Permalink
refactor(soft-delete-student-class): soft-delete when removing studen…
Browse files Browse the repository at this point in the history
…t from class
  • Loading branch information
phungmanhcuong committed Oct 9, 2024
1 parent c5dae21 commit 9863288
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ gem 'tzinfo-data', platforms: [:mswin, :mswin64]
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 7.0.8.4'

# Soft delete
gem 'paranoia', '~> 2.4'

# Use PostgreSQL for the backend
gem 'pg'

Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SHELL := /bin/zsh

# Loading all context of ~/.zshrc
.SHELLFLAGS := -c '. ~/.zshrc; exec "$$0"'
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
REMOTE := origin

clean:
echo "Preparing to setup soft-delete"
rails generate migration AddDeletedAtToCourseUsers deleted_at:datetime:index
rails db:migrate
27 changes: 27 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
class UsersController < ApplicationController
load_resource :user
before_action :set_course
before_action :set_course_user, only: [:destroy]

def show
if @user.built_in?
Expand All @@ -19,4 +21,29 @@ def other_instances
tenant = current_tenant
ActsAsTenant.without_tenant { Instance.containing_user(@user) - [tenant] }
end

def destroy
if @course_user.student?
@course_user.destroy
respond_to do |format|
format.html { redirect_to course_users_path(@course), notice: 'Student was successfully removed from the course.' }
format.json { head :no_content }
end
else
respond_to do |format|
format.html { redirect_to course_users_path(@course), alert: 'Only students can be removed.' }
format.json { render json: { error: 'Only students can be removed' }, status: :unprocessable_entity }
end
end
end

private

def set_course
@course = Course.find(params[:course_id])
end

def set_course_user
@course_user = @course.course_users.find(params[:id])
end
end
1 change: 1 addition & 0 deletions app/models/course_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class CourseUser < ApplicationRecord
include CourseUser::StaffConcern
include CourseUser::LevelProgressConcern
include CourseUser::TodoConcern
acts_as_paranoid

after_initialize :set_defaults, if: :new_record?
before_validation :set_defaults, if: :new_record?
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
post 'mark_as_read'
end

resources :courses do
resources :course_users, only: [:destroy]
end

resources :jobs, only: [:show]

resources :instance_user_role_requests, path: 'role_requests' do
Expand Down

0 comments on commit 9863288

Please sign in to comment.