Skip to content

Commit

Permalink
added reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
guub committed Nov 14, 2016
1 parent 1d5c62c commit 63b28cc
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def index

def show
@product = Product.find params[:id]
@review = Review.new
end

end
33 changes: 33 additions & 0 deletions app/controllers/reviews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class ReviewsController < ApplicationController
before_action :require_login

def create
@product = Product.find(params[:product_id].to_i)
@review = @product.reviews.new(review_params)
@review.user_id = current_user.id
if @review.save
redirect_to product_path(@product), notice: "Review submitted"
else
render @product
end
end

def destroy
@review = Review.find params[:id]
@review.destroy
redirect_to product_path(params[:product_id])
end

private

def review_params
params.require(:review).permit(:description, :rating)
end

def require_login
unless current_user
flash[:error] = "You must be logged in to access this section"
end
end

end
3 changes: 2 additions & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class Product < ActiveRecord::Base

monetize :price_cents, numericality: true
mount_uploader :image, ProductImageUploader
has_many :line_items

has_many :line_items
has_many :reviews
belongs_to :category

validates :name, presence: true
Expand Down
10 changes: 10 additions & 0 deletions app/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Review < ActiveRecord::Base

belongs_to :product
belongs_to :user

validates :product_id, presence: true
validates :user_id, presence: true
validates :rating, presence: true

end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class User < ActiveRecord::Base

has_many :reviews

validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
Expand Down
23 changes: 23 additions & 0 deletions app/views/products/_reviews.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if @product.reviews.length === 0%>
<p>There are no reviews!</p>
<% end %>

<% @product.reviews.reverse_each do |review| %>

<h4>
<%= review.user.first_name %>
</h4>
<%= review.description %>
</dd>
<dt>Rating
</dt>
<dd>
<%= review.rating %>
</dd>
<% if current_user && review.user_id.to_i == current_user.id%>
<td>
<%= link_to fa_icon('trash'), [@product, Review.find(review.id)], class: 'btn btn-sm btn-danger', method: :delete, data: { confirm: "Are you sure?" } %>
</td>
<% end %>
<br>
<% end %>
20 changes: 20 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@
</article>

</section>

<section class="reviews-show">

<h2 class="reviews-header">
Reviews
</h2>

<% if current_user %>
<%= form_for [@product, @review] do |f|%>
<%= f.text_area :description %>
<%= f.select(:rating, [1, 2, 3, 4, 5]) %>
<%= f.submit 'Add review'%>
<% end %>
<% end %>

<div>
<%= render 'reviews', reviews: @review %>
</div>

</section>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
delete :remove_item
end

resources :products, only: [:index, :show] do
resources :reviews, only: [:create, :destroy]
end

get '/signup' => 'users#new'
post '/users' => 'users#create'

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/201611140005_create_reviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.references :product, index: true, foreign_key: true
t.references :user, index: true, foreign_key: true
t.integer :rating
t.text :description

t.timestamps null: false
end
end
end
14 changes: 14 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@

add_index "products", ["category_id"], name: "index_products_on_category_id", using: :btree

create_table "reviews", force: :cascade do |t|
t.integer "product_id"
t.integer "user_id"
t.integer "rating"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "reviews", ["product_id"], name: "index_reviews_on_product_id", using: :btree
add_index "reviews", ["user_id"], name: "index_reviews_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
Expand All @@ -68,4 +80,6 @@
add_foreign_key "line_items", "orders"
add_foreign_key "line_items", "products"
add_foreign_key "products", "categories"
add_foreign_key "reviews", "products"
add_foreign_key "reviews", "users"
end
29 changes: 29 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,34 @@ def open_asset(file_name)
price: 2_483.75
})

User.create!({
id: 1,
first_name: "Guy",
last_name: "Booth",
email: "guy@guy",
password_digest: "No Bueno"
})

User.create!({
id: 2,
first_name: "Benno",
last_name: "Booth",
email: "ben@ben",
password_digest: "No Bueno mucho"
})

Review.create!({
user_id: 1,
product_id: 12,
description: "It's the best shelf ever!",
rating: 5
})

Review.create!({
user_id: 1,
product_id: 12,
description: "Was missing all the pieces! How am I supposed to build it??",
rating: 1
})

puts "DONE!"

0 comments on commit 63b28cc

Please sign in to comment.