-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permit reviews creation/update inside book details page
- Loading branch information
1 parent
1414407
commit 4039de6
Showing
28 changed files
with
301 additions
and
30 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class Books::ReviewsController < ApplicationController | ||
include FindBook | ||
|
||
before_action :authenticate_user! | ||
before_action :find_review, only: [ :edit, :update ] | ||
before_action :authorize_user, only: [ :edit, :update ] | ||
|
||
def new | ||
@review = @book.reviews.new | ||
end | ||
|
||
def create | ||
@review = @book.reviews.new(review_params) | ||
@review.user = current_user | ||
|
||
if @review.save | ||
respond_to do |format| | ||
format.html { redirect_to book_path(@book) } | ||
format.turbo_stream | ||
end | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @review.update(review_params) | ||
respond_to do |format| | ||
format.html { redirect_to(book_path(@book)) } | ||
format.turbo_stream | ||
end | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def review_params | ||
params.require(:review).permit(:title, :description, :rating) | ||
end | ||
|
||
def find_review | ||
@review = @book.reviews.find(params[:id]) | ||
end | ||
|
||
def authorize_user | ||
unless @review.user_id == current_user.id | ||
redirect_to book_path(@book) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module FindBook | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :find_book | ||
end | ||
|
||
private | ||
|
||
def find_book | ||
@book = Book.find(params[:book_id] || params[:id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Books::ReviewsHelper | ||
def rating_field(f) | ||
content_tag :div, class: "flex flex-row-reverse justify-end items-center" do | ||
(1..5).reverse_each.map do |rating| | ||
f.radio_button(:rating, rating, class: "peer -ms-5 size-5 bg-transparent border-0 text-transparent cursor-pointer appearance-none checked:bg-none focus:bg-none focus:ring-0 focus:ring-offset-0") + | ||
f.label("rating_#{rating}", class: "peer-checked:text-yellow-400 text-gray-300 pointer-events-none") do | ||
inline_svg_tag("star.svg", class: "size-6 shrink-0") | ||
end | ||
end.join.html_safe | ||
end | ||
end | ||
|
||
def user_is_creator?(review) | ||
review.user_id == current_user&.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%# locals: (book:) -%> | ||
|
||
<section class="mt-4 lg:mt-0 lg:ml-6" id="book-description"> | ||
<%= reviews_average(book.reviews) %> | ||
<h1 class="text-2xl font-bold text-gray-800 mb-1"><%= book.title %></h1> | ||
<p class="text-gray-600 text-lg italic"><%= book.subtitle %></p> | ||
<%= link_to "Leave a review", new_book_review_path(book), class: "px-4 py-2 mt-4 inline-block font-semibold text-sm bg-blue-500 text-white rounded-full shadow-sm", data: { turbo_frame: "modal" } if user_signed_in? %> | ||
</section> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<%# locals: (url:, submit_label:) -%> | ||
<%= modal({title: "Review: #{@book.title}"}) do %> | ||
<%= modal_form_for(@review, url: url) do |f| %> | ||
<%= alert_error(@review.errors.full_messages) %> | ||
|
||
<div class="field"> | ||
<%= f.label :rating %> | ||
<%= rating_field(f) %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= f.label :title %> | ||
<%= f.text_field :title, autofocus: true %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= f.label :description %> | ||
<%= f.text_area :description %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= f.submit submit_label %> | ||
</div> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%# locals: (review:) -%> | ||
<%= content_tag :div, class:"flex justify-between my-6 border-b pb-4", id: dom_id(review) do %> | ||
<div> | ||
<%= render_stars(review.rating) %> | ||
<h3 class="text-lg font-semibold text-gray-700"><%= review.title %></h3> | ||
<p class="text-sm text-gray-500">By <%= review.user.nickname %></p> | ||
<p class="text-gray-600 mt-2"><%= review.description %></p> | ||
</div> | ||
<% if user_is_creator?(review) %> | ||
<div> | ||
<%= link_to inline_svg_tag("pencil-square.svg", class: "size-6"), edit_book_review_path(@book, review), data: { turbo_frame: "modal" } %> | ||
</div> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<%= turbo_stream.prepend("reviews-list", partial: "review", locals: { review: @review }) %> | ||
<%= turbo_stream.replace("book-description", partial: "books/book_description", locals: { book: @book }) %> | ||
<%= turbo_stream.replace("empty-reviews-message") do %> | ||
<% # In the case this is the first review we need to replace the "empty-reviews-message" element %> | ||
<div id="reviews-list"> | ||
<%= render partial: "review", locals: { review: @review } %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: "form", locals: { url: book_review_path(@book, @review), submit_label: "Update review" } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render partial: "form", locals: { url: book_reviews_path, submit_label: "Leave review" } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<%= turbo_stream.replace(dom_id(@review), partial: "review", locals: { review: @review }) %> | ||
<%= turbo_stream.replace("book-description", partial: "books/book_description", locals: { book: @book }) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.