Skip to content

Commit f064f73

Browse files
committed
refactoring controllers using decent exposure gem
1 parent 48fb086 commit f064f73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+194
-326
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ gem "dartsass-sprockets"
3838
gem "jquery-rails"
3939
gem "cocoon"
4040
gem "scenic"
41+
gem "decent_exposure", "~> 3.0"
4142
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
4243
gem "image_processing", "~> 1.2"
4344
gem "bootstrap", "~> 5.3.3"

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ GEM
114114
debug (1.9.2)
115115
irb (~> 1.10)
116116
reline (>= 0.3.8)
117+
decent_exposure (3.0.4)
118+
activesupport (>= 4.0)
117119
devise (4.9.4)
118120
bcrypt (~> 3.0)
119121
orm_adapter (~> 0.1)
@@ -426,6 +428,7 @@ DEPENDENCIES
426428
cocoon
427429
dartsass-sprockets
428430
debug
431+
decent_exposure (~> 3.0)
429432
devise (~> 4.9)
430433
dotenv-rails
431434
faker
Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
class Student::AssignmentsController < ApplicationController
2-
before_action :set_course
3-
before_action :ensure_student_enroll_course
4-
5-
def index
6-
@assignments = @course.assignments.all
7-
end
8-
9-
def show
10-
@student = current_student
11-
@assignment = @course.assignments.find(params[:id])
12-
end
13-
14-
private
15-
def set_course
16-
@course = Course.find(params[:course_id])
17-
end
2+
expose :course
3+
expose :assignment, parent: :course
4+
expose :assignments, from: :course
5+
expose :student, :current_student
6+
before_action :ensure_student_enroll_course
187

8+
private
199
def ensure_student_enroll_course
20-
unless current_student.student_courses.where(course_id: @course.id).exists?
21-
redirect_to select_student_courses_path, notice: t(".notice")
10+
unless student.student_courses.where(course_id: course.id).exists?
11+
redirect_to new_courses_student_courses_path, notice: t(".notice")
2212
end
2313
end
2414
end

app/controllers/student/comments_controller.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
class Student::CommentsController < ApplicationController
2+
expose :course
3+
expose :post, parent: :course
4+
expose :comment, parent: :post
25
before_action :ensure_student_enroll_course
36

47
def create
5-
@comment = Comment.new(comment_params)
6-
if @comment.save
7-
redirect_to student_course_posts_path(@comment.post.course)
8+
if comment.save
9+
redirect_to student_course_posts_path(course)
810
else
9-
Rails.logger.debug @comment.errors.full_messages.inspect # 👈 add this
10-
redirect_to student_course_posts_path(Course.find(params[:course_id])), alert: "Failed: #{@comment.errors.full_messages.join(', ')}"
11+
redirect_to student_course_posts_path(course), alert: "Failed: #{comment.errors.full_messages.join(', ')}"
1112
end
1213
end
1314

1415
def destroy
15-
@comment = Comment.find(params[:id])
16-
@post = @comment.post
17-
@comment.destroy
16+
comment.destroy
1817
respond_to do |format|
19-
format.html { redirect_to student_course_post_path(@post.course, @post), status: :see_other, notice: t(".notice") }
18+
format.html { redirect_to student_course_post_path(course, post), status: :see_other, notice: t(".notice") }
2019
format.json { head :no_content }
2120
end
2221
end
2322

2423
private
2524
def comment_params
26-
params.require(:comment).permit(:body, :post_id, :commenter_id, :commenter_type)
25+
params.require(:comment).permit(:body, :commenter_id, :commenter_type)
2726
end
2827
def ensure_student_enroll_course
29-
unless current_student.student_courses.where(course_id: params[:course_id]).exists?
28+
unless current_student.student_courses.where(course_id: course.id).exists?
3029
redirect_to select_student_courses_path, notice: t(".notice")
3130
end
3231
end

app/controllers/student/courses_controller.rb

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,41 @@
11
class Student::CoursesController < ApplicationController
2-
before_action :set_student
3-
before_action :set_course, only: %i[show]
4-
5-
def index
6-
@courses = @student.courses.all
7-
end
2+
expose :student, :current_student
3+
expose :course
4+
expose :courses, from: :student
85

96
def your_progress
10-
@student_assignments = @student.courses.map(&:assignments).flatten.count
11-
@submitted_assignments = @student.submissions.count
7+
@student_assignments = courses.map(&:assignments).flatten.count
8+
@submitted_assignments = student.submissions.count
129
end
1310

1411
def upcoming_assignments
15-
@assignments = UpcomingAssignment.where(student_id: @student.id)
12+
@assignments = UpcomingAssignment.where(student_id: student.id)
1613
end
1714

1815
def fetch_posts
19-
@posts= Post.joins(:course).where(course: { id: @student.course_ids })
16+
@posts= Post.joins(:course).where(course: { id: student.course_ids })
2017
end
2118
def new_courses
22-
@courses = Course.where.not(id: @student.course_ids)
19+
@courses = Course.where.not(id: student.course_ids)
2320
end
2421

2522
def enroll
26-
@course = Course.find(params[:course_id])
27-
current_student.courses << (@course)
23+
courses << course
2824

2925
respond_to do |format|
3026
format.html { redirect_to new_courses_student_courses_path, notice: t(".notice") }
3127
end
3228
end
3329

3430
def unenroll
35-
@course = Course.find(params[:course_id])
36-
current_student.courses.delete(@course)
31+
student.courses.delete(course)
3732

3833
respond_to do |format|
3934
format.html { redirect_to student_courses_path, notice: t(".notice") }
4035
end
4136
end
4237

4338
private
44-
def set_student
45-
@student = current_student
46-
end
47-
48-
def set_course
49-
begin
50-
@course = @student.courses.find(params[:id])
51-
rescue ActiveRecord::RecordNotFound
52-
redirect_to "/404"
53-
end
54-
end
55-
5639
def course_params
5740
params.require(:student).permit(:course_ids[])
5841
end
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
class Student::PostsController < ApplicationController
2-
before_action :set_student
3-
before_action :set_course
4-
before_action :set_post, only: :show
5-
6-
def index
7-
@posts = @course.posts
8-
end
9-
def show
10-
@post.comments.build
11-
end
2+
expose :course
3+
expose :post, parent: :course
4+
expose :posts, from: :course
5+
expose :student, :current_student
6+
before_action :ensure_student_enroll_course
127

138
private
14-
def set_student
15-
@student = current_student
16-
end
17-
18-
def set_course
19-
@course = @student.courses.find(params[:course_id])
20-
end
21-
22-
def set_post
23-
@post = @course.posts.find(params[:id])
24-
end
9+
def ensure_student_enroll_course
10+
unless student.student_courses.where(course_id: course.id).exists?
11+
redirect_to new_courses_student_courses_path, notice: t(".notice")
12+
end
13+
end
2514
end
Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
11
class Student::SubmissionsController < ApplicationController
2-
before_action :set_student
3-
before_action :set_course
4-
before_action :set_assignment
5-
before_action :set_submission, only: %i[ show]
6-
7-
def index
8-
@submissions = @assignment.submissions.where(student_id: @student.id)
9-
end
10-
11-
def show
12-
@submission = @assignment.submissions.where(student_id: @student.id)
13-
end
14-
15-
def new
16-
@submission = @assignment.submissions.build
17-
end
2+
expose :student, :current_student
3+
expose :course
4+
expose :assignment, parent: :course
5+
expose :submission, parent: :student
186

197
def create
20-
@submission = @assignment.submissions.build(submission_params)
21-
228
respond_to do |format|
23-
if @submission.save
24-
format.html { redirect_to student_course_assignment_submission_path(@course, @assignment, @submission), notice: t(".notice") }
25-
format.json { render :show, status: :created, location: @submission }
9+
if submission.save
10+
format.html do
11+
redirect_to student_course_assignment_submission_path(course, assignment, submission),
12+
notice: t(".notice")
13+
end
14+
format.json { render :show, status: :created, location: submission }
2615
else
2716
format.html { render :new, status: :unprocessable_entity }
28-
format.json { render json: @submission.errors, status: :unprocessable_entity }
17+
format.json { render json: submission.errors, status: :unprocessable_entity }
2918
end
3019
end
3120
end
3221

3322
private
34-
def set_student
35-
@student = current_student
36-
end
37-
38-
def set_course
39-
@course = @student.courses.find(params[:course_id])
40-
end
41-
42-
def set_assignment
43-
@assignment = @course.assignments.find(params[:assignment_id])
44-
end
45-
46-
def set_submission
47-
@submission = current_student.submissions.find_by(assignment_id: params[:assignment_id])
48-
end
4923

5024
def submission_params
51-
params.require(:submission).permit(:title, :file, :assignment_id, :student_id)
25+
params.require(:submission).permit(:title, :file, :assignment_id)
5226
end
5327
end

app/controllers/teacher/assignments_controller.rb

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
class Teacher::AssignmentsController < ApplicationController
2-
before_action :set_course
3-
before_action :set_assignment, only: %i[ show edit update destroy ]
2+
expose :teacher, :current_teacher
3+
expose :course
4+
expose :assignment, parent: :course
5+
expose :assignments, from: :course
46
before_action :ensure_teacher_teaches_course
57

6-
def index
7-
@assignments = @course.assignments.all
8-
end
9-
10-
def new
11-
@assignment = @course.assignments.build
12-
end
138

149
def create
15-
@assignment = @course.assignments.create(assignment_params)
16-
1710
respond_to do |format|
18-
if @assignment.save
11+
if assignment.save
1912
format.html { redirect_to teacher_course_assignments_path, notice: t(".notice") }
20-
format.json { render :show, status: :created, location: @assignment }
13+
format.json { render :show, status: :created, location: assignment }
2114
else
2215
format.html { render :new, status: :unprocessable_entity }
23-
format.json { render json: @assignment.errors, status: :unprocessable_entity }
16+
format.json { render json: assignment.errors, status: :unprocessable_entity }
2417
end
2518
end
2619
end
2720

2821
def update
2922
respond_to do |format|
30-
if @assignment.update(assignment_params)
31-
format.html { redirect_to [ :teacher, @course, @assignment ], notice: t(".notice") }
32-
format.json { render :show, status: :ok, location: @assignment }
23+
if assignment.update(assignment_params)
24+
format.html { redirect_to [ :teacher, course, assignment ], notice: t(".notice") }
25+
format.json { render :show, status: :ok, location: assignment }
3326
else
3427
format.html { render :edit, status: :unprocessable_entity }
35-
format.json { render json: @assignment.errors, status: :unprocessable_entity }
28+
format.json { render json: assignment.errors, status: :unprocessable_entity }
3629
end
3730
end
3831
end
3932

4033
def destroy
41-
@assignment.destroy!
34+
assignment.destroy!
4235

4336
respond_to do |format|
4437
format.html { redirect_to teacher_course_assignments_path, status: :see_other, notice: t(".notice") }
@@ -47,20 +40,12 @@ def destroy
4740
end
4841

4942
private
50-
def set_course
51-
@course = Course.find(params[:course_id])
52-
end
53-
54-
def set_assignment
55-
@assignment = @course.assignments.find(params[:id])
56-
end
57-
5843
def assignment_params
5944
params.require(:assignment).permit(:title, :content, :deadline)
6045
end
6146

6247
def ensure_teacher_teaches_course
63-
unless @course.teacher_id == current_teacher.id
48+
unless course.teacher_id == teacher.id
6449
redirect_to teacher_courses_path, notice: t(".notice")
6550
end
6651
end

app/controllers/teacher/comments_controller.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
class Teacher::CommentsController < ApplicationController
2+
expose :teacher, :current_teacher
3+
expose :course
4+
expose :comment
25
before_action :ensure_teacher_teaches_course
6+
37
def destroy
4-
@comment = Comment.find(params[:id])
5-
@post= @comment.post
6-
@comment.destroy
8+
comment.destroy
79
respond_to do |format|
8-
format.html { redirect_to teacher_course_post_path(@post.course, @post), status: :see_other, notice: t(".notice") }
10+
format.html { redirect_to teacher_course_post_path(comment.post.course, comment.post), status: :see_other, notice: t(".notice") }
911
format.json { head :no_content }
1012
end
1113
end
1214

1315
private
1416
def ensure_teacher_teaches_course
15-
@course = Course.find(params[:course_id])
16-
unless @course.teacher_id == current_teacher.id
17+
unless course.teacher_id == teacher.id
1718
redirect_to teacher_courses_path, notice: t(".notice")
1819
end
1920
end

0 commit comments

Comments
 (0)