From 0c53573cdca1d8a2935e025cb044e9a8825d3f81 Mon Sep 17 00:00:00 2001 From: Dilraj Date: Fri, 16 Feb 2024 02:57:28 -0500 Subject: [PATCH] Added `released` scope to `Book` model --- .gitignore | 2 ++ app/controllers/books_controller.rb | 4 ++-- app/models/book.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 42f569d..a449742 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ lib/assets/*.csv lib/imports/*/*.csv node_modules + +*.csv diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index 6fab66d..f2e8596 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -3,10 +3,10 @@ class BooksController < ApplicationController # NOTE: Remember to see .jbuilder def index - @books = Book.all + @books = Book.released end def show - @book = Book.find(params[:id]) + @book = Book.released.find(params[:id]) end end diff --git a/app/models/book.rb b/app/models/book.rb index 77e137e..2218e38 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -4,6 +4,15 @@ class Book < ApplicationRecord has_many :chapters, :dependent => :destroy + # @brief Returns the released `Book`s + # @example `@books = Book.released` + # @return [ActiveRecord::Relation] Set of released Books. + if Rails.env.production? + scope :released, -> { all } + else + scope :released, -> { where.not(:en_short_summary => nil) } + end + def last_chapter return self.chapters.last end