Skip to content

Commit bcb1c15

Browse files
authored
Merge pull request chi-otters-2017#35 from chi-otters-2017/ratings_on_recipe_page
Ratings on recipe page
2 parents 7f4e6de + 834b87b commit bcb1c15

File tree

18 files changed

+176
-27
lines changed

18 files changed

+176
-27
lines changed

app/assets/javascripts/rating.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Place all the behaviors and hooks related to the matching controller here.
2+
// All this logic will automatically be available in application.js.

app/assets/stylesheets/application.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
.container {
2+
margin: 5%, auto;
3+
padding-bottom: 5%;
4+
}
5+
6+
.nav {
27
margin: 0%, auto;
38
font-family: 'Bree Serif', serif;
49
}
@@ -7,9 +12,11 @@
712
background-color: #A2D5F2;
813
border-bottom-radius: 10px;
914
}
15+
1016
.nav-link{
1117
margin-left: 5em;
1218
}
19+
1320
.nav-link:hover{
1421
font-size: larger;
1522
background-color: white;
@@ -40,6 +47,12 @@
4047
#add-recipe {
4148
margin-top: 20px;
4249
}
50+
51+
52+
#recipe-rating {
53+
margin: 20px 0px;
54+
}
55+
4356
.title{
4457
font-family: 'Lobster', cursive;
4558
font-size: 5em;

app/assets/stylesheets/rating.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the rating controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class RatingsController < ApplicationController
2+
include SessionsHelper
3+
4+
def create
5+
get_recipe
6+
7+
@rating = @recipe.ratings.new(rating_params)
8+
@rating.update_attributes(user_id: current_user.id)
9+
if @rating.save
10+
flash[:notice] = "Rating has been recorded."
11+
12+
redirect_to recipe_path(@recipe)
13+
else
14+
render file: 'public/404.html'
15+
end
16+
end
17+
18+
19+
private
20+
21+
def rating_params
22+
params.require(:rating).permit(:user_id, :recipe_id, :stars)
23+
end
24+
25+
def get_recipe
26+
@recipe ||= Recipe.find_by(id: params[:recipe_id])
27+
end
28+
end

app/controllers/recipes_controller.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def create
2424
def show
2525
if get_recipe
2626
if logged_in
27-
get_recipe
27+
@rating = Rating.new
2828
render :show
2929
else
30-
render :show
30+
render 'general_use_partials/_login_required'
3131
end
3232
else
3333
render file: 'public/404.html'
@@ -77,11 +77,6 @@ def recipe_params
7777
params.require(:recipe).permit(:title, :category_id, :user_id, :directions, :time, :difficulty)
7878
end
7979

80-
81-
def get_recipe
82-
@recipe ||= Recipe.find_by(id: params[:id])
83-
end
84-
8580
def get_category
8681
@category ||= Category.find_by(id: params[:category_id])
8782
end

app/helpers/rating_helper.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module RatingHelper
2+
def recipe_rating(recipe)
3+
total = 0
4+
recipe.ratings.each do |rating|
5+
total += rating.stars
6+
end
7+
average = (total.to_f / recipe.ratings.count.to_f).round(2)
8+
end
9+
10+
def already_rated?(recipe)
11+
!!recipe.ratings.find_by(user_id: current_user.id)
12+
end
13+
14+
15+
end

app/helpers/recipes_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ def recipe_owner?(recipe)
1616
session[:user_id] == recipe.user_id
1717
end
1818

19+
20+
def get_recipe
21+
@recipe ||= Recipe.find_by(id: params[:id])
22+
end
23+
24+
def already_rated?(recipe)
25+
!!recipe.ratings.find_by(user_id: current_user.id)
26+
end
27+
1928
end

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<body>
1515

16-
<div id="nav-bar" class='container'>
16+
<div id="nav-bar" class='nav'>
1717
<span class='nav-link'>
1818
<% if !!logged_in %>
1919
<% if admin? %>

app/views/rating/edit.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Rating#edit</h1>
2+
<p>Find me in app/views/rating/edit.html.erb</p>

app/views/recipes/edit.html.erb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
<h1>Edit Recipe</h1>
1+
<article class='container'><h1>Edit Recipe</h1>
22

3-
<strong><p class='alert'><%= flash.alert %></p></strong>
4-
<% if @recipe.errors.any? %>
5-
<% @recipe.errors.full_messages.each do |error| %>
6-
<li class='error'><%= error %></li>
3+
<strong><p class='alert'><%= flash.alert %></p></strong>
4+
<% if @recipe.errors.any? %>
5+
<% @recipe.errors.full_messages.each do |error| %>
6+
<li class='error'><%= error %></li>
7+
<% end %>
78
<% end %>
8-
<% end %>
99

10-
<%= form_for @recipe, url: category_recipe_path do |f| %>
10+
<%= form_for @recipe, url: category_recipe_path do |f| %>
1111

12-
<p><%= f.label :title %></p>
13-
<p><%= f.text_field :title %></p>
12+
<p><%= f.label :title %></p>
13+
<p><%= f.text_field :title %></p>
1414

15-
<p><%= f.label :directions %></p>
16-
<p><%= f.text_area :directions %></p>
15+
<p><%= f.label :directions %></p>
16+
<p><%= f.text_area :directions %></p>
1717

18-
<p><%= f.label :time %></p>
19-
<span><%= f.text_field :time %></span>
20-
<span><%= f.label "in minutes" %></span>
18+
<p><%= f.label :time %></p>
19+
<span><%= f.text_field :time %></span>
20+
<span><%= f.label "in minutes" %></span>
2121

22-
<p><%= f.label :difficulty %></p>
23-
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']], selected: params[:difficulty]) %></p><br>
22+
<p><%= f.label :difficulty %></p>
23+
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']], selected: params[:difficulty]) %></p><br>
2424

25-
<%= f.submit %>
26-
<% end %>
25+
<%= f.submit %>
26+
<% end %>
2727

28+
</article>

0 commit comments

Comments
 (0)