Second Commit

This commit is contained in:
2021-04-15 13:06:31 +05:30
parent 7f1d1f54e7
commit 129cd9cbcc
169 changed files with 11137 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end

49
app/models/book.rb Normal file
View 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

View File

2
app/models/library.rb Normal file
View File

@@ -0,0 +1,2 @@
class Library < ApplicationRecord
end

9
app/models/user.rb Normal file
View 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
View 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