Permalink
Browse files

Finished star-ratings

  • Loading branch information...
1 parent 6920017 commit 88cbb9fbd96f5e03fbfeb6eed9eaac8bf3a6745c @cpursley committed Nov 24, 2012
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -100,3 +100,11 @@ footer ul li {
float: left;
margin-left: 10px;
}
+
+/* star-rating */
+
+.stars * { margin: 0; padding: 0; list-style: none; }
+.stars { height: 25px; width: 125px; background: url('../images/star.png') left top repeat-x; }
+.stars .yellowstars { height: 25px; width: 0px; background: url('../images/star.png') left repeat-x; }
+.stars .text { text-align: right; font-size: 10px; }
+.floatstars { float: right; }
@@ -0,0 +1,3 @@
+// Place all the styles related to the ratings controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -28,6 +28,13 @@ def show
@dream = Dream.find(params[:id])
@comments = @dream.comments.all
+ if user_signed_in?
+ @rating_currentuser = @dream.ratings.find_by_user_id(current_user.id)
+ unless @rating_currentuser
+ @rating_currentuser = current_user.ratings.new
+ end
+ end
+
respond_to do |format|
format.html # show.html.erb
format.json { render json: @dream }
@@ -0,0 +1,45 @@
+class RatingsController < ApplicationController
+ before_filter :authenticate_user!
+
+ def create
+ @dream = Dream.find(params[:dream_id])
+ @rating = @dream.ratings.build(params[:rating])
+ @rating.user = current_user
+
+ respond_to do |format|
+ if @dream.state == 1
+ if @rating.save
+ format.html { redirect_to(@dream, :notice => 'Thank you for rating this dream!') }
+ else
+ format.html { redirect_to(@dream, :notice => 'There was an error saving your rating.') }
+ end
+ else
+ format.html { redirect_to(@dream, :notice => 'Ratings are limited to published dreams only.') }
+ end
+ end
+ end
+
+ def update
+ @rating = current_user.ratings.find(params[:id])
+ @dream = Dream.find(params[:dream_id])
+
+ respond_to do |format|
+ if @rating.update_attributes(params[:rating])
+ format.html { redirect_to(@dream, :notice => 'Thank you for updating your rating!') }
+ else
+ format.html { redirect_to(@dream, :notice => 'There was an error saving your rating.') }
+ end
+ end
+ end
+
+ def destroy
+ @rating = current_user.ratings.find(params[:id])
+ @dream = Dream.find(params[:dream_id])
+
+ @rating.destroy
+
+ respond_to do |format|
+ format.html { redirect_to @dream }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module RatingsHelper
+end
View
@@ -1,6 +1,7 @@
class Dream < ActiveRecord::Base
belongs_to :user
- has_many :comments, :dependent => :destroy
+ has_many :comments, :dependent => :destroy
+ has_many :ratings, :dependent => :destroy
attr_accessible :title, :teaser, :body, :version, :changelog, :user_id, :message, :freezebody, :state, :submitted, :accepted
@@ -13,6 +14,15 @@ class Dream < ActiveRecord::Base
validates :message, :length => { :maximum => 5000 }
validates :state, :presence => true, :numericality => true, :inclusion => { :in => 0..1 }
+def count_ratings
+ self.ratings.all.count
+end
+
+def avg_rating
+ @avg = self.ratings.average(:stars)
+ @avg ? @avg : 0
+end
+
protected
def record_not_found
flash[:error] = 'The dream you requested could not be found.'
View
@@ -0,0 +1,10 @@
+class Rating < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :dream
+
+ attr_accessible :dream_id, :stars, :user_id
+
+ validates :user_id, :presence => true
+ validates :dream_id, :presence => true
+ validates :stars, :presence => true, :numericality => true, :inclusion => { :in => 0..5 }
+end
View
@@ -7,6 +7,7 @@ class User < ActiveRecord::Base
has_many :dreams, :dependent => :destroy
has_many :comments, :dependent => :destroy
+ has_many :ratings, :dependent => :destroy
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :fullname, :shortbio, :weburl
@@ -1,8 +1,12 @@
<section id = "dreams">
<% @dreams.each do |dream| %>
- <dream class="dream">
+ <ul class="stars floatstars">
+ <li class="yellowstars" style="width: <%= dream.avg_rating * 25 %> px !important;"></li>
+ </ul>
+
<div class = "title"><%= link_to dream.title, dream %></div>
- <div class = "byline"><%= dream.created_at.strftime("%B %d, %Y") %></div>
+
+ <div class = "byline"><%= dream.created_at.strftime("%B %d, %Y") %> : <%= dream.state %></div>
<div class = "teaser"><%= dream.teaser %></div>
</dream>
<% end %>
@@ -1,5 +1,11 @@
<section id="dreamheader">
- <h2><%= @dream.title %></h2>
+ <h2>
+ <%= @dream.title %>
+ <ul class="stars floatstars">
+ <li class="yellowstars" style="width: <%= @dream.avg_rating * 25 %>px !important;"></li>
+ <li class="text"><%= @dream.avg_rating %> average from <%= pluralize(@dream.count_ratings, "vote") %></li>
+ </ul>
+ </h2>
<div class="byline">written by <%= link_to @dream.user.fullname, "" %></div>
<h3><%= @dream.teaser %></h3>
@@ -9,8 +15,28 @@
<%= @dream.body %>
</section>
-<% # comments for published articles only %>
+<% # comments for published dreams only %>
<% if @dream.state == 1 %>
+
+ <section id="rating">
+ <% if user_signed_in? %>
+ <%= form_for [@dream, @rating_currentuser] do |f| %>
+ <div id="ratingbox">
+ <ul id="ratingbuttons">
+ <li><%= f.radio_button :stars, "5" %><span>Excellent</span></li>
+ <li><%= f.radio_button :stars, "4" %><span>Very Good</span></li>
+ <li><%= f.radio_button :stars, "3" %><span>Good</span></li>
+ <li><%= f.radio_button :stars, "2" %><span>Average</span></li>
+ <li><%= f.radio_button :stars, "1" %><span>Poor</span></li>
+ </ul>
+ <div class="center"><%= f.submit "Rate this dream" %></div>
+ </div>
+ <% end %>
+ <% else %>
+ <div id="ratingbox"><%= link_to "Sign in", new_user_session_path %> to rate this dream</div>
+ <% end %>
+ </section>
+
<section id="comments">
<% @comments.each do |comment| %>
<dream class="comment">
View
@@ -8,7 +8,8 @@
get 'mydreams'
end
- resources :comments, :only => [:create, :destroy]
+ resources :comments, :only => [:create, :destroy]
+ resources :ratings, :only => [:create, :update, :destroy]
end
@@ -0,0 +1,13 @@
+class CreateRatings < ActiveRecord::Migration
+ def change
+ create_table :ratings do |t|
+ t.integer :user_id, :null => false
+ t.integer :dream_id, :null => false
+ t.integer :stars
+
+ t.timestamps
+ end
+
+ add_index :ratings, :dream_id
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121123212359) do
+ActiveRecord::Schema.define(:version => 20121124151038) do
create_table "comments", :force => true do |t|
t.integer "user_id"
@@ -39,6 +39,16 @@
t.datetime "updated_at", :null => false
end
+ create_table "ratings", :force => true do |t|
+ t.integer "user_id", :null => false
+ t.integer "dream_id", :null => false
+ t.integer "stars"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "ratings", ["dream_id"], :name => "index_ratings_on_dream_id"
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ user_id: 1
+ dream_id: 1
+ stars: 1
+
+two:
+ user_id: 1
+ dream_id: 1
+ stars: 1
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class RatingsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class RatingsHelperTest < ActionView::TestCase
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class RatingTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 88cbb9f

Please sign in to comment.