From 48b5ba6ca4fb76269472c89af0d51076bd3914ef Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sat, 20 Jan 2018 01:56:59 +0200 Subject: [PATCH 1/4] unlist books of the logged in user to be shown up in the latest books list, modify most_recent_with_content function --- app/controllers/books_controller.rb | 7 ++++--- app/controllers/dashboard_controller.rb | 6 +++--- app/models/book.rb | 6 ++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index c5e60479..a656b7af 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -38,13 +38,13 @@ def destroy if book.present? authorize book book.destroy - + # Also destroy FavoriteBook records of this book FavoriteBook.where(book_id: params[:id]) .map do |fav_book| fav_book.destroy end - + render json: {}, status: :ok else skip_authorization @@ -87,8 +87,9 @@ def show_more skip_authorization page = params[:page] + current_user_id = (current_user.nil?) ? nil : current_user.id books = Book - .most_recent_with_content(page) + .most_recent_with_content(current_user_id, page) .map do |book| BookSerializer.new(book) end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index a2091bcc..c95d8bb9 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -6,7 +6,7 @@ def index @user = current_user @hashedEmail = Digest::MD5.hexdigest(@user.email) @books = Book - .most_recent_with_content + .most_recent_with_content(@user.id) .map do |book| BookSerializer.new(book) end @@ -16,7 +16,7 @@ def index .map do |book| BookSerializer.new(book) end - + @favorites = FavoriteBook .where(user_id: @user) .order("created_at DESC") @@ -28,7 +28,7 @@ def index fav_book.destroy end end - + end end diff --git a/app/models/book.rb b/app/models/book.rb index 39c42c74..84e6226d 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -10,8 +10,10 @@ class Book < ActiveRecord::Base after_create :send_admin_notification self.per_page = 10 - def self.most_recent_with_content(page = 1) - joins(:phrase_pairs) + def self.most_recent_with_content(current_user_id = nil, page = 1) + where + .not(user_id: current_user_id) + .joins(:phrase_pairs) .uniq .includes(:user) .order("created_at DESC") From a03b5cfad7adcc0aa0775ed399551ee61e439669 Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sat, 20 Oct 2018 10:58:33 +0200 Subject: [PATCH 2/4] Update books_controller.rb remove unnecessary changes --- app/controllers/books_controller.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index a656b7af..de95e525 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -38,13 +38,11 @@ def destroy if book.present? authorize book book.destroy - # Also destroy FavoriteBook records of this book FavoriteBook.where(book_id: params[:id]) .map do |fav_book| fav_book.destroy end - render json: {}, status: :ok else skip_authorization From 21f86c5884e3a6e2f127180e57486f56b9a0781e Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sat, 20 Oct 2018 11:00:09 +0200 Subject: [PATCH 3/4] Update books_controller.rb adding lines for readability --- app/controllers/books_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index de95e525..a656b7af 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -38,11 +38,13 @@ def destroy if book.present? authorize book book.destroy + # Also destroy FavoriteBook records of this book FavoriteBook.where(book_id: params[:id]) .map do |fav_book| fav_book.destroy end + render json: {}, status: :ok else skip_authorization From 5a0c85a0836c602096b2ab5444411954c5276e3b Mon Sep 17 00:00:00 2001 From: Amr Adel Date: Sat, 20 Oct 2018 11:00:48 +0200 Subject: [PATCH 4/4] Update books_controller.rb remove unneeded changes. --- app/controllers/books_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index a656b7af..99a8452f 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -38,13 +38,13 @@ def destroy if book.present? authorize book book.destroy - + # Also destroy FavoriteBook records of this book FavoriteBook.where(book_id: params[:id]) .map do |fav_book| fav_book.destroy end - + render json: {}, status: :ok else skip_authorization