Second Commit
This commit is contained in:
3
app/models/application_record.rb
Normal file
3
app/models/application_record.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
end
|
||||
49
app/models/book.rb
Normal file
49
app/models/book.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
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
|
||||
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
2
app/models/library.rb
Normal file
2
app/models/library.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Library < ApplicationRecord
|
||||
end
|
||||
9
app/models/user.rb
Normal file
9
app/models/user.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class User < ApplicationRecord
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :validatable
|
||||
|
||||
has_many :users_books
|
||||
has_many :books, through: :users_books
|
||||
end
|
||||
21
app/models/users_book.rb
Normal file
21
app/models/users_book.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class UsersBook < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :book
|
||||
validates_uniqueness_of :book_id, scope: :user_id
|
||||
|
||||
enum status: {
|
||||
borrowed: 0,
|
||||
returned: 1,
|
||||
overdue: 2
|
||||
}, _default: :borrowed
|
||||
|
||||
before_save :assign_identifier
|
||||
|
||||
def assign_identifier
|
||||
self.identifier = "#{book.title.first(5).upcase + SecureRandom.urlsafe_base64.upcase}"
|
||||
end
|
||||
|
||||
def due_date
|
||||
created_at + 7.days
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user