Skip to content
Browse files

Refactor on following:

* move rateings_controller#create to submissions_controller#rating
* change url to post/put for sumbit rating
* add Submission#rating
* add attr_accessible :rate to Rating to prevent someone updates on other user's ratings
  • Loading branch information...
1 parent 6f6c44e commit 5411d2dbf4dd1b9ed6b5d9bab63cff10edf1e023 @samnang committed Feb 24, 2012
View
11 app/controllers/puzzles/ratings_controller.rb
@@ -1,11 +0,0 @@
-class Puzzles::RatingsController < Puzzles::Base
- def create
- options = params[:rating]
- rating = Rating.find_or_initialize_by_user_id_and_puzzle_id(options[:user_id], options[:puzzle_id])
- if rating.update_attributes(options)
- head :ok
- else
- render :json => {:errors => rating.errors.full_messages}, :status => 422
- end
- end
-end
View
11 app/controllers/puzzles/submissions_controller.rb
@@ -1,5 +1,4 @@
class Puzzles::SubmissionsController < Puzzles::Base
-
def new
@submission = @puzzle.submissions.new
end
@@ -18,4 +17,14 @@ def create
render :action => :new
end
end
+
+ def rating
+ submission = current_user.submissions.find(params[:id])
+ @rating = submission.rating
+ if @rating.update_attributes(params[:rating])
+ head :ok
+ else
+ render :json => {:errors => @rating.errors.full_messages}, :status => 422
+ end
+ end
end
View
1 app/controllers/users/submissions_controller.rb
@@ -4,6 +4,7 @@ class Users::SubmissionsController < ApplicationController
def show
@puzzle = @submission.puzzle
+ @rating = @submission.rating if @submission.correct?
end
def update
View
2 app/models/rating.rb
@@ -8,6 +8,8 @@ class Rating < ActiveRecord::Base
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
5 app/models/submission.rb
@@ -11,6 +11,11 @@ class Submission < ActiveRecord::Base
attr_accessor :file
+ def rating
+ @rating ||= Rating.where(:puzzle_id => puzzle_id, :user_id => user_id)
+ @rating.first || @rating.new
+ end
+
private
def score!
View
6 app/views/users/submissions/_puzzle_rating.html.haml
@@ -1,11 +1,11 @@
-= form_for [rating.puzzle, rating], :remote => true do |f|
+= 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.parameterize('_')}", difficulty
+ = f.label :rate, difficulty, :value => difficulty
%p
- = f.submit "Rate"
+ = f.submit "Rate It"
View
4 app/views/users/submissions/show.html.haml
@@ -18,8 +18,8 @@
listed below, contact us at puzzlenode@gmail.com. You may also be able to
find help by checking out the #mendicant channel on Freenode.
- - if @submission.correct
- = render "puzzle_rating", :rating => @puzzle.ratings.build(:user => current_user)
+ - if @submission.correct?
+ = render "puzzle_rating"
#action
- if @submission.correct
View
5 config/routes.rb
@@ -10,9 +10,10 @@
:as => "attachment"
resources :puzzles do
- resources :submissions, :controller => "Puzzles::Submissions"
resources :comments, :controller => "Puzzles::Comments"
- post "ratings" => "puzzles/ratings#create"
+ resources :submissions, :controller => "Puzzles::Submissions" do
+ match :rating, :on => :member, :via => [:put, :post]
+ end
end
match '/tags/:tag', :to => 'puzzles#tag',

0 comments on commit 5411d2d

Please sign in to comment.
Something went wrong with that request. Please try again.