Commit b0d2e89757d703d1b2317808251e1b7faa50fd45
Committed by
Fabio Teixeira
1 parent
bca28ed5
Exists in
communities_ratings
Add method to calculate community rating's average
Signed-off-by: Fábio Teixeira <fabio1079@gmail.com> Signed-off-by: Pedro de Lyra <pedrodelyra@gmail.com>
Showing
1 changed file
with
10 additions
and
0 deletions
Show diff stats
plugins/communities_ratings/lib/community_rating.rb
@@ -14,4 +14,14 @@ class CommunityRating < ActiveRecord::Base | @@ -14,4 +14,14 @@ class CommunityRating < ActiveRecord::Base | ||
14 | 14 | ||
15 | has_one :comment | 15 | has_one :comment |
16 | 16 | ||
17 | + def self.average_rating community_id | ||
18 | + average = CommunityRating.where(community_id: community_id).average(:value) | ||
19 | + | ||
20 | + if average | ||
21 | + (average - average.truncate) >= 0.5 ? average.ceil : average.floor | ||
22 | + else | ||
23 | + nil | ||
24 | + end | ||
25 | + end | ||
26 | + | ||
17 | end | 27 | end |