mongoid-letsrate 1.0.10 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b1076405c5d69f606e636b0dd7c6f1ce2626d5e
4
- data.tar.gz: 5b4df317255c7239227bd9c099d4afe03b3f3f04
3
+ metadata.gz: 074c706c0437cdd6bfa96e193dc8dd51981cd2df
4
+ data.tar.gz: 29d5427cdb2961d846ccbf1ab67d701005a82b2a
5
5
  SHA512:
6
- metadata.gz: cfbd6c745062b113b8955512648a87a9555c59496a89dea85256ccf9137ed3dff620bdcbbc3b5cdad62248a49881ce22c1a9b78bd43698839139912a2b6a507e
7
- data.tar.gz: f0a05ec10dbe82b4c1203726c5d5ea30083e40bd714ee72dd970577fbc0e5b69a25dfff7ca4f4f57a43624528db96f56f3e35c2a9fa29dceff713593f7582b1b
6
+ metadata.gz: fb6a25a527a3e60c9b53ba5e6d0e0bd91e8a4da13f3764e440c76ec190e5cb82573182bb8eb5e39332b4123cc54369a368805272da17769bca3326cae5c979a0
7
+ data.tar.gz: 6eb10ddea4bf364375473495850f19449c8c8122b90577cd04a13cd4a43a73a3702cd17f4a4ae0fcbbebb6b5cb2e4ea049e8e2f7a4c42af7d9cb0a01a5ada747
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-letsrate (1.0.10)
4
+ mongoid-letsrate (1.0.11)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -23,11 +23,11 @@ gem 'mongoid-letsrate'
23
23
  ### Generate
24
24
 
25
25
  ```
26
- rails g mongoid-letsrate User
26
+ rails g mongoid_letsrate User
27
27
  ```
28
28
 
29
- The generator takes one argument which is the name of your existing devise user model UserModelName. This is necessary to bind the user and rating datas.
30
- Also the generator copies necessary files (jquery raty plugin files, star icons and javascripts)
29
+ The generator takes one argument which is the name of your existing devise user model UserModelName. This is necessary to bind the user and rating data.
30
+ Also the generator copies necessary files (jquery.raty plugin files, star icons and javascripts)
31
31
 
32
32
  Example:
33
33
 
@@ -37,14 +37,14 @@ Suppose you will have a devise user model which name is User. The devise generat
37
37
  rails g devise:install
38
38
  rails g devise user
39
39
 
40
- rails g mongoid-letsrate user # => user is the model generated by devise
40
+ rails g mongoid_letsrate user # => user is the model generated by devise
41
41
  ```
42
42
 
43
- This generator will create Rate and RatingCache models and link to your user model.
43
+ This generator will create Rate and RatingCache models and link them to your user model.
44
44
 
45
45
  ### Prepare
46
46
 
47
- I suppose you have a car model
47
+ Suppose you have a car model
48
48
 
49
49
  ```
50
50
  rails g model car name:string
@@ -56,6 +56,7 @@ You should add the letsrate_rateable function.
56
56
  class Car
57
57
  include Mongoid::Document
58
58
  include Mongoid::Timestamps
59
+ include Mongoid::Letsrate
59
60
 
60
61
  letsrate_rateable
61
62
  end
@@ -66,6 +67,8 @@ Then you need to add a call letsrate_rater in the user model.
66
67
  ```ruby
67
68
  class User
68
69
  include Mongoid::Document
70
+ include Mongoid::Timestamps
71
+ include Mongoid::Letsrate
69
72
 
70
73
  letsrate_rater
71
74
  end
@@ -74,7 +77,7 @@ end
74
77
  ### Using
75
78
 
76
79
  There is a helper method which name is rating_for to add the star links. By default rating_for will display the average rating and accept the
77
- new rating value from authenticated user.
80
+ new rating value from authenticated users.
78
81
 
79
82
  ```erb
80
83
  <%# show.html.erb -> /cars/1 %>
@@ -9,7 +9,6 @@ class MongoidLetsrateGenerator < Rails::Generators::NamedBase
9
9
  copy_file 'star-off.png', 'app/assets/images/star-off.png'
10
10
  copy_file 'star-half.png', 'app/assets/images/star-half.png'
11
11
  copy_file 'letsrate.js.erb', 'app/assets/javascripts/letsrate.js.erb'
12
- copy_file 'rater_controller.rb', 'app/controllers/rater_controller.rb'
13
12
  end
14
13
 
15
14
  desc 'model is being created...'
@@ -19,6 +18,7 @@ class MongoidLetsrateGenerator < Rails::Generators::NamedBase
19
18
  class_collisions 'Rate'
20
19
  template 'model.rb', File.join('app/models', 'rate.rb')
21
20
  template 'cache_model.rb', File.join('app/models', 'rating_cache.rb')
21
+ template 'rater_controller.rb', File.join('app/controllers', 'rater_controller.rb')
22
22
  end
23
23
 
24
24
  def add_rate_path_to_route
@@ -1,464 +1,464 @@
1
1
  /*!
2
- * jQuery Raty - A Star Rating Plugin - http://wbotelhos.com/raty
3
- * -------------------------------------------------------------------
4
- *
5
- * jQuery Raty is a plugin that generates a customizable star rating.
2
+ * jQuery Raty - A Star Rating Plugin
6
3
  *
7
4
  * Licensed under The MIT License
8
5
  *
9
- * @version 2.4.5
10
- * @since 2010.06.11
11
- * @author Washington Botelho
12
- * @documentation wbotelhos.com/raty
13
- * @twitter twitter.com/wbotelhos
14
- *
15
- * Usage:
16
- * -------------------------------------------------------------------
17
- * $('#star').raty();
18
- *
19
- * <div id="star"></div>
6
+ * @author : Washington Botelho
7
+ * @doc : http://wbotelhos.com/raty
8
+ * @version : 2.5.2
20
9
  *
21
10
  */
22
11
 
23
12
  ;(function($) {
24
13
 
25
- var methods = {
26
- init: function(settings) {
27
- return this.each(function() {
28
- var self = this,
29
- $this = $(self).empty();
30
-
31
- self.opt = $.extend(true, {}, $.fn.raty.defaults, settings);
32
-
33
- $this.data('settings', self.opt);
34
-
35
- if (typeof self.opt.number == 'function') {
36
- self.opt.number = self.opt.number.call(self);
37
- } else {
38
- self.opt.number = methods.between(self.opt.number, 0, 20)
39
- }
40
-
41
- if (self.opt.path.substring(self.opt.path.length - 1, self.opt.path.length) != '/') {
42
- self.opt.path += '/';
43
- }
44
-
45
- if (typeof self.opt.score == 'function') {
46
- self.opt.score = self.opt.score.call(self);
47
- }
48
-
49
- if (self.opt.score) {
50
- self.opt.score = methods.between(self.opt.score, 0, self.opt.number);
51
- }
52
-
53
- for (var i = 1; i <= self.opt.number; i++) {
54
- $('<img />', {
55
- src : self.opt.path + ((!self.opt.score || self.opt.score < i) ? self.opt.starOff : self.opt.starOn),
56
- alt : i,
57
- title : (i <= self.opt.hints.length && self.opt.hints[i - 1] !== null) ? self.opt.hints[i - 1] : i
58
- }).appendTo(self);
59
-
60
- if (self.opt.space) {
61
- $this.append((i < self.opt.number) ? '&#160;' : '');
62
- }
63
- }
64
-
65
- self.stars = $this.children('img:not(".raty-cancel")');
66
- self.score = $('<input />', { type: 'hidden', name: self.opt.scoreName }).appendTo(self);
67
-
68
- if (self.opt.score && self.opt.score > 0) {
69
- self.score.val(self.opt.score);
70
- methods.roundStar.call(self, self.opt.score);
71
- }
72
-
73
- if (self.opt.iconRange) {
74
- methods.fill.call(self, self.opt.score);
75
- }
76
-
77
- methods.setTarget.call(self, self.opt.score, self.opt.targetKeep);
78
-
79
- var space = self.opt.space ? 4 : 0,
80
- width = self.opt.width || (self.opt.number * self.opt.size + self.opt.number * space);
81
-
82
- if (self.opt.cancel) {
83
- self.cancel = $('<img />', { src: self.opt.path + self.opt.cancelOff, alt: 'x', title: self.opt.cancelHint, 'class': 'raty-cancel' });
84
-
85
- if (self.opt.cancelPlace == 'left') {
86
- $this.prepend('&#160;').prepend(self.cancel);
87
- } else {
88
- $this.append('&#160;').append(self.cancel);
89
- }
90
-
91
- width += (self.opt.size + space);
92
- }
93
-
94
- if (self.opt.readOnly) {
95
- methods.fixHint.call(self);
96
-
97
- if (self.cancel) {
98
- self.cancel.hide();
99
- }
100
- } else {
101
- $this.css('cursor', 'pointer');
14
+ var methods = {
15
+ init: function(settings) {
16
+ return this.each(function() {
17
+ methods.destroy.call(this);
18
+
19
+ this.opt = $.extend(true, {}, $.fn.raty.defaults, settings);
20
+
21
+ var that = $(this),
22
+ inits = ['number', 'readOnly', 'score', 'scoreName'];
23
+
24
+ methods._callback.call(this, inits);
25
+
26
+ if (this.opt.precision) {
27
+ methods._adjustPrecision.call(this);
28
+ }
29
+
30
+ this.opt.number = methods._between(this.opt.number, 0, this.opt.numberMax);
31
+
32
+ this.opt.path = this.opt.path || '';
33
+
34
+ if (this.opt.path && this.opt.path.charAt( this.opt.path.length - 1 ) !== '/') {
35
+ this.opt.path += '/';
36
+ }
37
+
38
+ this.stars = methods._createStars.call(this);
39
+ this.score = methods._createScore.call(this);
40
+
41
+ methods._apply.call(this, this.opt.score);
42
+
43
+ var space = this.opt.space ? 4 : 0,
44
+ width = this.opt.width || (this.opt.number * this.opt.size + this.opt.number * space);
45
+
46
+ if (this.opt.cancel) {
47
+ this.cancel = methods._createCancel.call(this);
48
+
49
+ width += (this.opt.size + space);
50
+ }
51
+
52
+ if (this.opt.readOnly) {
53
+ methods._lock.call(this);
54
+ } else {
55
+ that.css('cursor', 'pointer');
56
+ methods._binds.call(this);
57
+ }
58
+
59
+ if (this.opt.width !== false) {
60
+ that.css('width', width);
61
+ }
62
+
63
+ methods._target.call(this, this.opt.score);
64
+
65
+ that.data({ 'settings': this.opt, 'raty': true });
66
+ });
67
+ }, _adjustPrecision: function() {
68
+ this.opt.targetType = 'score';
69
+ this.opt.half = true;
70
+ }, _apply: function(score) {
71
+ if (score && score > 0) {
72
+ score = methods._between(score, 0, this.opt.number);
73
+ this.score.val(score);
74
+ }
102
75
 
103
- methods.bindAction.call(self);
104
- }
105
-
106
- $this.css('width', width);
107
- });
108
- }, between: function(value, min, max) {
109
- return Math.min(Math.max(parseFloat(value), min), max);
110
- }, bindAction: function() {
111
- var self = this,
112
- $this = $(self);
113
-
114
- $this.mouseleave(function() {
115
- var score = self.score.val() || undefined;
116
-
117
- methods.initialize.call(self, score);
118
- methods.setTarget.call(self, score, self.opt.targetKeep);
119
-
120
- if (self.opt.mouseover) {
121
- self.opt.mouseover.call(self, score);
122
- }
123
- });
124
-
125
- var action = self.opt.half ? 'mousemove' : 'mouseover';
126
-
127
- if (self.opt.cancel) {
128
- self.cancel.mouseenter(function() {
129
- $(this).attr('src', self.opt.path + self.opt.cancelOn);
130
-
131
- self.stars.attr('src', self.opt.path + self.opt.starOff);
132
-
133
- methods.setTarget.call(self, null, true);
134
-
135
- if (self.opt.mouseover) {
136
- self.opt.mouseover.call(self, null);
137
- }
138
- }).mouseleave(function() {
139
- $(this).attr('src', self.opt.path + self.opt.cancelOff);
140
-
141
- if (self.opt.mouseover) {
142
- self.opt.mouseover.call(self, self.score.val() || null);
143
- }
144
- }).click(function(evt) {
145
- self.score.removeAttr('value');
146
-
147
- if (self.opt.click) {
148
- self.opt.click.call(self, null, evt);
149
- }
150
- });
151
- }
152
-
153
- self.stars.bind(action, function(evt) {
154
- var value = parseInt(this.alt, 10);
155
-
156
- if (self.opt.half) {
157
- var position = parseFloat((evt.pageX - $(this).offset().left) / self.opt.size),
158
- diff = (position > .5) ? 1 : .5;
159
-
160
- value = parseFloat(this.alt) - 1 + diff;
161
-
162
- methods.fill.call(self, value);
163
-
164
- if (self.opt.precision) {
165
- value = value - diff + position;
166
- }
167
-
168
- methods.showHalf.call(self, value);
169
- } else {
170
- methods.fill.call(self, value);
171
- }
172
-
173
- $this.data('score', value);
174
-
175
- methods.setTarget.call(self, value, true);
176
-
177
- if (self.opt.mouseover) {
178
- self.opt.mouseover.call(self, value, evt);
179
- }
180
- }).click(function(evt) {
181
- self.score.val((self.opt.half || self.opt.precision) ? $this.data('score') : this.alt);
182
-
183
- if (self.opt.click) {
184
- self.opt.click.call(self, self.score.val(), evt);
185
- }
186
- });
187
- }, cancel: function(isClick) {
188
- return $(this).each(function() {
189
- var self = this,
190
- $this = $(self);
191
-
192
- if ($this.data('readonly') === true) {
193
- return this;
194
- }
195
-
196
- if (isClick) {
197
- methods.click.call(self, null);
198
- } else {
199
- methods.score.call(self, null);
200
- }
201
-
202
- self.score.removeAttr('value');
203
- });
204
- }, click: function(score) {
205
- return $(this).each(function() {
206
- if ($(this).data('readonly') === true) {
207
- return this;
208
- }
209
-
210
- methods.initialize.call(this, score);
211
-
212
- if (this.opt.click) {
213
- this.opt.click.call(this, score);
214
- } else {
215
- methods.error.call(this, 'you must add the "click: function(score, evt) { }" callback.');
216
- }
217
-
218
- methods.setTarget.call(this, score, true);
219
- });
220
- }, error: function(message) {
221
- $(this).html(message);
222
-
223
- $.error(message);
224
- }, fill: function(score) {
225
- var self = this,
226
- number = self.stars.length,
227
- count = 0,
228
- $star ,
229
- star ,
230
- icon ;
231
-
232
- for (var i = 1; i <= number; i++) {
233
- $star = self.stars.eq(i - 1);
234
-
235
- if (self.opt.iconRange && self.opt.iconRange.length > count) {
236
- star = self.opt.iconRange[count];
237
-
238
- if (self.opt.single) {
239
- icon = (i == score) ? (star.on || self.opt.starOn) : (star.off || self.opt.starOff);
240
- } else {
241
- icon = (i <= score) ? (star.on || self.opt.starOn) : (star.off || self.opt.starOff);
242
- }
243
-
244
- if (i <= star.range) {
245
- $star.attr('src', self.opt.path + icon);
246
- }
247
-
248
- if (i == star.range) {
249
- count++;
250
- }
251
- } else {
252
- if (self.opt.single) {
253
- icon = (i == score) ? self.opt.starOn : self.opt.starOff;
254
- } else {
255
- icon = (i <= score) ? self.opt.starOn : self.opt.starOff;
256
- }
257
-
258
- $star.attr('src', self.opt.path + icon);
259
- }
260
- }
261
- }, fixHint: function() {
262
- var $this = $(this),
263
- score = parseInt(this.score.val(), 10),
264
- hint = this.opt.noRatedMsg;
265
-
266
- if (!isNaN(score) && score > 0) {
267
- hint = (score <= this.opt.hints.length && this.opt.hints[score - 1] !== null) ? this.opt.hints[score - 1] : score;
268
- }
269
-
270
- $this.data('readonly', true).css('cursor', 'default').attr('title', hint);
271
-
272
- this.score.attr('readonly', 'readonly');
273
- this.stars.attr('title', hint);
274
- }, getScore: function() {
275
- var score = [],
276
- value ;
277
-
278
- $(this).each(function() {
279
- value = this.score.val();
280
-
281
- score.push(value ? parseFloat(value) : undefined);
282
- });
283
-
284
- return (score.length > 1) ? score : score[0];
285
- }, readOnly: function(isReadOnly) {
286
- return this.each(function() {
287
- var $this = $(this);
288
-
289
- if ($this.data('readonly') === isReadOnly) {
290
- return this;
291
- }
292
-
293
- if (this.cancel) {
294
- if (isReadOnly) {
295
- this.cancel.hide();
296
- } else {
297
- this.cancel.show();
298
- }
299
- }
300
-
301
- if (isReadOnly) {
302
- $this.unbind();
303
-
304
- $this.children('img').unbind();
305
-
306
- methods.fixHint.call(this);
307
- } else {
308
- methods.bindAction.call(this);
309
- methods.unfixHint.call(this);
310
- }
311
-
312
- $this.data('readonly', isReadOnly);
313
- });
314
- }, reload: function() {
315
- return methods.set.call(this, {});
316
- }, roundStar: function(score) {
317
- var diff = (score - Math.floor(score)).toFixed(2);
318
-
319
- if (diff > this.opt.round.down) {
320
- var icon = this.opt.starOn; // Full up: [x.76 .. x.99]
321
-
322
- if (diff < this.opt.round.up && this.opt.halfShow) { // Half: [x.26 .. x.75]
323
- icon = this.opt.starHalf;
324
- } else if (diff < this.opt.round.full) { // Full down: [x.00 .. x.5]
325
- icon = this.opt.starOff;
326
- }
327
-
328
- this.stars.eq(Math.ceil(score) - 1).attr('src', this.opt.path + icon);
329
- } // Full down: [x.00 .. x.25]
330
- }, score: function() {
331
- return arguments.length ? methods.setScore.apply(this, arguments) : methods.getScore.call(this);
332
- }, set: function(settings) {
333
- this.each(function() {
334
- var $this = $(this),
335
- actual = $this.data('settings'),
336
- clone = $this.clone().removeAttr('style').insertBefore($this);
337
-
338
- $this.remove();
339
-
340
- clone.raty($.extend(actual, settings));
341
- });
342
-
343
- return $(this.selector);
344
- }, setScore: function(score) {
345
- return $(this).each(function() {
346
- if ($(this).data('readonly') === true) {
347
- return this;
348
- }
349
-
350
- methods.initialize.call(this, score);
351
- methods.setTarget.call(this, score, true);
352
- });
353
- }, setTarget: function(value, isKeep) {
354
- if (this.opt.target) {
355
- var $target = $(this.opt.target);
356
-
357
- if ($target.length == 0) {
358
- methods.error.call(this, 'target selector invalid or missing!');
359
- }
360
-
361
- var score = value;
362
-
363
- if (!isKeep || score === undefined) {
364
- score = this.opt.targetText;
365
- } else {
366
- if (this.opt.targetType == 'hint') {
367
- score = (score === null && this.opt.cancel)
368
- ? this.opt.cancelHint
369
- : this.opt.hints[Math.ceil(score - 1)];
370
- } else {
371
- score = this.opt.precision
372
- ? parseFloat(score).toFixed(1)
373
- : score;
374
- }
375
- }
376
-
377
- if (this.opt.targetFormat.indexOf('{score}') < 0) {
378
- methods.error.call(this, 'template "{score}" missing!');
379
- }
380
-
381
- if (value !== null) {
382
- score = this.opt.targetFormat.toString().replace('{score}', score);
383
- }
384
-
385
- if ($target.is(':input')) {
386
- $target.val(score);
387
- } else {
388
- $target.html(score);
389
- }
390
- }
391
- }, showHalf: function(score) {
392
- var diff = (score - Math.floor(score)).toFixed(1);
393
-
394
- if (diff > 0 && diff < .6) {
395
- this.stars.eq(Math.ceil(score) - 1).attr('src', this.opt.path + this.opt.starHalf);
396
- }
397
- }, initialize: function(score) {
398
- score = !score ? 0 : methods.between(score, 0, this.opt.number);
399
-
400
- methods.fill.call(this, score);
401
-
402
- if (score > 0) {
403
- if (this.opt.halfShow) {
404
- methods.roundStar.call(this, score);
405
- }
406
-
407
- this.score.val(score);
408
- }
409
- }, unfixHint: function() {
410
- for (var i = 0; i < this.opt.number; i++) {
411
- this.stars.eq(i).attr('title', (i < this.opt.hints.length && this.opt.hints[i] !== null) ? this.opt.hints[i] : i);
412
- }
413
-
414
- $(this).data('readonly', false).css('cursor', 'pointer').removeAttr('title');
415
-
416
- this.score.attr('readonly', 'readonly');
417
- }
418
- };
419
-
420
- $.fn.raty = function(method) {
421
- if (methods[method]) {
422
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
423
- } else if (typeof method === 'object' || !method) {
424
- return methods.init.apply(this, arguments);
425
- } else {
426
- $.error('Method ' + method + ' does not exist!');
427
- }
428
- };
429
-
430
- $.fn.raty.defaults = {
431
- cancel : false,
432
- cancelHint : 'cancel this rating!',
433
- cancelOff : 'cancel-off.png',
434
- cancelOn : 'cancel-on.png',
435
- cancelPlace : 'left',
436
- click : undefined,
437
- half : false,
438
- halfShow : true,
439
- hints : ['bad', 'poor', 'regular', 'good', 'gorgeous'],
440
- iconRange : undefined,
441
- mouseover : undefined,
442
- noRatedMsg : 'not rated yet',
443
- number : 5,
444
- path : 'img/',
445
- precision : false,
446
- round : { down: .25, full: .6, up: .76 },
447
- readOnly : false,
448
- score : undefined,
449
- scoreName : 'score',
450
- single : false,
451
- size : 16,
452
- space : true,
453
- starHalf : 'star-half.png',
454
- starOff : 'star-off.png',
455
- starOn : 'star-on.png',
456
- target : undefined,
457
- targetFormat : '{score}',
458
- targetKeep : false,
459
- targetText : '',
460
- targetType : 'hint',
461
- width : undefined
462
- };
463
-
464
- })(jQuery);
76
+ methods._fill.call(this, score);
77
+
78
+ if (score) {
79
+ methods._roundStars.call(this, score);
80
+ }
81
+ }, _between: function(value, min, max) {
82
+ return Math.min(Math.max(parseFloat(value), min), max);
83
+ }, _binds: function() {
84
+ if (this.cancel) {
85
+ methods._bindCancel.call(this);
86
+ }
87
+
88
+ methods._bindClick.call(this);
89
+ methods._bindOut.call(this);
90
+ methods._bindOver.call(this);
91
+ }, _bindCancel: function() {
92
+ methods._bindClickCancel.call(this);
93
+ methods._bindOutCancel.call(this);
94
+ methods._bindOverCancel.call(this);
95
+ }, _bindClick: function() {
96
+ var self = this,
97
+ that = $(self);
98
+
99
+ self.stars.on('click.raty', function(evt) {
100
+ self.score.val((self.opt.half || self.opt.precision) ? that.data('score') : this.alt);
101
+
102
+ if (self.opt.click) {
103
+ self.opt.click.call(self, parseFloat(self.score.val()), evt);
104
+ }
105
+ });
106
+ }, _bindClickCancel: function() {
107
+ var self = this;
108
+
109
+ self.cancel.on('click.raty', function(evt) {
110
+ self.score.removeAttr('value');
111
+
112
+ if (self.opt.click) {
113
+ self.opt.click.call(self, null, evt);
114
+ }
115
+ });
116
+ }, _bindOut: function() {
117
+ var self = this;
118
+
119
+ $(this).on('mouseleave.raty', function(evt) {
120
+ var score = parseFloat(self.score.val()) || undefined;
121
+
122
+ methods._apply.call(self, score);
123
+ methods._target.call(self, score, evt);
124
+
125
+ if (self.opt.mouseout) {
126
+ self.opt.mouseout.call(self, score, evt);
127
+ }
128
+ });
129
+ }, _bindOutCancel: function() {
130
+ var self = this;
131
+
132
+ self.cancel.on('mouseleave.raty', function(evt) {
133
+ $(this).attr('src', self.opt.path + self.opt.cancelOff);
134
+
135
+ if (self.opt.mouseout) {
136
+ self.opt.mouseout.call(self, self.score.val() || null, evt);
137
+ }
138
+ });
139
+ }, _bindOverCancel: function() {
140
+ var self = this;
141
+
142
+ self.cancel.on('mouseover.raty', function(evt) {
143
+ $(this).attr('src', self.opt.path + self.opt.cancelOn);
144
+
145
+ self.stars.attr('src', self.opt.path + self.opt.starOff);
146
+
147
+ methods._target.call(self, null, evt);
148
+
149
+ if (self.opt.mouseover) {
150
+ self.opt.mouseover.call(self, null);
151
+ }
152
+ });
153
+ }, _bindOver: function() {
154
+ var self = this,
155
+ that = $(self),
156
+ action = self.opt.half ? 'mousemove.raty' : 'mouseover.raty';
157
+
158
+ self.stars.on(action, function(evt) {
159
+ var score = parseInt(this.alt, 10);
160
+
161
+ if (self.opt.half) {
162
+ var position = parseFloat((evt.pageX - $(this).offset().left) / self.opt.size),
163
+ plus = (position > .5) ? 1 : .5;
164
+
165
+ score = score - 1 + plus;
166
+
167
+ methods._fill.call(self, score);
168
+
169
+ if (self.opt.precision) {
170
+ score = score - plus + position;
171
+ }
172
+
173
+ methods._roundStars.call(self, score);
174
+
175
+ that.data('score', score);
176
+ } else {
177
+ methods._fill.call(self, score);
178
+ }
179
+
180
+ methods._target.call(self, score, evt);
181
+
182
+ if (self.opt.mouseover) {
183
+ self.opt.mouseover.call(self, score, evt);
184
+ }
185
+ });
186
+ }, _callback: function(options) {
187
+ for (var i in options) {
188
+ if (typeof this.opt[options[i]] === 'function') {
189
+ this.opt[options[i]] = this.opt[options[i]].call(this);
190
+ }
191
+ }
192
+ }, _createCancel: function() {
193
+ var that = $(this),
194
+ icon = this.opt.path + this.opt.cancelOff,
195
+ cancel = $('<img />', { src: icon, alt: 'x', title: this.opt.cancelHint, 'class': 'raty-cancel' });
196
+
197
+ if (this.opt.cancelPlace == 'left') {
198
+ that.prepend('&#160;').prepend(cancel);
199
+ } else {
200
+ that.append('&#160;').append(cancel);
201
+ }
202
+
203
+ return cancel;
204
+ }, _createScore: function() {
205
+ return $('<input />', { type: 'hidden', name: this.opt.scoreName }).appendTo(this);
206
+ }, _createStars: function() {
207
+ var that = $(this);
208
+
209
+ for (var i = 1; i <= this.opt.number; i++) {
210
+ var title = methods._getHint.call(this, i),
211
+ icon = (this.opt.score && this.opt.score >= i) ? 'starOn' : 'starOff';
212
+
213
+ icon = this.opt.path + this.opt[icon];
214
+
215
+ $('<img />', { src : icon, alt: i, title: title }).appendTo(this);
216
+
217
+ if (this.opt.space) {
218
+ that.append((i < this.opt.number) ? '&#160;' : '');
219
+ }
220
+ }
221
+
222
+ return that.children('img');
223
+ }, _error: function(message) {
224
+ $(this).html(message);
225
+
226
+ $.error(message);
227
+ }, _fill: function(score) {
228
+ var self = this,
229
+ hash = 0;
230
+
231
+ for (var i = 1; i <= self.stars.length; i++) {
232
+ var star = self.stars.eq(i - 1),
233
+ select = self.opt.single ? (i == score) : (i <= score);
234
+
235
+ if (self.opt.iconRange && self.opt.iconRange.length > hash) {
236
+ var irange = self.opt.iconRange[hash],
237
+ on = irange.on || self.opt.starOn,
238
+ off = irange.off || self.opt.starOff,
239
+ icon = select ? on : off;
240
+
241
+ if (i <= irange.range) {
242
+ star.attr('src', self.opt.path + icon);
243
+ }
244
+
245
+ if (i == irange.range) {
246
+ hash++;
247
+ }
248
+ } else {
249
+ var icon = select ? 'starOn' : 'starOff';
250
+
251
+ star.attr('src', this.opt.path + this.opt[icon]);
252
+ }
253
+ }
254
+ }, _getHint: function(score) {
255
+ var hint = this.opt.hints[score - 1];
256
+ return (hint === '') ? '' : (hint || score);
257
+ }, _lock: function() {
258
+ var score = parseInt(this.score.val(), 10), // TODO: 3.1 >> [['1'], ['2'], ['3', '.1', '.2']]
259
+ hint = score ? methods._getHint.call(this, score) : this.opt.noRatedMsg;
260
+
261
+ $(this).data('readonly', true).css('cursor', '').attr('title', hint);
262
+
263
+ this.score.attr('readonly', 'readonly');
264
+ this.stars.attr('title', hint);
265
+
266
+ if (this.cancel) {
267
+ this.cancel.hide();
268
+ }
269
+ }, _roundStars: function(score) {
270
+ var rest = (score - Math.floor(score)).toFixed(2);
271
+
272
+ if (rest > this.opt.round.down) {
273
+ var icon = 'starOn'; // Up: [x.76 .. x.99]
274
+
275
+ if (this.opt.halfShow && rest < this.opt.round.up) { // Half: [x.26 .. x.75]
276
+ icon = 'starHalf';
277
+ } else if (rest < this.opt.round.full) { // Down: [x.00 .. x.5]
278
+ icon = 'starOff';
279
+ }
280
+
281
+ this.stars.eq(Math.ceil(score) - 1).attr('src', this.opt.path + this.opt[icon]);
282
+ } // Full down: [x.00 .. x.25]
283
+ }, _target: function(score, evt) {
284
+ if (this.opt.target) {
285
+ var target = $(this.opt.target);
286
+
287
+ if (target.length === 0) {
288
+ methods._error.call(this, 'Target selector invalid or missing!');
289
+ }
290
+
291
+ if (this.opt.targetFormat.indexOf('{score}') < 0) {
292
+ methods._error.call(this, 'Template "{score}" missing!');
293
+ }
294
+
295
+ var mouseover = evt && evt.type == 'mouseover';
296
+
297
+ if (score === undefined) {
298
+ score = this.opt.targetText;
299
+ } else if (score === null) {
300
+ score = mouseover ? this.opt.cancelHint : this.opt.targetText;
301
+ } else {
302
+ if (this.opt.targetType == 'hint') {
303
+ score = methods._getHint.call(this, Math.ceil(score));
304
+ } else if (this.opt.precision) {
305
+ score = parseFloat(score).toFixed(1);
306
+ }
307
+
308
+ if (!mouseover && !this.opt.targetKeep) {
309
+ score = this.opt.targetText;
310
+ }
311
+ }
312
+
313
+ if (score) {
314
+ score = this.opt.targetFormat.toString().replace('{score}', score);
315
+ }
316
+
317
+ if (target.is(':input')) {
318
+ target.val(score);
319
+ } else {
320
+ target.html(score);
321
+ }
322
+ }
323
+ }, _unlock: function() {
324
+ $(this).data('readonly', false).css('cursor', 'pointer').removeAttr('title');
325
+
326
+ this.score.removeAttr('readonly', 'readonly');
327
+
328
+ for (var i = 0; i < this.opt.number; i++) {
329
+ this.stars.eq(i).attr('title', methods._getHint.call(this, i + 1));
330
+ }
331
+
332
+ if (this.cancel) {
333
+ this.cancel.css('display', '');
334
+ }
335
+ }, cancel: function(click) {
336
+ return this.each(function() {
337
+ if ($(this).data('readonly') !== true) {
338
+ methods[click ? 'click' : 'score'].call(this, null);
339
+ this.score.removeAttr('value');
340
+ }
341
+ });
342
+ }, click: function(score) {
343
+ return $(this).each(function() {
344
+ if ($(this).data('readonly') !== true) {
345
+ methods._apply.call(this, score);
346
+
347
+ if (!this.opt.click) {
348
+ methods._error.call(this, 'You must add the "click: function(score, evt) { }" callback.');
349
+ }
350
+
351
+
352
+ this.opt.click.call(this, score, $.Event('click'));
353
+
354
+ methods._target.call(this, score);
355
+ }
356
+ });
357
+ }, destroy: function() {
358
+ return $(this).each(function() {
359
+ var that = $(this),
360
+ raw = that.data('raw');
361
+
362
+ if (raw) {
363
+ that.off('.raty').empty().css({ cursor: raw.style.cursor, width: raw.style.width }).removeData('readonly');
364
+ } else {
365
+ that.data('raw', that.clone()[0]);
366
+ }
367
+ });
368
+ }, getScore: function() {
369
+ var score = [],
370
+ value ;
371
+
372
+ $(this).each(function() {
373
+ value = this.score.val();
374
+
375
+ score.push(value ? parseFloat(value) : undefined);
376
+ });
377
+
378
+ return (score.length > 1) ? score : score[0];
379
+ }, readOnly: function(readonly) {
380
+ return this.each(function() {
381
+ var that = $(this);
382
+
383
+ if (that.data('readonly') !== readonly) {
384
+ if (readonly) {
385
+ that.off('.raty').children('img').off('.raty');
386
+
387
+ methods._lock.call(this);
388
+ } else {
389
+ methods._binds.call(this);
390
+ methods._unlock.call(this);
391
+ }
392
+
393
+ that.data('readonly', readonly);
394
+ }
395
+ });
396
+ }, reload: function() {
397
+ return methods.set.call(this, {});
398
+ }, score: function() {
399
+ return arguments.length ? methods.setScore.apply(this, arguments) : methods.getScore.call(this);
400
+ }, set: function(settings) {
401
+ return this.each(function() {
402
+ var that = $(this),
403
+ actual = that.data('settings'),
404
+ news = $.extend({}, actual, settings);
405
+
406
+ that.raty(news);
407
+ });
408
+ }, setScore: function(score) {
409
+ return $(this).each(function() {
410
+ if ($(this).data('readonly') !== true) {
411
+ methods._apply.call(this, score);
412
+ methods._target.call(this, score);
413
+ }
414
+ });
415
+ }
416
+ };
417
+
418
+ $.fn.raty = function(method) {
419
+ if (methods[method]) {
420
+ return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
421
+ } else if (typeof method === 'object' || !method) {
422
+ return methods.init.apply(this, arguments);
423
+ } else {
424
+ $.error('Method ' + method + ' does not exist!');
425
+ }
426
+ };
427
+
428
+ $.fn.raty.defaults = {
429
+ cancel : false,
430
+ cancelHint : 'Cancel this rating!',
431
+ cancelOff : 'cancel-off.png',
432
+ cancelOn : 'cancel-on.png',
433
+ cancelPlace : 'left',
434
+ click : undefined,
435
+ half : false,
436
+ halfShow : true,
437
+ hints : ['bad', 'poor', 'regular', 'good', 'gorgeous'],
438
+ iconRange : undefined,
439
+ mouseout : undefined,
440
+ mouseover : undefined,
441
+ noRatedMsg : 'Not rated yet!',
442
+ number : 5,
443
+ numberMax : 20,
444
+ path : '',
445
+ precision : false,
446
+ readOnly : false,
447
+ round : { down: .25, full: .6, up: .76 },
448
+ score : undefined,
449
+ scoreName : 'score',
450
+ single : false,
451
+ size : 16,
452
+ space : true,
453
+ starHalf : 'star-half.png',
454
+ starOff : 'star-off.png',
455
+ starOn : 'star-on.png',
456
+ target : undefined,
457
+ targetFormat : '{score}',
458
+ targetKeep : false,
459
+ targetText : '',
460
+ targetType : 'hint',
461
+ width : undefined
462
+ };
463
+
464
+ })(jQuery);
@@ -1,9 +1,9 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  class RaterController < ApplicationController
3
3
  def create
4
- if user_signed_in?
4
+ if <%= file_name %>_signed_in?
5
5
  obj = params[:klass].classify.constantize.find(params[:id])
6
- obj.rate params[:score].to_i, current_user
6
+ obj.rate params[:score].to_i, current_<%= file_name %>
7
7
 
8
8
  render :json => true
9
9
  else
@@ -8,7 +8,7 @@ module Helpers
8
8
 
9
9
  star = options[:star] || 5
10
10
 
11
- disable_after_rate = options[:disable_after_rate] || true
11
+ disable_after_rate = options[:disable_after_rate].nil? ? true : options[:disable_after_rate]
12
12
 
13
13
  readonly = !(current_user && rateable_obj.can_rate?(current_user))
14
14
 
@@ -25,7 +25,7 @@ module Helpers
25
25
  @rating = Rate.find_by_rater_id_and_rateable_id(@user.id, @object.id)
26
26
  stars = @rating ? @rating.stars : 0
27
27
 
28
- disable_after_rate = options[:disable_after_rate] || false
28
+ disable_after_rate = options[:disable_after_rate].nil? ? true : options[:disable_after_rate]
29
29
 
30
30
  readonly=false
31
31
  if disable_after_rate
@@ -73,7 +73,7 @@ module Mongoid
73
73
 
74
74
  def letsrate_rateable
75
75
  has_many :rates, :as => :rateable, :class_name => 'Rate', :dependent => :destroy
76
- has_many :raters
76
+ has_many :raters, class_name: 'User'
77
77
 
78
78
  has_one :average, :as => :cacheable,
79
79
  :class_name => 'RatingCache', :dependent => :destroy
@@ -1,6 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Mongoid
3
3
  module Letsrate
4
- VERSION = '1.0.10'
4
+ VERSION = '1.0.11'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-letsrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Johann