50 lines
1.1 KiB
Ruby
50 lines
1.1 KiB
Ruby
class Book < ApplicationRecord
|
|
has_many :users_books
|
|
has_many :users, through: :users_books
|
|
|
|
enum genre: [ :tech, :science, :nonfiction, :fiction, :philosophy ]
|
|
|
|
enum sub_genre: {
|
|
signal_processing: 0,
|
|
data_science: 1,
|
|
mathematics: 2,
|
|
economics: 3,
|
|
history: 4,
|
|
psychology: 5,
|
|
classic: 6,
|
|
computer_science: 7,
|
|
novel: 8,
|
|
science: 9,
|
|
autobiography: 10,
|
|
physics: 11,
|
|
objectivism: 12,
|
|
trivia: 13,
|
|
misc: 14,
|
|
poetry: 15,
|
|
education: 16,
|
|
philosophy: 17,
|
|
anthology: 18,
|
|
politics: 19,
|
|
comic: 20,
|
|
legal: 21
|
|
}, _prefix: :sub
|
|
|
|
def description
|
|
str = ''
|
|
str += genre if genre
|
|
str += ' / ' if sub_genre
|
|
str += sub_genre if sub_genre
|
|
str += ', ' if author
|
|
str += "by #{author}" if author
|
|
str += ', ' if publisher
|
|
str += "published by #{publisher}" if publisher
|
|
str += ' - ' if available_copies > 0
|
|
str += "<b>#{available_copies} copies left</b>" if available_copies > 0
|
|
str.html_safe
|
|
end
|
|
|
|
def available_copies
|
|
copies - users_books.where(status: :borrowed).count
|
|
end
|
|
end
|