mongoid-letsrate 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -4
- data/Gemfile.lock +1 -1
- data/README.md +15 -14
- data/lib/generators/{letsrate → mongoid_letsrate}/USAGE +0 -0
- data/lib/generators/{letsrate → mongoid_letsrate}/mongoid_letsrate_generator.rb +2 -1
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/cache_model.rb +4 -4
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/jquery.raty.js +0 -0
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/letsrate.js.erb +0 -1
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/model.rb +3 -3
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/rater_controller.rb +2 -1
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/star-half.png +0 -0
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/star-off.png +0 -0
- data/lib/generators/{letsrate → mongoid_letsrate}/templates/star-on.png +0 -0
- data/lib/{mongoid_letsrate → mongoid/letsrate}/helpers.rb +10 -10
- data/lib/mongoid/letsrate/model.rb +85 -0
- data/lib/mongoid/letsrate/version.rb +6 -0
- data/lib/mongoid-letsrate.rb +4 -0
- data/mongoid-letsrate.gemspec +5 -3
- metadata +15 -16
- data/lib/generators/letsrate/templates/migration.rb +0 -20
- data/lib/mongoid_letsrate/model.rb +0 -109
- data/lib/mongoid_letsrate/version.rb +0 -3
- data/lib/mongoid_letsrate.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b1076405c5d69f606e636b0dd7c6f1ce2626d5e
|
4
|
+
data.tar.gz: 5b4df317255c7239227bd9c099d4afe03b3f3f04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfbd6c745062b113b8955512648a87a9555c59496a89dea85256ccf9137ed3dff620bdcbbc3b5cdad62248a49881ce22c1a9b78bd43698839139912a2b6a507e
|
7
|
+
data.tar.gz: f0a05ec10dbe82b4c1203726c5d5ea30083e40bd714ee72dd970577fbc0e5b69a25dfff7ca4f4f57a43624528db96f56f3e35c2a9fa29dceff713593f7582b1b
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Provides the best way to add rating capabilites to your Rails application with j
|
|
6
6
|
|
7
7
|
[](http://travis-ci.org/malagant/mongoid-letsrate)
|
8
8
|
[](https://gemnasium.com/malagant/mongoid-letsrate)
|
9
|
-
[](https://codeclimate.com/github/malagant/mongoid-letsrate)
|
10
10
|
|
11
11
|
Find it at [github.com/malagant/mongoid-letsrate](https://github.com/malagant/mongoid-letsrate)
|
12
12
|
|
@@ -50,19 +50,24 @@ I suppose you have a car model
|
|
50
50
|
rails g model car name:string
|
51
51
|
```
|
52
52
|
|
53
|
-
You should add the
|
53
|
+
You should add the letsrate_rateable function.
|
54
54
|
|
55
55
|
```ruby
|
56
|
-
class Car
|
57
|
-
|
56
|
+
class Car
|
57
|
+
include Mongoid::Document
|
58
|
+
include Mongoid::Timestamps
|
59
|
+
|
60
|
+
letsrate_rateable
|
58
61
|
end
|
59
62
|
```
|
60
63
|
|
61
|
-
Then you need to add a call
|
64
|
+
Then you need to add a call letsrate_rater in the user model.
|
62
65
|
|
63
66
|
```ruby
|
64
|
-
class User
|
65
|
-
|
67
|
+
class User
|
68
|
+
include Mongoid::Document
|
69
|
+
|
70
|
+
letsrate_rater
|
66
71
|
end
|
67
72
|
```
|
68
73
|
|
@@ -74,23 +79,19 @@ new rating value from authenticated user.
|
|
74
79
|
```erb
|
75
80
|
<%# show.html.erb -> /cars/1 %>
|
76
81
|
|
77
|
-
|
78
|
-
Engine : <%= rating_for @car, "engine" %>
|
79
|
-
Price : <%= rating_for @car, "price" %>
|
82
|
+
Rating : <%= rating_for @car %>
|
80
83
|
```
|
81
84
|
|
82
85
|
If you need to change the star number, you should use star option like below.
|
83
86
|
|
84
87
|
```erb
|
85
|
-
|
86
|
-
Speed : <%= rating_for @car, "engine", :star => 7 %>
|
87
|
-
Speed : <%= rating_for @car, "price" %>
|
88
|
+
Rating : <%= rating_for @car, :star => 10 %>
|
88
89
|
```
|
89
90
|
|
90
91
|
You can use the rating_for_user helper method to show the star rating for the user.
|
91
92
|
|
92
93
|
```erb
|
93
|
-
|
94
|
+
Rating : <%= rating_for_user @car, current_user, :star => 10 %>
|
94
95
|
```
|
95
96
|
|
96
97
|
|
File without changes
|
@@ -1,10 +1,10 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
1
2
|
class RatingCache
|
2
3
|
include Mongoid::Document
|
3
4
|
include Mongoid::Timestamps
|
4
5
|
|
5
|
-
field :avg, type: Float
|
6
|
-
field :qty, type: Integer
|
7
|
-
field :dimension, type: String
|
6
|
+
field :avg, type: Float
|
7
|
+
field :qty, type: Integer
|
8
8
|
|
9
9
|
belongs_to :cacheable, :polymorphic => true
|
10
|
-
end
|
10
|
+
end
|
File without changes
|
@@ -1,10 +1,10 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
1
2
|
class Rate
|
2
3
|
include Mongoid::Document
|
3
4
|
include Mongoid::Timestamps
|
4
5
|
|
5
|
-
field :stars, type: Float
|
6
|
-
field :dimension, type: String
|
6
|
+
field :stars, type: Float
|
7
7
|
|
8
8
|
belongs_to :rater, :class_name => '<%= file_name.classify %>'
|
9
9
|
belongs_to :rateable, :polymorphic => true
|
10
|
-
end
|
10
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
1
2
|
class RaterController < ApplicationController
|
2
3
|
def create
|
3
4
|
if user_signed_in?
|
4
5
|
obj = params[:klass].classify.constantize.find(params[:id])
|
5
|
-
obj.rate params[:score].to_i, current_user
|
6
|
+
obj.rate params[:score].to_i, current_user
|
6
7
|
|
7
8
|
render :json => true
|
8
9
|
else
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,7 +1,8 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
1
2
|
module Helpers
|
2
|
-
def rating_for(rateable_obj,
|
3
|
+
def rating_for(rateable_obj, options={})
|
3
4
|
|
4
|
-
cached_average = rateable_obj.average
|
5
|
+
cached_average = rateable_obj.average
|
5
6
|
|
6
7
|
avg = cached_average ? cached_average.avg : 0
|
7
8
|
|
@@ -9,35 +10,34 @@ module Helpers
|
|
9
10
|
|
10
11
|
disable_after_rate = options[:disable_after_rate] || true
|
11
12
|
|
12
|
-
readonly = !(current_user && rateable_obj.can_rate?(current_user
|
13
|
+
readonly = !(current_user && rateable_obj.can_rate?(current_user))
|
13
14
|
|
14
|
-
content_tag :div, '',
|
15
|
+
content_tag :div, '', :class => "star", "data-rating" => avg,
|
15
16
|
"data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
|
16
17
|
"data-disable-after-rate" => disable_after_rate,
|
17
18
|
"data-readonly" => readonly,
|
18
19
|
"data-star-count" => star
|
19
20
|
end
|
20
21
|
|
21
|
-
def rating_for_user(rateable_obj, rating_user,
|
22
|
+
def rating_for_user(rateable_obj, rating_user, options = {})
|
22
23
|
@object = rateable_obj
|
23
24
|
@user = rating_user
|
24
|
-
|
25
|
-
|
25
|
+
@rating = Rate.find_by_rater_id_and_rateable_id(@user.id, @object.id)
|
26
|
+
stars = @rating ? @rating.stars : 0
|
26
27
|
|
27
28
|
disable_after_rate = options[:disable_after_rate] || false
|
28
29
|
|
29
30
|
readonly=false
|
30
31
|
if disable_after_rate
|
31
|
-
readonly = current_user.present? ? !rateable_obj.can_rate?(current_user.id
|
32
|
+
readonly = current_user.present? ? !rateable_obj.can_rate?(current_user.id) : true
|
32
33
|
end
|
33
34
|
|
34
|
-
content_tag :div, '',
|
35
|
+
content_tag :div, '', :class => "star", "data-rating" => stars,
|
35
36
|
"data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
|
36
37
|
"data-disable-after-rate" => disable_after_rate,
|
37
38
|
"data-readonly" => readonly,
|
38
39
|
"data-star-count" => stars
|
39
40
|
end
|
40
|
-
|
41
41
|
end
|
42
42
|
|
43
43
|
class ActionView::Base
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
module Letsrate
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def rate(stars, user, dirichlet_method=false)
|
9
|
+
if can_rate? user
|
10
|
+
rates.create! do |r|
|
11
|
+
r.stars = stars
|
12
|
+
r.rater = user
|
13
|
+
end
|
14
|
+
if dirichlet_method
|
15
|
+
update_rate_average_dirichlet(stars)
|
16
|
+
else
|
17
|
+
update_rate_average(stars)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
raise 'User has already rated.'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_rate_average_dirichlet(stars)
|
25
|
+
## assumes 5 possible vote categories
|
26
|
+
dp = {1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1}
|
27
|
+
stars_group = Hash[rates.group(:stars).count.map { |k, v| [k.to_i, v] }]
|
28
|
+
posterior = dp.merge(stars_group) { |key, a, b| a + b }
|
29
|
+
sum = posterior.map { |i, v| v }.inject { |a, b| a + b }
|
30
|
+
davg = posterior.map { |i, v| i * v }.inject { |a, b| a + b }.to_f / sum
|
31
|
+
|
32
|
+
if average.nil?
|
33
|
+
RatingCache.create! do |avg|
|
34
|
+
avg.cacheable_id = self.id
|
35
|
+
avg.cacheable_type = self.class.name
|
36
|
+
avg.qty = 1
|
37
|
+
avg.avg = davg
|
38
|
+
end
|
39
|
+
else
|
40
|
+
a = average
|
41
|
+
a.qty = rates.count
|
42
|
+
a.avg = davg
|
43
|
+
a.save!(validate: false)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def update_rate_average(stars)
|
48
|
+
if average.nil?
|
49
|
+
RatingCache.create! do |avg|
|
50
|
+
avg.cacheable_id = self.id
|
51
|
+
avg.cacheable_type = self.class.name
|
52
|
+
avg.avg = stars
|
53
|
+
avg.qty = 1
|
54
|
+
end
|
55
|
+
else
|
56
|
+
a = average
|
57
|
+
a.qty = rates.count
|
58
|
+
a.avg = rates.average(:stars)
|
59
|
+
a.save!(validate: false)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def can_rate?(user)
|
64
|
+
user.ratings_given.where(rateable_id: id, rateable_type: self.class.name).size.zero?
|
65
|
+
end
|
66
|
+
|
67
|
+
module ClassMethods
|
68
|
+
|
69
|
+
def letsrate_rater
|
70
|
+
has_many :ratings_given, :class_name => 'Rate'
|
71
|
+
belongs_to :rater
|
72
|
+
end
|
73
|
+
|
74
|
+
def letsrate_rateable
|
75
|
+
has_many :rates, :as => :rateable, :class_name => 'Rate', :dependent => :destroy
|
76
|
+
has_many :raters
|
77
|
+
|
78
|
+
has_one :average, :as => :cacheable,
|
79
|
+
:class_name => 'RatingCache', :dependent => :destroy
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
ActiveSupport.run_load_hooks(:mongoid_letsrate, Mongoid::Letsrate)
|
data/mongoid-letsrate.gemspec
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'mongoid/letsrate/version'
|
4
6
|
|
5
7
|
Gem::Specification.new do |s|
|
6
8
|
s.name = 'mongoid-letsrate'
|
7
|
-
s.version =
|
9
|
+
s.version = Mongoid::Letsrate::VERSION
|
8
10
|
s.authors = ['Michael Johann','Murat GUZEL']
|
9
11
|
s.email = ['mjohann@rails-experts.com']
|
10
12
|
s.homepage = 'http://github.com/malagant/mongoid-letsrate'
|
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.
|
4
|
+
version: 1.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Johann
|
@@ -24,21 +24,20 @@ files:
|
|
24
24
|
- Gemfile.lock
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
|
-
- lib/generators/
|
28
|
-
- lib/generators/
|
29
|
-
- lib/generators/
|
30
|
-
- lib/generators/
|
31
|
-
- lib/generators/
|
32
|
-
- lib/generators/
|
33
|
-
- lib/generators/
|
34
|
-
- lib/generators/
|
35
|
-
- lib/generators/
|
36
|
-
- lib/generators/
|
37
|
-
- lib/
|
38
|
-
- lib/
|
39
|
-
- lib/
|
40
|
-
- lib/
|
41
|
-
- lib/mongoid_letsrate/version.rb
|
27
|
+
- lib/generators/mongoid_letsrate/USAGE
|
28
|
+
- lib/generators/mongoid_letsrate/mongoid_letsrate_generator.rb
|
29
|
+
- lib/generators/mongoid_letsrate/templates/cache_model.rb
|
30
|
+
- lib/generators/mongoid_letsrate/templates/jquery.raty.js
|
31
|
+
- lib/generators/mongoid_letsrate/templates/letsrate.js.erb
|
32
|
+
- lib/generators/mongoid_letsrate/templates/model.rb
|
33
|
+
- lib/generators/mongoid_letsrate/templates/rater_controller.rb
|
34
|
+
- lib/generators/mongoid_letsrate/templates/star-half.png
|
35
|
+
- lib/generators/mongoid_letsrate/templates/star-off.png
|
36
|
+
- lib/generators/mongoid_letsrate/templates/star-on.png
|
37
|
+
- lib/mongoid-letsrate.rb
|
38
|
+
- lib/mongoid/letsrate/helpers.rb
|
39
|
+
- lib/mongoid/letsrate/model.rb
|
40
|
+
- lib/mongoid/letsrate/version.rb
|
42
41
|
- mongoid-letsrate.gemspec
|
43
42
|
homepage: http://github.com/malagant/mongoid-letsrate
|
44
43
|
licenses: []
|
@@ -1,20 +0,0 @@
|
|
1
|
-
class CreateRates < ActiveRecord::Migration
|
2
|
-
|
3
|
-
def self.up
|
4
|
-
create_table :rates do |t|
|
5
|
-
t.belongs_to :rater
|
6
|
-
t.belongs_to :rateable, :polymorphic => true
|
7
|
-
t.float :stars, :null => false
|
8
|
-
t.string :dimension
|
9
|
-
t.timestamps
|
10
|
-
end
|
11
|
-
|
12
|
-
add_index :rates, :rater_id
|
13
|
-
add_index :rates, [:rateable_id, :rateable_type]
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.down
|
17
|
-
drop_table :rates
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,109 +0,0 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
module MongoidLetsrate
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
def rate(stars, user, dimension=nil, dirichlet_method=false)
|
6
|
-
dimension = nil if dimension.blank?
|
7
|
-
|
8
|
-
if can_rate? user, dimension
|
9
|
-
rates(dimension).create! do |r|
|
10
|
-
r.stars = stars
|
11
|
-
r.rater = user
|
12
|
-
end
|
13
|
-
if dirichlet_method
|
14
|
-
update_rate_average_dirichlet(stars, dimension)
|
15
|
-
else
|
16
|
-
update_rate_average(stars, dimension)
|
17
|
-
end
|
18
|
-
else
|
19
|
-
raise 'User has already rated.'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def update_rate_average_dirichlet(stars, dimension=nil)
|
24
|
-
## assumes 5 possible vote categories
|
25
|
-
dp = {1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1}
|
26
|
-
stars_group = Hash[rates(dimension).group(:stars).count.map{|k,v| [k.to_i,v] }]
|
27
|
-
posterior = dp.merge(stars_group){|key, a, b| a + b}
|
28
|
-
sum = posterior.map{ |i, v| v }.inject { |a, b| a + b }
|
29
|
-
davg = posterior.map{ |i, v| i * v }.inject { |a, b| a + b }.to_f / sum
|
30
|
-
|
31
|
-
if average(dimension).nil?
|
32
|
-
RatingCache.create! do |avg|
|
33
|
-
avg.cacheable_id = self.id
|
34
|
-
avg.cacheable_type = self.class.name
|
35
|
-
avg.qty = 1
|
36
|
-
avg.avg = davg
|
37
|
-
avg.dimension = dimension
|
38
|
-
end
|
39
|
-
else
|
40
|
-
a = average(dimension)
|
41
|
-
a.qty = rates(dimension).count
|
42
|
-
a.avg = davg
|
43
|
-
a.save!(validate: false)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def update_rate_average(stars, dimension=nil)
|
48
|
-
if average(dimension).nil?
|
49
|
-
RatingCache.create! do |avg|
|
50
|
-
avg.cacheable_id = self.id
|
51
|
-
avg.cacheable_type = self.class.name
|
52
|
-
avg.avg = stars
|
53
|
-
avg.qty = 1
|
54
|
-
avg.dimension = dimension
|
55
|
-
end
|
56
|
-
else
|
57
|
-
a = average(dimension)
|
58
|
-
a.qty = rates(dimension).count
|
59
|
-
a.avg = rates(dimension).average(:stars)
|
60
|
-
a.save!(validate: false)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def average(dimension=nil)
|
65
|
-
dimension ? self.send("#{dimension}_average") : rate_average_without_dimension
|
66
|
-
end
|
67
|
-
|
68
|
-
def can_rate?(user, dimension=nil)
|
69
|
-
user.ratings_given.where(dimension: dimension, rateable_id: id, rateable_type: self.class.name).size.zero?
|
70
|
-
end
|
71
|
-
|
72
|
-
def rates(dimension=nil)
|
73
|
-
dimension ? self.send("#{dimension}_rates") : rates_without_dimension
|
74
|
-
end
|
75
|
-
|
76
|
-
def raters(dimension=nil)
|
77
|
-
dimension ? self.send("#{dimension}_raters") : raters_without_dimension
|
78
|
-
end
|
79
|
-
|
80
|
-
module ClassMethods
|
81
|
-
|
82
|
-
def letsrate_rater
|
83
|
-
has_many :ratings_given, :class_name => "Rate", :foreign_key => :rater_id
|
84
|
-
end
|
85
|
-
|
86
|
-
def letsrate_rateable(*dimensions)
|
87
|
-
has_many :rates_without_dimension, -> { where dimension: nil}, :as => :rateable, :class_name => "Rate", :dependent => :destroy
|
88
|
-
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
|
89
|
-
|
90
|
-
has_one :rate_average_without_dimension, -> { where dimension: nil}, :as => :cacheable,
|
91
|
-
:class_name => "RatingCache", :dependent => :destroy
|
92
|
-
|
93
|
-
|
94
|
-
dimensions.each do |dimension|
|
95
|
-
has_many "#{dimension}_rates".to_sym, -> {where dimension: dimension.to_s},
|
96
|
-
:dependent => :destroy,
|
97
|
-
:class_name => "Rate",
|
98
|
-
:as => :rateable
|
99
|
-
|
100
|
-
has_many "#{dimension}_raters".to_sym, :through => "#{dimension}_rates", :source => :rater
|
101
|
-
|
102
|
-
has_one "#{dimension}_average".to_sym, -> { where dimension: dimension.to_s },
|
103
|
-
:as => :cacheable, :class_name => "RatingCache",
|
104
|
-
:dependent => :destroy
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|