Skip to content

Commit

Permalink
Added released scope to Book model
Browse files Browse the repository at this point in the history
  • Loading branch information
dsomel21 committed Feb 16, 2024
1 parent 4adfa19 commit 0c53573
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ lib/assets/*.csv
lib/imports/*/*.csv

node_modules

*.csv
4 changes: 2 additions & 2 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c53573

Please sign in to comment.