Skip to content

Commit

Permalink
Split books show view into partials
Browse files Browse the repository at this point in the history
This page is concerned with both showing the book’s details and listing
reviews.

By extracting out partials we can more easily see the high-level
structure, and have separate view specs to cover each aspect of the
page.
  • Loading branch information
andyw8 committed Nov 14, 2015
1 parent 7b58369 commit d4ad61a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 54 deletions.
17 changes: 17 additions & 0 deletions app/views/books/_details.html.erb
@@ -0,0 +1,17 @@
<div class="book">
<div class="book-image">
<%= image_tag(book.sample_cover) %>
</div>
<div class="book-content">
<h1><%= book.title %></h1>
<% if book.author.present? %>
<h3>by <%= link_to book.author.name, book.author %></h3>
<% end %>
<% if book.has_summary? %>
<% # doesn't yet handle paragraphs, line breaks yet %>
<p class="summary"><%= book.summary %></p>
<% end %>
<p class="released-on"><%= l(book.released_on, format: :long) %></p>
</div>
</div>
29 changes: 29 additions & 0 deletions app/views/books/_reviews.html.erb
@@ -0,0 +1,29 @@
<% # Markup for users Review feature %>
<div class="comment">
<h2>Reviews</h2>
</div>
<% [*1..3].sample.times do %>
<div class="comment">
<div class="comment-image">
<img src="https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/placeholder_logo_1.png" alt="Logo image">
</div>
<div class="comment-content">
<h1>Samuel L. Jackson</h1>
<p>Yeah, I like animals better than people sometimes... Especially dogs. Dogs are the best. Every time you come home, they act like they haven't seen you in a year. And the good thing about dogs... is they got different dogs for different people.</p>
<p class="comment-detail"><%= Time.current %></p>
</div>
</div>
<div class="comment">
<div class="comment-image">
<img src="https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/placeholder_logo_2.png" alt="Logo image">
</div>
<div class="comment-content">
<h1>David H. Hanson</h1>
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
<p class="comment-detail"><%= Time.current %></p>
</div>
</div>
<% end %>
<% # Markup for users Review feature %>
49 changes: 2 additions & 47 deletions app/views/books/show.html.erb
@@ -1,49 +1,4 @@
<div class="box">
<div class="book">
<div class="book-image">
<%= image_tag(book.sample_cover) %>
</div>
<div class="book-content">
<h1><%= book.title %></h1>
<% if book.author.present? %>
<h3>by <%= link_to book.author.name, book.author %></h3>
<% end %>
<% if book.has_summary? %>
<% # doesn't yet handle paragraphs, line breaks yet %>
<p class="summary"><%= book.summary %></p>
<% end %>
<p class="released-on"><%= l(book.released_on, format: :long) %></p>
</div>
</div>
<% # Markup for users Review feature %>
<div class="comment">
<h2>Reviews</h2>
</div>
<% [*1..3].sample.times do %>
<div class="comment">
<div class="comment-image">
<img src="https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/placeholder_logo_1.png" alt="Logo image">
</div>
<div class="comment-content">
<h1>Samuel L. Jackson</h1>
<p>Yeah, I like animals better than people sometimes... Especially dogs. Dogs are the best. Every time you come home, they act like they haven't seen you in a year. And the good thing about dogs... is they got different dogs for different people.</p>
<p class="comment-detail"><%= Time.current %></p>
</div>
</div>
<div class="comment">
<div class="comment-image">
<img src="https://raw.githubusercontent.com/thoughtbot/refills/master/source/images/placeholder_logo_2.png" alt="Logo image">
</div>
<div class="comment-content">
<h1>David H. Hanson</h1>
<p>Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows.</p>
<p class="comment-detail"><%= Time.current %></p>
</div>
</div>
<% end %>
<% # Markup for users Review feature %>
<%= render "details", book: book %>
<%= render "reviews", book: book %>
</div>
@@ -1,11 +1,11 @@
require "rails_helper"

RSpec.describe "books/show" do
RSpec.describe "books/_details" do
context "when the book has a summary" do
it "displays the summary" do
book = build_stubbed(:book, summary: "The first book in the series")

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to have_css("p.summary",
text: "The first book in the series")
Expand All @@ -16,7 +16,7 @@
it "doesn't display a summary" do
book = build_stubbed(:book)

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to_not have_css("p.summary")
end
Expand All @@ -26,7 +26,7 @@
it "displays the cover sample" do
book = build_stubbed(:book, cover: "book_cover.jpg")

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to have_cover("book_cover.jpg")
end
Expand All @@ -36,7 +36,7 @@
it "doesn't display the cover sample" do
book = build_stubbed(:book)

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to have_cover("default_cover.jpg")
end
Expand All @@ -47,7 +47,7 @@
author = build_stubbed(:author, name: "John")
book = build_stubbed(:book, author: author)

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to have_css("h3", "John")
end
Expand All @@ -57,7 +57,7 @@
it "doesn't display author name" do
book = build_stubbed(:book)

render template: "books/show", locals: { book: book }
render template: "books/_details", locals: { book: book }

expect(rendered).to_not have_css("h3", "John")
end
Expand Down

0 comments on commit d4ad61a

Please sign in to comment.