Skip to content

Instantly share code, notes, and snippets.

@petehuang
Created June 16, 2013 19:27
Show Gist options
  • Save petehuang/5793102 to your computer and use it in GitHub Desktop.
Save petehuang/5793102 to your computer and use it in GitHub Desktop.
Trying to override "rate" to not save a rating. From letsrate gem.
$.fn.raty.defaults.path = "/assets";
$.fn.raty.defaults.half_show = true;
$(function(){
$(".star").each(function() {
var $readonly = ($(this).attr('data-readonly') == 'true');
$(this).raty({
score: function(){
return $(this).attr('data-rating')
},
number: function() {
return $(this).attr('data-star-count')
},
readOnly: $readonly,
click: function(score, evt) {
var _this = this;
$.post('<%= Rails.application.class.routes.url_helpers.rate_path %>',
{
score: score,
dimension: $(this).attr('data-dimension'),
id: $(this).attr('data-id'),
klass: $(this).attr('data-classname')
},
function(data) {
if(data) {
// success code goes here ...
if ($(_this).attr('data-disable-after-rate') == 'true') {
$(_this).raty('set', { readOnly: true, score: score });
}
}
});
}
});
});
});
class RaterController < ApplicationController
def create
if current_user.present?
obj = eval "#{params[:klass]}.find(#{params[:id]})"
if params[:dimension].present?
obj.rate params[:score].to_i, current_user.id, "#{params[:dimension]}"
else
obj.rate params[:score].to_i, current_user.id
end
render :json => true
else
render :json => false
end
end
end
module RatingsHelper
def rate(stars, user_id, dimension=nil)
if can_rate? user_id, dimension
r = rates(dimension).build
r.stars = stars
r.rater_id = user_id
else
raise "User has already rated."
end
end
.
.
.
.
Started POST "/rate" for 127.0.0.1 at 2013-06-16 14:22:37 -0500
Processing by RaterController#create as */*
Parameters: {"score"=>"3", "dimension"=>"manner", "id"=>"1", "klass"=>"Doctor"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Doctor Load (0.1ms) SELECT "doctors".* FROM "doctors" WHERE "doctors"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) select count(*) as cnt from rates where rateable_id=1 and rateable_type='Doctor' and rater_id=3 and dimension='manner'
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "rates" ("created_at", "dimension", "rateable_id", "rateable_type", "rater_id", "stars", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Sun, 16 Jun 2013 19:22:37 UTC +00:00], ["dimension", "manner"], ["rateable_id", 1], ["rateable_type", "Doctor"], ["rater_id", 3], ["stars", 3.0], ["updated_at", Sun, 16 Jun 2013 19:22:37 UTC +00:00]]
(2.3ms) commit transaction
RatingCache Load (0.2ms) SELECT "rating_caches".* FROM "rating_caches" WHERE "rating_caches"."cacheable_id" = 1 AND "rating_caches"."cacheable_type" = 'Doctor' AND "rating_caches"."dimension" = 'manner' LIMIT 1
(0.0ms) begin transaction
SQL (0.4ms) INSERT INTO "rating_caches" ("avg", "cacheable_id", "cacheable_type", "created_at", "dimension", "qty", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["avg", 3.0], ["cacheable_id", 1], ["cacheable_type", "Doctor"], ["created_at", Sun, 16 Jun 2013 19:22:37 UTC +00:00], ["dimension", "manner"], ["qty", 1], ["updated_at", Sun, 16 Jun 2013 19:22:37 UTC +00:00]]
(2.2ms) commit transaction
(0.1ms) begin transaction
(0.0ms) commit transaction
Completed 200 OK in 60ms (Views: 0.2ms | ActiveRecord: 6.9ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment