acts_rateable 0.0.5 → 0.0.6
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.
@@ -8,10 +8,10 @@ module ActsRateable
|
|
8
8
|
|
9
9
|
def acts_rateable(options = {})
|
10
10
|
|
11
|
-
has_many :rates, class_name: ActsRateable::Rate, foreign_key: :resource_id, conditions: { resource_type: self.name }, dependent: :destroy
|
12
|
-
has_many :rated, class_name: ActsRateable::Rate, foreign_key: :author_id, conditions: { author_type: self.name }, dependent: :destroy
|
13
|
-
has_one :rating, class_name: ActsRateable::Rating, foreign_key: :resource_id, conditions: { resource_type: self.name }, dependent: :destroy
|
14
|
-
has_one :count, class_name: ActsRateable::Count, foreign_key: :resource_id, conditions: { resource_type: self.name }, dependent: :destroy
|
11
|
+
has_many :rates, class_name: ActsRateable::Rate, foreign_key: :resource_id, conditions: { resource_type: self.base_class.name }, dependent: :destroy
|
12
|
+
has_many :rated, class_name: ActsRateable::Rate, foreign_key: :author_id, conditions: { author_type: self.base_class.name }, dependent: :destroy
|
13
|
+
has_one :rating, class_name: ActsRateable::Rating, foreign_key: :resource_id, conditions: { resource_type: self.base_class.name }, dependent: :destroy
|
14
|
+
has_one :count, class_name: ActsRateable::Count, foreign_key: :resource_id, conditions: { resource_type: self.base_class.name }, dependent: :destroy
|
15
15
|
|
16
16
|
scope :order_by_rating, lambda { | column='estimate', direction="DESC" |
|
17
17
|
includes(:rating).group('ar_ratings.id').order("ar_ratings.#{column.downcase} #{direction.upcase}")
|
@@ -22,8 +22,8 @@ module ActsRateable
|
|
22
22
|
}
|
23
23
|
|
24
24
|
after_save do
|
25
|
-
ActsRateable::Rating.where({resource_id: self.id, resource_type: self.class.name}).first_or_initialize.save #if !rates.empty?
|
26
|
-
ActsRateable::Count.where({resource_id: self.id, resource_type: self.class.name}).first_or_initialize.save #if !rates.empty?
|
25
|
+
ActsRateable::Rating.where({resource_id: self.id, resource_type: self.class.base_class.name}).first_or_initialize.save #if !rates.empty?
|
26
|
+
ActsRateable::Count.where({resource_id: self.id, resource_type: self.class.base_class.name}).first_or_initialize.save #if !rates.empty?
|
27
27
|
end
|
28
28
|
|
29
29
|
include LocalInstanceMethods
|
data/lib/acts_rateable/count.rb
CHANGED
@@ -15,21 +15,21 @@ module ActsRateable
|
|
15
15
|
|
16
16
|
def self.set_totals(author)
|
17
17
|
sql = "SELECT COUNT(*) total_ratings, SUM(value) rating_sum, AVG(value) rating_avg, "+
|
18
|
-
"(SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.name}') rated_count, "+
|
19
|
-
"((SELECT COUNT(*) from ar_rates WHERE author_type = '#{author.class.name}') / (SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.name}')) avg_num_ratings "+
|
20
|
-
"FROM ar_rates WHERE author_type = '#{author.class.name}'"
|
21
|
-
@@global_counts[author.class.name] = ActsRateable::Rate.connection.execute(sql).first
|
18
|
+
"(SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.base_class.name}') rated_count, "+
|
19
|
+
"((SELECT COUNT(*) from ar_rates WHERE author_type = '#{author.class.base_class.name}') / (SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.base_class.name}')) avg_num_ratings "+
|
20
|
+
"FROM ar_rates WHERE author_type = '#{author.class.base_class.name}'"
|
21
|
+
@@global_counts[author.class.base_class.name] = ActsRateable::Rate.connection.execute(sql).first
|
22
22
|
end
|
23
23
|
|
24
24
|
# RETURNS = { "total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>"", "rated_count"=>"", "avg_num_ratings"=>"" }
|
25
25
|
def self.get_totals(author)
|
26
|
-
@@global_counts[author.class.name] ||= set_totals(author)
|
26
|
+
@@global_counts[author.class.base_class.name] ||= set_totals(author)
|
27
27
|
end
|
28
28
|
|
29
29
|
# RETURNS = {"total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>""}
|
30
30
|
def self.values_for(author)
|
31
31
|
sql = "SELECT COUNT(*) total_ratings, COALESCE(SUM(value),0) rating_sum, COALESCE(AVG(value),0) rating_avg "+
|
32
|
-
"FROM ar_rates WHERE author_type = '#{author.class.name}' and author_id = '#{author.id}'"
|
32
|
+
"FROM ar_rates WHERE author_type = '#{author.class.base_class.name}' and author_id = '#{author.id}'"
|
33
33
|
ActsRateable::Rate.connection.execute(sql).first
|
34
34
|
end
|
35
35
|
|
data/lib/acts_rateable/rate.rb
CHANGED
@@ -17,8 +17,8 @@ module ActsRateable
|
|
17
17
|
|
18
18
|
def self.rated?(resource, author)
|
19
19
|
rate = where({
|
20
|
-
author_type: author.class.name, author_id: author.id,
|
21
|
-
resource_type: resource.class.name, resource_id: resource.id
|
20
|
+
author_type: author.class.base_class.name, author_id: author.id,
|
21
|
+
resource_type: resource.class.base_class.name, resource_id: resource.id
|
22
22
|
})
|
23
23
|
return rate if rate
|
24
24
|
return false
|
@@ -27,8 +27,8 @@ module ActsRateable
|
|
27
27
|
def self.create(author, resource, value)
|
28
28
|
return unless author && resource && value
|
29
29
|
atts = {
|
30
|
-
resource_type: resource.class.name, resource_id: resource.id,
|
31
|
-
author_type: author.class.name, author_id: author.id,
|
30
|
+
resource_type: resource.class.base_class.name, resource_id: resource.id,
|
31
|
+
author_type: author.class.base_class.name, author_id: author.id,
|
32
32
|
value: value
|
33
33
|
}
|
34
34
|
rate = where(atts.except(:value)).first_or_initialize(atts)
|
data/lib/acts_rateable/rating.rb
CHANGED
@@ -15,21 +15,21 @@ module ActsRateable
|
|
15
15
|
|
16
16
|
def self.set_totals(resource)
|
17
17
|
sql = "SELECT COUNT(*) total_ratings, SUM(value) rating_sum, AVG(value) rating_avg, "+
|
18
|
-
"(SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.name}') rated_count, "+
|
19
|
-
"((SELECT COUNT(*) from ar_rates WHERE resource_type = '#{resource.class.name}') / (SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.name}')) avg_num_ratings "+
|
20
|
-
"FROM ar_rates WHERE resource_type = '#{resource.class.name}'"
|
21
|
-
@@global_ratings[resource.class.name] = ActsRateable::Rate.connection.execute(sql).first
|
18
|
+
"(SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}') rated_count, "+
|
19
|
+
"((SELECT COUNT(*) from ar_rates WHERE resource_type = '#{resource.class.base_class.name}') / (SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}')) avg_num_ratings "+
|
20
|
+
"FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}'"
|
21
|
+
@@global_ratings[resource.class.base_class.name] = ActsRateable::Rate.connection.execute(sql).first
|
22
22
|
end
|
23
23
|
|
24
24
|
# RETURNS = { "total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>"", "rated_count"=>"", "avg_num_ratings"=>"" }
|
25
25
|
def self.get_totals(resource)
|
26
|
-
@@global_ratings[resource.class.name] ||= set_totals(resource)
|
26
|
+
@@global_ratings[resource.class.base_class.name] ||= set_totals(resource)
|
27
27
|
end
|
28
28
|
|
29
29
|
# RETURNS = {"total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>""}
|
30
30
|
def self.values_for(resource)
|
31
31
|
sql = "SELECT COUNT(*) total_ratings, COALESCE(SUM(value),0) rating_sum, COALESCE(AVG(value),0) rating_avg "+
|
32
|
-
"FROM ar_rates WHERE resource_type = '#{resource.class.name}' and resource_id = '#{resource.id}'"
|
32
|
+
"FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}' and resource_id = '#{resource.id}'"
|
33
33
|
ActsRateable::Rate.connection.execute(sql).first
|
34
34
|
end
|
35
35
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_rateable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|