From 5c16cb53c7b0d73b4db2fbb6602a66f36b6bd9a2 Mon Sep 17 00:00:00 2001 From: ferro96 Date: Mon, 4 Nov 2024 15:09:45 -0600 Subject: [PATCH] Added scope for enrollment --- app/helpers/courses_helper.rb | 13 +++++++++++++ app/models/enrollment.rb | 2 ++ app/views/courses/_course.html.haml | 2 ++ 3 files changed, 17 insertions(+) diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 4631b33..61379aa 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -14,4 +14,17 @@ def enrollment_button(course) link_to "Check price", course_path(course), class: "btn btn-md btn-success" end end + + def review_button(course) + user_course = course.enrollments.where(user: current_user) + if current_user + if user_course.any? + if user_course.pending_review.any? + link_to "Add a review", edit_enrollment_path(user_course.first) + else + link_to "Thanks for reviewing! Your Review", enrollment_path(user_course.first) + end + end + end + end end diff --git a/app/models/enrollment.rb b/app/models/enrollment.rb index 43f14c2..41df6c0 100644 --- a/app/models/enrollment.rb +++ b/app/models/enrollment.rb @@ -10,6 +10,8 @@ class Enrollment < ApplicationRecord validate :cant_subscribe_to_own_course + scope :pending_review, -> { where(rating: [ 0, nil, "" ], review: [ 0, nil, "" ]) } + def to_s user.to_s + " " + course.to_s end diff --git a/app/views/courses/_course.html.haml b/app/views/courses/_course.html.haml index 8c57027..f1f7ac8 100644 --- a/app/views/courses/_course.html.haml +++ b/app/views/courses/_course.html.haml @@ -7,6 +7,8 @@ .card-footer .text-center = enrollment_button(course) + .row + = review_button(course) .card-footer .fa.fa-list-check = course.lessons.count