Skip to content

Commit

Permalink
Merge pull request #56 from Vjp888/sorting_index_page_#23
Browse files Browse the repository at this point in the history
Sorting index page #23
  • Loading branch information
milevy1 authored Mar 25, 2019
2 parents d39c7ec + 0a76c10 commit 191856a
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class BooksController < ApplicationController
def index
@books = Book.all
@books = Book.sort_books(params[:sorting_params])
@reviews = Review.all
end

Expand Down
58 changes: 58 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,64 @@ def grab_reviews(direction, limit)
end
end

def self.sort_by_avg_rating(direction)
case direction
when "top"
self.left_outer_joins(:reviews)
.select('books.*, avg(reviews.rating) as average_rating')
.group(:id)
.order('average_rating DESC nulls last')
when "bottom"
self.left_outer_joins(:reviews)
.select('books.*, avg(reviews.rating) as average_rating')
.group(:id)
.order('average_rating ASC nulls last')
end
end

def self.sort_by_review_count(direction)
case direction
when "top"
self.left_outer_joins(:reviews)
.select('books.*, count(reviews) as review_count')
.group(:id)
.order('review_count DESC')
when "bottom"
self.left_outer_joins(:reviews)
.select('books.*, count(reviews) as review_count')
.group(:id)
.order('review_count ASC')
end
end

def self.sort_by_page_count(direction)
case direction
when "top"
self.order(pages: :desc)
when "bottom"
self.order(pages: :asc)
end
end

def self.sort_books(sort_param = "nothing")
case sort_param
when "top_reviews"
self.sort_by_avg_rating("top")
when "bottom_reviews"
self.sort_by_avg_rating("bottom")
when "most_pages"
self.sort_by_page_count('top')
when "least_pages"
self.sort_by_page_count("bottom")
when "most_reviews"
self.sort_by_review_count("top")
when "least_reviews"
self.sort_by_review_count("bottom")
else
self.all
end
end

def self.top_three_rated
# add argument of limit, order to dry this up
# rename method
Expand Down
13 changes: 12 additions & 1 deletion app/views/books/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<section id="sort-nav">
<%= link_to "Top Reviews", books_path(sorting_params: :top_reviews) %>
<%= link_to "Bottom Reviews", books_path(sorting_params: :bottom_reviews) %>
<%= link_to "Most Pages", books_path(sorting_params: :most_pages) %>
<%= link_to "Least Pages", books_path(sorting_params: :least_pages) %>
<%= link_to "Most Reviews", books_path(sorting_params: :most_reviews) %>
<%= link_to "Least Reviews", books_path(sorting_params: :least_reviews) %>
</section>

<section id="book-statistics">
<section class="book-statistics-card" id="highest-rated-books">
<h3>Highest rated books</h3>
Expand Down Expand Up @@ -30,6 +39,7 @@
<section class="secondary-nav">
<%= link_to "Add a Book", new_book_path %>
</section>

<% @books.each do |book| %>
<section id="book-<%= book.id %>">
<a href=<%= book_path(book) %>><img src="<%= book.thumbnail %>" alt="cover of <%= book.title %>"></a>
Expand All @@ -45,4 +55,5 @@
<p>Average rating: <%= book.average_rating %> out of 5</p>
<p>Total reviews: <%= book.review_count %></p>
</section>
<% end %>
<% end %>
</section>
49 changes: 49 additions & 0 deletions spec/features/book_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,55 @@
end
end

it 'lets a user sort the book index page' do
author = Author.create(name: "Rickey Bobby")
book_2 = Book.create(title: "book title 2", pages: 200, year_published: 1867, thumbnail: "steve.jp", authors: [author])
book_1 = Book.create(title: "book title 1", pages: 100, year_published: 1980, thumbnail: "steve.jpg", authors: [author])
book_1.reviews.create(title: "fantastic", description: "asdafd", rating: 5, username: 'bob')
book_1.reviews.create(title: "horrible", description: "cdsubnvfkdf", rating: 1, username: 'bob')
book_1.reviews.create(title: "meh", description: "meh", rating: 3, username: 'bob')
book_2.reviews.create(title: "stupendous", description: "really", rating: 5, username: 'bob')
book_2.reviews.create(title: "alright", description: "kinda", rating: 4, username: 'bob')

visit books_path

within '#sort-nav' do
click_link 'Top Reviews'
end
expect(page.all('.book-card')[0]).to have_content("book title 2")
expect(page.all('.book-card')[1]).to have_content("book title 1")

within '#sort-nav' do
click_link "Bottom Reviews"
end
expect(page.all('.book-card')[0]).to have_content("book title 1")
expect(page.all('.book-card')[1]).to have_content("book title 2")

within '#sort-nav' do
click_link "Most Pages"
end
expect(page.all('.book-card')[0]).to have_content("book title 2")
expect(page.all('.book-card')[1]).to have_content("book title 1")

within '#sort-nav' do
click_link "Least Pages"
end
expect(page.all('.book-card')[0]).to have_content("book title 1")
expect(page.all('.book-card')[1]).to have_content("book title 2")

within '#sort-nav' do
click_link "Most Reviews"
end
expect(page.all('.book-card')[0]).to have_content("book title 1")
expect(page.all('.book-card')[1]).to have_content("book title 2")

within '#sort-nav' do
click_link "Least Reviews"
end
expect(page.all('.book-card')[0]).to have_content("book title 2")
expect(page.all('.book-card')[1]).to have_content("book title 1")
end

describe 'book statistics section' do
before :each do
@book_1 = Book.create(thumbnail: 'steve.jpg', title: 'Book 1 title', pages: 40, year_published: 1987)
Expand Down
56 changes: 56 additions & 0 deletions spec/models/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,62 @@
it { should have_many :reviews }
end

describe 'Class Methods' do
describe 'Sorting books on index' do
before :each do
@author = Author.create(name: "Rickey Bobby")
@book_2 = Book.create!(title: "book title 2", pages: 200, year_published: 1867, thumbnail: "steve.jpg", authors: [@author])
@book_1 = Book.create!(title: "book title 1", pages: 100, year_published: 1980, thumbnail: "steve.jpg", authors: [@author])
@book_3 = Book.create!(title: "book title 3", pages: 150, year_published: 1980, thumbnail: "steve.jpg", authors: [@author])
@book_1.reviews.create!(title: "fantastic", description: "asdafd", rating: 5, username: 'bob')
@book_1.reviews.create!(title: "horrible", description: "cdsubnvfkdf", rating: 1, username: 'bob')
@book_1.reviews.create!(title: "meh", description: "meh", rating: 1, username: 'bob')
@book_2.reviews.create!(title: "stupendous", description: "really", rating: 5, username: 'bob')
@book_2.reviews.create!(title: "alright", description: "kinda", rating: 5, username: 'bob')
@sort_1 = [@book_1, @book_2, @book_3]
@sort_2 = [@book_2, @book_1, @book_3]
end
describe '.sort_books' do
it 'sorts books by given params' do
most_page = [@book_2, @book_3, @book_1]
least_page = [@book_1, @book_3, @book_2]
least_reviews = [@book_3, @book_2, @book_1]

expect(Book.sort_books("top_reviews")).to eq(@sort_2)
expect(Book.sort_books("bottom_reviews")).to eq(@sort_1)
expect(Book.sort_books("most_pages")).to eq(most_page)
expect(Book.sort_books("least_pages")).to eq(least_page)
expect(Book.sort_books("most_reviews")).to eq(@sort_1)
expect(Book.sort_books("least_reviews")).to eq(least_reviews)
expect(Book.sort_books).to eq(Book.all)
end
end
describe '.sort_by_review_count' do
it 'it sorts books by reviews either asc or desc' do
bottom_sort = [@book_3, @book_2, @book_1]
expect(Book.sort_by_review_count("top")).to eq(@sort_1)
expect(Book.sort_by_review_count("bottom")).to eq(bottom_sort)
end
end

describe '.sort_by_avg_rating' do
it 'it sorts books by average rating either asc or desc' do
expect(Book.sort_by_avg_rating("top")).to eq(@sort_2)
expect(Book.sort_by_avg_rating("bottom")).to eq(@sort_1)
end
end

describe '.sort_by_page_count' do
it 'it sorts books by page count either asc or desc' do
sort_1 = [@book_1, @book_3, @book_2]
sort_2 = [@book_2, @book_3, @book_1]
expect(Book.sort_by_page_count("top")).to eq(sort_2)
expect(Book.sort_by_page_count("bottom")).to eq(sort_1)
end
end
end
end

describe 'Instance methods' do
describe 'review ratings and counts' do
before(:each) do
Expand Down

0 comments on commit 191856a

Please sign in to comment.