Permalink
Browse files

Rename Rating -> Difficulty model and prevent users to update other u…

…ser's ratings
  • Loading branch information...
1 parent 5411d2d commit 9f88badd9345027483aada246194dbc575f7f206 @samnang committed Feb 24, 2012
@@ -20,11 +20,11 @@ def create
def rating
submission = current_user.submissions.find(params[:id])
- @rating = submission.rating
- if @rating.update_attributes(params[:rating])
+ @difficulty = submission.difficulty
+ if @difficulty.update_attributes(:rating => params[:difficulty][:rating])
head :ok
else
- render :json => {:errors => @rating.errors.full_messages}, :status => 422
+ render :json => {:errors => @difficulty.errors.full_messages}, :status => 422
end
end
end
@@ -4,7 +4,7 @@ class Users::SubmissionsController < ApplicationController
def show
@puzzle = @submission.puzzle
- @rating = @submission.rating if @submission.correct?
+ @difficulty = @submission.difficulty if @submission.correct?
end
def update
@@ -1,15 +1,13 @@
-class Rating < ActiveRecord::Base
+class Difficulty < ActiveRecord::Base
DIFFICULTIES = ["Not Challenging", "Somewhat Challenging", "Very Challenging"]
belongs_to :user
belongs_to :puzzle
- validates :rate, :inclusion => {:in => DIFFICULTIES}
+ validates :rating, :inclusion => {:in => DIFFICULTIES}
validates :user_id, :presence => true, :uniqueness => {:scope => :puzzle_id, :message => "has been rated already"}
validate :check_user_got_correct_sumsission
- attr_accessible :rate
-
def check_user_got_correct_sumsission
errors.add(:user, "can't rate the puzzle unless you get correct submission.") unless puzzle.answered_correctly?(user)
end
View
@@ -1,10 +1,10 @@
class Puzzle < ActiveRecord::Base
acts_as_taggable
- has_many :submissions, :dependent => :destroy
- has_many :comments, :dependent => :destroy, :order => "created_at"
- has_many :attachments, :dependent => :destroy
- has_many :ratings, :dependent => :destroy
+ has_many :submissions, :dependent => :destroy
+ has_many :comments, :dependent => :destroy, :order => "created_at"
+ has_many :attachments, :dependent => :destroy
+ has_many :difficulties, :dependent => :destroy
accepts_nested_attributes_for :attachments, :allow_destroy => true
View
@@ -11,9 +11,9 @@ class Submission < ActiveRecord::Base
attr_accessor :file
- def rating
- @rating ||= Rating.where(:puzzle_id => puzzle_id, :user_id => user_id)
- @rating.first || @rating.new
+ def difficulty
+ @difficulty ||= Difficulty.where(:puzzle_id => puzzle_id, :user_id => user_id)
+ @difficulty.first || @difficulty.new
end
private
@@ -0,0 +1,4 @@
+%span.ratings
+ - puzzle.ratings.count(:group => :rate).each do |difficulty, count|
+ %a{:title => difficulty, :class => difficulty.parameterize}
+ = count
@@ -0,0 +1,11 @@
+= form_for @difficulty, :url => rating_puzzle_submission_path(@puzzle, @submission), :remote => true do |f|
+ = f.hidden_field :user_id
+ = f.hidden_field :puzzle_id
+ %p
+ Puzzle difficulty:
+ - Difficulty::DIFFICULTIES.each do |difficulty|
+ %br
+ = f.radio_button :rating, difficulty
+ = f.label :rating, difficulty, :value => difficulty
+ %p
+ = f.submit "Rate It"
@@ -1,11 +0,0 @@
-= form_for @rating, :url => rating_puzzle_submission_path(@puzzle, @submission), :remote => true do |f|
- = f.hidden_field :user_id
- = f.hidden_field :puzzle_id
- %p
- Puzzle difficulty:
- - Rating::DIFFICULTIES.each do |difficulty|
- %br
- = f.radio_button :rate, difficulty
- = f.label :rate, difficulty, :value => difficulty
- %p
- = f.submit "Rate It"
@@ -19,7 +19,7 @@
find help by checking out the #mendicant channel on Freenode.
- if @submission.correct?
- = render "puzzle_rating"
+ = render "puzzle_difficulty"
#action
- if @submission.correct
@@ -0,0 +1,15 @@
+class CreateDifficulties < ActiveRecord::Migration
+ def change
+ create_table :difficulties do |t|
+ t.references :user
+ t.references :puzzle
+ t.string :rating
+
+ t.timestamps
+ end
+
+ add_index :difficulties, :user_id
+ add_index :difficulties, :puzzle_id
+ add_index :difficulties, [:user_id, :puzzle_id], :unique => true
+ end
+end
@@ -1,15 +0,0 @@
-class CreateRatings < ActiveRecord::Migration
- def change
- create_table :ratings do |t|
- t.references :user
- t.references :puzzle
- t.string :rate
-
- t.timestamps
- end
-
- add_index :ratings, :user_id
- add_index :ratings, :puzzle_id
- add_index :ratings, [:user_id, :puzzle_id], :unique => true
- end
-end
View
@@ -61,6 +61,18 @@
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
+ create_table "difficulties", :force => true do |t|
+ t.integer "user_id"
+ t.integer "puzzle_id"
+ t.string "rating"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "difficulties", ["puzzle_id"], :name => "index_difficulties_on_puzzle_id"
+ add_index "difficulties", ["user_id", "puzzle_id"], :name => "index_difficulties_on_user_id_and_puzzle_id", :unique => true
+ add_index "difficulties", ["user_id"], :name => "index_difficulties_on_user_id"
+
create_table "emails", :force => true do |t|
t.string "from_address", :null => false
t.string "reply_to_address"
@@ -89,18 +101,6 @@
t.string "slug"
end
- create_table "ratings", :force => true do |t|
- t.integer "user_id"
- t.integer "puzzle_id"
- t.string "rate"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
-
- add_index "ratings", ["puzzle_id"], :name => "index_ratings_on_puzzle_id"
- add_index "ratings", ["user_id", "puzzle_id"], :name => "index_ratings_on_user_id_and_puzzle_id", :unique => true
- add_index "ratings", ["user_id"], :name => "index_ratings_on_user_id"
-
create_table "submissions", :force => true do |t|
t.integer "user_id"
t.integer "puzzle_id"

0 comments on commit 9f88bad

Please sign in to comment.