parent
4676eed9a3
commit
0b5ea87f11
@ -0,0 +1,2 @@
|
|||||||
|
// Place all the behaviors and hooks related to the matching controller here.
|
||||||
|
// All this logic will automatically be available in application.js.
|
@ -0,0 +1,225 @@
|
|||||||
|
/************************************************************************
|
||||||
|
*************************************************************************
|
||||||
|
@Name : jRating - jQuery Plugin
|
||||||
|
@Revison : 3.0
|
||||||
|
@Date : 28/01/2013
|
||||||
|
@Author: ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr)
|
||||||
|
@License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
|
||||||
|
|
||||||
|
**************************************************************************
|
||||||
|
*************************************************************************/
|
||||||
|
(function($) {
|
||||||
|
$.fn.jRating = function(op) {
|
||||||
|
var defaults = {
|
||||||
|
/** String vars **/
|
||||||
|
bigStarsPath : '<%= image_path "seems_rateable/stars.png" %>', // path of the icon stars.png
|
||||||
|
smallStarsPath : '<%= image_path "seems_rateable/small.png" %>', // path of the icon small.png
|
||||||
|
path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
|
||||||
|
type : 'big', // can be set to 'small' or 'big'
|
||||||
|
|
||||||
|
/** Boolean vars **/
|
||||||
|
step:false, // if true, mouseover binded star by star,
|
||||||
|
isDisabled:false,
|
||||||
|
showRateInfo: false,
|
||||||
|
canRateAgain : false,
|
||||||
|
|
||||||
|
/** Integer vars **/
|
||||||
|
length:5, // number of star to display
|
||||||
|
decimalLength : 0, // number of decimals.. Max 3, but you can complete the function 'getNote'
|
||||||
|
rateMax : 20, // maximal rate - integer from 0 to 9999 (or more)
|
||||||
|
rateInfosX : -45, // relative position in X axis of the info box when mouseover
|
||||||
|
rateInfosY : 5, // relative position in Y axis of the info box when mouseover
|
||||||
|
nbRates : 1,
|
||||||
|
|
||||||
|
/** Functions **/
|
||||||
|
onSuccess : null,
|
||||||
|
onError : null
|
||||||
|
};
|
||||||
|
|
||||||
|
if(this.length>0)
|
||||||
|
return this.each(function() {
|
||||||
|
/*vars*/
|
||||||
|
var opts = $.extend(defaults, op),
|
||||||
|
newWidth = 0,
|
||||||
|
starWidth = 0,
|
||||||
|
starHeight = 0,
|
||||||
|
bgPath = '',
|
||||||
|
hasRated = false,
|
||||||
|
globalWidth = 0,
|
||||||
|
nbOfRates = opts.nbRates;
|
||||||
|
|
||||||
|
if($(this).hasClass('jDisabled') || opts.isDisabled)
|
||||||
|
var jDisabled = true;
|
||||||
|
else
|
||||||
|
var jDisabled = false;
|
||||||
|
|
||||||
|
getStarWidth();
|
||||||
|
$(this).height(starHeight);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var average = parseFloat($(this).attr('data-average')), // get the average of all rates
|
||||||
|
idBox = parseInt($(this).attr('data-id')), // get the id of the box
|
||||||
|
kls = $(this).attr('data-kls'),
|
||||||
|
dimension = $(this).attr('data-dimension'),
|
||||||
|
widthRatingContainer = starWidth*opts.length, // Width of the Container
|
||||||
|
widthColor = average/opts.rateMax*widthRatingContainer, // Width of the color Container
|
||||||
|
quotient =
|
||||||
|
$('<div>',
|
||||||
|
{
|
||||||
|
'class' : 'jRatingColor',
|
||||||
|
css:{
|
||||||
|
width:widthColor
|
||||||
|
}
|
||||||
|
}).appendTo($(this)),
|
||||||
|
|
||||||
|
average =
|
||||||
|
$('<div>',
|
||||||
|
{
|
||||||
|
'class' : 'jRatingAverage',
|
||||||
|
css:{
|
||||||
|
width:0,
|
||||||
|
top:- starHeight
|
||||||
|
}
|
||||||
|
}).appendTo($(this)),
|
||||||
|
|
||||||
|
jstar =
|
||||||
|
$('<div>',
|
||||||
|
{
|
||||||
|
'class' : 'jStar',
|
||||||
|
css:{
|
||||||
|
width:widthRatingContainer,
|
||||||
|
height:starHeight,
|
||||||
|
top:- (starHeight*2),
|
||||||
|
background: 'url('+bgPath+') repeat-x'
|
||||||
|
}
|
||||||
|
}).appendTo($(this));
|
||||||
|
|
||||||
|
$(this).css({width: widthRatingContainer,overflow:'hidden',zIndex:1,position:'relative'});
|
||||||
|
|
||||||
|
if(!jDisabled)
|
||||||
|
$(this).unbind().bind({
|
||||||
|
mouseenter : function(e){
|
||||||
|
var realOffsetLeft = findRealLeft(this);
|
||||||
|
var relativeX = e.pageX - realOffsetLeft;
|
||||||
|
if (opts.showRateInfo)
|
||||||
|
var tooltip =
|
||||||
|
$('<p>',{
|
||||||
|
'class' : 'jRatingInfos',
|
||||||
|
html : getNote(relativeX)+' <span class="maxRate">/ '+opts.rateMax+'</span>',
|
||||||
|
css : {
|
||||||
|
top: (e.pageY + opts.rateInfosY),
|
||||||
|
left: (e.pageX + opts.rateInfosX)
|
||||||
|
}
|
||||||
|
}).appendTo('body').show();
|
||||||
|
},
|
||||||
|
mouseover : function(e){
|
||||||
|
$(this).css('cursor','pointer');
|
||||||
|
},
|
||||||
|
mouseout : function(){
|
||||||
|
$(this).css('cursor','default');
|
||||||
|
if(hasRated) average.width(globalWidth);
|
||||||
|
else average.width(0);
|
||||||
|
},
|
||||||
|
mousemove : function(e){
|
||||||
|
var realOffsetLeft = findRealLeft(this);
|
||||||
|
var relativeX = e.pageX - realOffsetLeft;
|
||||||
|
if(opts.step) newWidth = Math.floor(relativeX/starWidth)*starWidth + starWidth;
|
||||||
|
else newWidth = relativeX;
|
||||||
|
average.width(newWidth);
|
||||||
|
if (opts.showRateInfo)
|
||||||
|
$("p.jRatingInfos")
|
||||||
|
.css({
|
||||||
|
left: (e.pageX + opts.rateInfosX)
|
||||||
|
})
|
||||||
|
.html(getNote(newWidth) +' <span class="maxRate">/ '+opts.rateMax+'</span>');
|
||||||
|
},
|
||||||
|
mouseleave : function(){
|
||||||
|
$("p.jRatingInfos").remove();
|
||||||
|
},
|
||||||
|
click : function(e){
|
||||||
|
var element = this;
|
||||||
|
|
||||||
|
/*set vars*/
|
||||||
|
hasRated = true;
|
||||||
|
globalWidth = newWidth;
|
||||||
|
nbOfRates--;
|
||||||
|
|
||||||
|
if(!opts.canRateAgain || parseInt(nbOfRates) <= 0) $(this).unbind().css('cursor','default').addClass('jDisabled');
|
||||||
|
|
||||||
|
if (opts.showRateInfo) $("p.jRatingInfos").fadeOut('fast',function(){$(this).remove();});
|
||||||
|
e.preventDefault();
|
||||||
|
var rate = getNote(newWidth);
|
||||||
|
average.width(newWidth);
|
||||||
|
|
||||||
|
|
||||||
|
$.post(defaults.path,
|
||||||
|
{
|
||||||
|
idBox : idBox,
|
||||||
|
rate : rate,
|
||||||
|
kls : kls,
|
||||||
|
dimension : dimension
|
||||||
|
/** action : 'rating' **/
|
||||||
|
},
|
||||||
|
function(data) {
|
||||||
|
if(!data.error)
|
||||||
|
{
|
||||||
|
/** Here you can display an alert box,
|
||||||
|
or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
|
||||||
|
exemple : */
|
||||||
|
if(opts.onSuccess) opts.onSuccess( element, rate );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
/** Here you can display an alert box,
|
||||||
|
or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
|
||||||
|
exemple : */
|
||||||
|
if(opts.onError) opts.onError( element, rate );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'json'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function getNote(relativeX) {
|
||||||
|
var noteBrut = parseFloat((relativeX*100/widthRatingContainer)*opts.rateMax/100);
|
||||||
|
switch(opts.decimalLength) {
|
||||||
|
case 1 :
|
||||||
|
var note = Math.round(noteBrut*10)/10;
|
||||||
|
break;
|
||||||
|
case 2 :
|
||||||
|
var note = Math.round(noteBrut*100)/100;
|
||||||
|
break;
|
||||||
|
case 3 :
|
||||||
|
var note = Math.round(noteBrut*1000)/1000;
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
var note = Math.round(noteBrut*1)/1;
|
||||||
|
}
|
||||||
|
return note;
|
||||||
|
};
|
||||||
|
|
||||||
|
function getStarWidth(){
|
||||||
|
switch(opts.type) {
|
||||||
|
case 'small' :
|
||||||
|
starWidth = 12; // width of the picture small.png
|
||||||
|
starHeight = 10; // height of the picture small.png
|
||||||
|
bgPath = opts.smallStarsPath;
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
starWidth = 23; // width of the picture stars.png
|
||||||
|
starHeight = 20; // height of the picture stars.png
|
||||||
|
bgPath = opts.bigStarsPath;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function findRealLeft(obj) {
|
||||||
|
if( !obj ) return 0;
|
||||||
|
return obj.offsetLeft + findRealLeft( obj.offsetParent );
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
})(jQuery);
|
@ -0,0 +1,25 @@
|
|||||||
|
$(document).ready(function(){
|
||||||
|
$(".rateable").jRating({
|
||||||
|
//default options displayed below ->
|
||||||
|
|
||||||
|
rateMax: 5, //Maximal rate
|
||||||
|
length : 5, //Number of stars
|
||||||
|
//decimalLength : 0, //Number of decimals in the rate
|
||||||
|
//type : 'big', //Big or small
|
||||||
|
//step : true, //If set to true, filling of the stars is done star by star (step by step).
|
||||||
|
//isDisabled: false, //Set true to display static rating
|
||||||
|
//showRateInfo:false, //Rate info panel, set true to display
|
||||||
|
//rateInfosX : 45, //In pixel - Absolute left position of the information box during mousemove.
|
||||||
|
//rateInfosY : 5, //In pixel - Absolute top position of the information box during mousemove.
|
||||||
|
path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
|
||||||
|
onSuccess : function(element, rate){
|
||||||
|
//something like ->
|
||||||
|
//alert('success');
|
||||||
|
$('<span class="text-success"><small style="display:inline-block;">Thanks for rating!</small></span>').insertAfter(element)
|
||||||
|
},
|
||||||
|
onError : function(element, rate) {
|
||||||
|
$('<span class="text-error"><small style="display:inline-block;">You have already rated!</small></span>').insertAfter(element)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@ -0,0 +1,4 @@
|
|||||||
|
/*
|
||||||
|
Place all the styles related to the matching controller here.
|
||||||
|
They will automatically be included in application.css.
|
||||||
|
*/
|
@ -0,0 +1,23 @@
|
|||||||
|
module PraiseTreadHelper
|
||||||
|
#added by william
|
||||||
|
def is_praise_or_tread(object,user_id)
|
||||||
|
@obj_type = object.class.name.underscore
|
||||||
|
@obj_id = object.id
|
||||||
|
@is_praise = PraiseTread.find_by_sql("select * from praise_treads where user_id=#{user_id} and " +
|
||||||
|
"praise_tread_object_type='#{@obj_type}' and praise_tread_object_id=#{@obj_id} ")
|
||||||
|
return @is_praise
|
||||||
|
end
|
||||||
|
#end
|
||||||
|
|
||||||
|
def get_praise_num(object)
|
||||||
|
@obj_type = object.class.name.underscore
|
||||||
|
@obj_id = object.id
|
||||||
|
@record = PraiseTreadCache.find_by_object_id_and_object_type(@obj_id,@obj_type)
|
||||||
|
if @record
|
||||||
|
return @record.praise_num
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,4 @@
|
|||||||
|
class PraiseTread < ActiveRecord::Base
|
||||||
|
attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,11 @@
|
|||||||
|
class PraiseTreadCache < ActiveRecord::Base
|
||||||
|
attr_accessible :object_id,:object_type,:praise_num,:tread_num
|
||||||
|
|
||||||
|
def plus(num)
|
||||||
|
self.update_attribute(:praise_num, self.praise_num.to_i + num)
|
||||||
|
end
|
||||||
|
|
||||||
|
def minus(num)
|
||||||
|
self.update_attribute(:praise_num, self.praise_num.to_i - num)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,17 @@
|
|||||||
|
<div id="praise">
|
||||||
|
<% if is_praise_or_tread(obj,user_id).size > 0 %>
|
||||||
|
<%= image_tag("/images/praise.png") %>
|
||||||
|
<%= link_to "取消贊",:controller=>"praise_tread",:action=>"praise_minus",:remote=>true,:obj => obj %>
|
||||||
|
(<%= get_praise_num(obj)%>)
|
||||||
|
<% else %>
|
||||||
|
<%= image_tag("/images/tread.png") %>
|
||||||
|
<%= link_to "贊",:controller=>"praise_tread",:action=>"praise_plus",:remote=>true,:obj => obj %>
|
||||||
|
(<%= get_praise_num(obj)%>)
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% if show_flag %>
|
||||||
|
<div id="tread">
|
||||||
|
<%= link_to image_tag("/images/tread.png"),:controller=>"praise_tread",
|
||||||
|
:action=>"tread_minus",:remote=>true,:obj => obj %>踩
|
||||||
|
</div>
|
||||||
|
<% end %>
|
@ -0,0 +1,3 @@
|
|||||||
|
$('#praise_tread').html('<%= j(
|
||||||
|
render :partial => "/praise_tread/praise_tread",:locals => {:obj => @obj,:show_flag => false,:user_id => User.current.id}
|
||||||
|
)%>');
|
@ -0,0 +1,3 @@
|
|||||||
|
$('#praise_tread').html('<%= j(
|
||||||
|
render :partial => "/praise_tread/praise_tread",:locals => {:obj => @obj,:show_flag => false,:user_id => User.current.id}
|
||||||
|
)%>');
|
@ -0,0 +1,4 @@
|
|||||||
|
#SeemsRateable engine Initializer
|
||||||
|
|
||||||
|
#Configure owner class of the given ratings
|
||||||
|
SeemsRateable::Engine.config.owner_class = "User"
|
@ -0,0 +1,18 @@
|
|||||||
|
class CreateSeemsRateableRates < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
create_table :seems_rateable_rates do |t|
|
||||||
|
t.belongs_to :rater
|
||||||
|
t.belongs_to :rateable, :polymorphic => true
|
||||||
|
t.float :stars, :null => false
|
||||||
|
t.integer :rater_id, :limit => 8
|
||||||
|
t.integer :rateable_id
|
||||||
|
t.string :rateable_type
|
||||||
|
t.string :dimension
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
drop_table :rates
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,17 @@
|
|||||||
|
class CreateSeemsRateableCachedRatings < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
create_table :seems_rateable_cached_ratings do |t|
|
||||||
|
t.belongs_to :cacheable, :polymorphic => true
|
||||||
|
t.float :avg, :null => false
|
||||||
|
t.integer :cnt, :null => false
|
||||||
|
t.string :dimension
|
||||||
|
t.integer :cacheable_id, :limit => 8
|
||||||
|
t.string :cacheable_type
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
drop_table :cached_ratings
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,15 @@
|
|||||||
|
class CreatePraiseTreads < ActiveRecord::Migration
|
||||||
|
def self.up
|
||||||
|
create_table :praise_treads do |t|
|
||||||
|
t.column :user_id,:integer,:null => false
|
||||||
|
t.column :praise_tread_object_id,:integer
|
||||||
|
t.column :praise_tread_object_type,:string
|
||||||
|
t.column :praise_or_tread,:integer
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.down
|
||||||
|
drop_table :praise_treads
|
||||||
|
end
|
||||||
|
end
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue