From 63b28cce4f04778a68d26911b71d467fab223613 Mon Sep 17 00:00:00 2001 From: guub Date: Mon, 14 Nov 2016 21:48:07 +0000 Subject: [PATCH] added reviews --- app/controllers/products_controller.rb | 1 + app/controllers/reviews_controller.rb | 33 +++++++++++++++++++++++ app/models/product.rb | 3 ++- app/models/review.rb | 10 +++++++ app/models/user.rb | 2 ++ app/views/products/_reviews.html.erb | 23 ++++++++++++++++ app/views/products/show.html.erb | 20 ++++++++++++++ config/routes.rb | 4 +++ db/migrate/201611140005_create_reviews.rb | 12 +++++++++ db/schema.rb | 14 ++++++++++ db/seeds.rb | 29 ++++++++++++++++++++ 11 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 app/controllers/reviews_controller.rb create mode 100644 app/models/review.rb create mode 100644 app/views/products/_reviews.html.erb create mode 100644 db/migrate/201611140005_create_reviews.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 1e82ef27ac..b5a8c1cb56 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -6,6 +6,7 @@ def index def show @product = Product.find params[:id] + @review = Review.new end end diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb new file mode 100644 index 0000000000..32bb2ad417 --- /dev/null +++ b/app/controllers/reviews_controller.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb index 6bef65dce2..b2f2759619 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/app/models/review.rb b/app/models/review.rb new file mode 100644 index 0000000000..09015c700f --- /dev/null +++ b/app/models/review.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 27102562a5..93c658e2d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/products/_reviews.html.erb b/app/views/products/_reviews.html.erb new file mode 100644 index 0000000000..ec099f3ec8 --- /dev/null +++ b/app/views/products/_reviews.html.erb @@ -0,0 +1,23 @@ +<% if @product.reviews.length === 0%> +

There are no reviews!

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

+ <%= review.user.first_name %> +

+ <%= review.description %> + +
Rating +
+
+ <%= review.rating %> +
+<% if current_user && review.user_id.to_i == current_user.id%> + + <%= link_to fa_icon('trash'), [@product, Review.find(review.id)], class: 'btn btn-sm btn-danger', method: :delete, data: { confirm: "Are you sure?" } %> + +<% end %> +
+<% end %> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb index 64d02317dd..7391e60510 100644 --- a/app/views/products/show.html.erb +++ b/app/views/products/show.html.erb @@ -29,3 +29,23 @@ + +
+ +

+ Reviews +

+ + <% 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 %> + +
+ <%= render 'reviews', reviews: @review %> +
+ +
diff --git a/config/routes.rb b/config/routes.rb index 6bf51882af..ea865eac7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/db/migrate/201611140005_create_reviews.rb b/db/migrate/201611140005_create_reviews.rb new file mode 100644 index 0000000000..aefc24480e --- /dev/null +++ b/db/migrate/201611140005_create_reviews.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index da82ffaeeb..6d0bdf1958 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 1bad7490cd..dd43d36f19 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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!"