Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class Announcement < ApplicationRecord
validates_presence_of :body, :club_id

belongs_to :club

end
16 changes: 8 additions & 8 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ def self.check_if_duplicate(book_params)

def self.only_with_ratings
distinct.select('books.item, r.book_id, COUNT(r.book_id) AS review_count, AVG(r.value)')
.joins('INNER JOIN ratings AS r ON books.id = r.book_id')
.group('r.book_id, books.item')
.order('avg DESC')
.limit('20')
.joins('INNER JOIN ratings AS r ON books.id = r.book_id')
.group('r.book_id, books.item')
.order('avg DESC')
.limit('20')
end

def self.change_count(action, bookshelf)
case action
when 'inc'
bookshelf.update(book_count: bookshelf.book_count += 1)
when 'dec'
bookshelf.update(book_count: bookshelf.book_count -= 1)
when 'inc'
bookshelf.update(book_count: bookshelf.book_count += 1)
when 'dec'
bookshelf.update(book_count: bookshelf.book_count -= 1)
end
end
end
1 change: 0 additions & 1 deletion app/models/discussion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ class Discussion < ApplicationRecord
belongs_to :reading
belongs_to :user
has_many :comments, dependent: :destroy

end
8 changes: 3 additions & 5 deletions app/models/reading.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Reading < ApplicationRecord
validates_presence_of :book_id, :club_id
validates_uniqueness_of :book_id, { scope: :club_id }
validates_uniqueness_of :book_id, scope: :club_id

belongs_to :club
belongs_to :book
has_many :discussions, dependent: :destroy


def self.handle_archive(id)
to_archive = Reading.find(id)
to_archive.is_current = false
Expand All @@ -16,8 +15,7 @@ def self.handle_archive(id)
def self.find_current(club)
select("books.item, books.id, readings.id AS reading_id,
readings.club_id, readings.start_date, readings.finish_date")
.joins("INNER JOIN books ON readings.book_id = books.id")
.where("readings.club_id = #{club.id} AND readings.is_current = true")
.joins("INNER JOIN books ON readings.book_id = books.id")
.where("readings.club_id = #{club.id} AND readings.is_current = true")
end

end
6 changes: 3 additions & 3 deletions app/models/shelving.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Shelving < ApplicationRecord
validates_presence_of :bookshelf_id, :book_id
validates_uniqueness_of :book_id, { scope: :bookshelf_id }
validates_uniqueness_of :book_id, scope: :bookshelf_id

belongs_to :book
belongs_to :bookshelf

def self.with_book(shelf_id)
select("books.item, books.id, shelvings.id AS shelving_id, shelvings.bookshelf_id")
.joins("INNER JOIN books ON shelvings.book_id = books.id")
.where("shelvings.bookshelf_id = #{shelf_id}")
.joins("INNER JOIN books ON shelvings.book_id = books.id")
.where("shelvings.bookshelf_id = #{shelf_id}")
end
end
8 changes: 3 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class User < ApplicationRecord

def self.valid_login?(email, password)
user = find_by(email: email)
if user && user.authenticate(password)
user
end
user if user&.authenticate(password)
end

def allow_token_to_be_used_only_once
Expand All @@ -45,8 +43,8 @@ def self.with_unexpired_token(token, period)
private

def build_default_bookshelves
Bookshelf.create(name: 'Read', user_id: self.id)
Bookshelf.create(name: 'Will Read', user_id: self.id)
Bookshelf.create(name: 'Read', user_id: id)
Bookshelf.create(name: 'Will Read', user_id: id)
end

def invalidate_token
Expand Down