Skip to content

Commit

Permalink
Merge pull request #45 from dalvarez2596/lesson-89
Browse files Browse the repository at this point in the history
Added scopes and move logic from controller to models
  • Loading branch information
dalvarez2596 authored Nov 19, 2024
2 parents 28518df + 57259ee commit ad2b2b4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class HomeController < ApplicationController
skip_before_action :authenticate_user!, only: [ :index ]
def index
@courses = Course.all.limit(4)
@latest_courses = Course.all.limit(4).order(created_at: :desc)
@latest_good_reviews = Enrollment.reviewed.order(rating: :desc, created_at: :desc).limit(3)
@top_rated_courses = Course.order(average_rating: :desc, created_at: :desc).limit(3)
@popular_courses = Course.order(enrollments_count: :desc, created_at: :desc).limit(3)
@purchased_courses = Course.joins(:enrollments).where(enrollments: { user: current_user }).order(created_at: :desc).limit(3)
@latest_good_reviews = Enrollment.reviewed.latest_good_reviews
@latest = Course.latest
@top_rated = Course.top_rated
@popular = Course.popular
@purchased = Course.joins(:enrollments).where(enrollments: { user: current_user }).order(created_at: :desc).limit(3)
end

def activity
Expand Down
4 changes: 4 additions & 0 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Course < ApplicationRecord
belongs_to :user, counter_cache: true
# User.find_each{ |user| User.reset_counters(user.id, :courses)}

scope :latest, -> { limit(4).order(created_at: :desc) }
scope :top_rated, -> { limit(4).order(average_rating: :desc, created_at: :desc) }
scope :popular, -> { limit(4).order(enrollments_count: :desc, created_at: :desc) }

has_many :lessons, dependent: :destroy
has_many :enrollments

Expand Down
1 change: 1 addition & 0 deletions app/models/enrollment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Enrollment < ApplicationRecord

scope :pending_review, -> { where(rating: [ 0, nil, "" ], review: [ 0, nil, "" ]) }
scope :reviewed, -> { where.not(rating: [ 0, nil, "" ]) }
scope :latest_good_reviews, -> { limit(4).order(rating: :desc, created_at: :desc) }

extend FriendlyId
friendly_id :to_s, use: :slugged
Expand Down
8 changes: 4 additions & 4 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,31 @@
.fa.fa-user-graduate
Continue Learning
.row.row-cols-lg-4
- @purchased_courses.each do |course|
- @purchased.each do |course|
.col
= render 'courses/course', course: course
%hr
%h2
.fa.fa-chart-line
Popular Courses
.row.row-cols-lg-4
- @popular_courses.each do |course|
- @popular.each do |course|
.col
= render 'courses/course', course: course
%hr
%h2
.fa.fa-star
Top Rated Courses
.row.row-cols-lg-4
- @top_rated_courses.each do |course|
- @top_rated.each do |course|
.col
= render 'courses/course', course: course
%hr
%h2
.fa.fa-clock
Latest Courses
.row.row-cols-lg-4
- @latest_courses.each do |course|
- @latest.each do |course|
.col
= render 'courses/course', course: course
%hr
Expand Down

0 comments on commit ad2b2b4

Please sign in to comment.