Skip to content

Commit

Permalink
データの取得方法を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-24 committed Nov 17, 2024
1 parent e611be3 commit bb9c1c9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 88 deletions.
12 changes: 11 additions & 1 deletion app/controllers/courses/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
class Courses::BooksController < ApplicationController
def index
@course = Course.find(params[:course_id])
@books = Book.filtered_books(params[:status], @course)
@books = Book.with_attached_cover
.eager_load(:practices_books, practices: { categories: :courses_categories })
.where(courses_categories: { course_id: @course.id })
.where(must_read_status)
.order(updated_at: :desc, id: :desc)
end

private

def must_read_status
params[:status] == 'mustread' ? { practices_books: { must_read: true } } : {}
end
end
18 changes: 0 additions & 18 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,4 @@ def cover_url
rescue ActiveStorage::FileNotFoundError, ActiveStorage::InvariableError
image_url default_image_path
end

def coursebook?(course_practices)
practices.any? do |practice|
course_practices.any? { |course_practice| practice.id == course_practice.id }
end
end

def self.filtered_books(status, course)
books = Book
.with_attached_cover
.includes(:practices)
.order(updated_at: :desc, id: :desc)

course_practices = course.extract_practices
mustread_or_all =
status == 'mustread' ? books.select { |book| book.practices_books.any?(&:must_read) } : books
mustread_or_all.select { |book| book.coursebook?(course_practices) }
end
end
10 changes: 0 additions & 10 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,4 @@ class Course < ApplicationRecord
def self.default_course
find_by(title: DEFAULT_COURSE)
end

def extract_practices
categories = Category
.joins(:courses_categories)
.where(courses_categories: { course_id: id })
.includes(:practices)
.order('courses_categories.position')

categories.map(&:practices).flatten
end
end
47 changes: 0 additions & 47 deletions test/models/book_test.rb

This file was deleted.

12 changes: 0 additions & 12 deletions test/models/course_test.rb

This file was deleted.

0 comments on commit bb9c1c9

Please sign in to comment.