rails_rateable 0.0.1 → 0.0.2
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/lib/app/models/rails_rateable.rb +64 -0
- data/lib/{rating.rb → app/models/rating.rb} +0 -0
- data/lib/{user_rating.rb → app/models/user_rating.rb} +0 -0
- data/lib/generators/rails_rateable/rails_rateable_generator.rb +1 -1
- data/lib/generators/rails_rateable/templates/migration.rb +1 -1
- data/lib/rails_rateable/version.rb +1 -1
- data/lib/rails_rateable.rb +3 -63
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c0770a073f90fffb1bfd2cc9b8d786585ed0848
|
4
|
+
data.tar.gz: 17ab4e67a4f49694a7afaec5c6a5595d1afe3d65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 613e77a3f6ae71d6a662dfadc6e0f9eb5d6f05c1020efc3725b4ea788ca00e00b84a79f8c755baee211e898ed8e59698af248a3452ce668bcc126933b537d8b8
|
7
|
+
data.tar.gz: 251252e3e85b0574d529540874f1596214eb89d331b2d0d86e33508752f5f665ece10e52a28a0ecb2ec93d64820b11e5f34f422fbce75a2b8ddee2284e0bedf7
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module RailsRateable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
has_one :rating, :as => :rateable, :dependent => :destroy
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
#put class method here to be trigger as say User.method_name
|
10
|
+
def acts_as_rateable(options = {})
|
11
|
+
unless respond_to?(:max_rating)
|
12
|
+
class_attribute :max_rating
|
13
|
+
self.max_rating = options[:max_rating] || 5
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_top_rated(params = {})
|
18
|
+
find_params = params.merge(:include => :rating)
|
19
|
+
find_params[:order] = ['ratings.average_rating DESC', find_params.delete(:order)].compact.join(", ")
|
20
|
+
find_params[:limit] = 20 unless find_params.key?(:limit)
|
21
|
+
find(:all, find_params)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
# Rates the object by a given score. A user object should be passed to the method.
|
27
|
+
def rate_it(score, user)
|
28
|
+
create_rating unless rating
|
29
|
+
rating.rate(score, user)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the average rating. Calculation based on the already given scores.
|
33
|
+
def average_rating
|
34
|
+
rating && rating.average_rating || 0.0
|
35
|
+
end
|
36
|
+
|
37
|
+
# Rounds the average rating value.
|
38
|
+
def average_rating_round
|
39
|
+
average_rating.round
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the average rating in percent.
|
43
|
+
def average_rating_percent
|
44
|
+
f = 100 / max_rating.to_f
|
45
|
+
average_rating * f
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the number of ratings.
|
49
|
+
def ratings_count
|
50
|
+
rating && rating.ratings_count || 0
|
51
|
+
end
|
52
|
+
|
53
|
+
# Checks whether a user rated the object or not.
|
54
|
+
def rated_by?(user)
|
55
|
+
rating && rating.user_ratings.exists?(:user_id => user)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the rating a specific user has given the object.
|
59
|
+
def rating_by(user)
|
60
|
+
user_rating = rating && rating.user_ratings.find_by_user_id(user.id)
|
61
|
+
user_rating ? user_rating.score : nil
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
File without changes
|
File without changes
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rails/generators/migration'
|
2
2
|
|
3
|
-
class
|
3
|
+
class RailsRateableGenerator < Rails::Generators::Base
|
4
4
|
include Rails::Generators::Migration
|
5
5
|
def self.source_root
|
6
6
|
@_rails_rateable_source_root ||= File.expand_path("../templates", __FILE__)
|
data/lib/rails_rateable.rb
CHANGED
@@ -1,64 +1,4 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'app/models/rails_rateable'
|
2
|
+
require 'app/models/rating'
|
3
|
+
require 'app/models/user_rating'
|
3
4
|
|
4
|
-
included do
|
5
|
-
has_one :rating, :as => :rateable, :dependent => :destroy
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
#put class method here to be trigger as say User.method_name
|
10
|
-
def acts_as_rateable(options = {})
|
11
|
-
unless respond_to?(:max_rating)
|
12
|
-
class_attribute :max_rating
|
13
|
-
self.max_rating = options[:max_rating] || 5
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def find_top_rated(params = {})
|
18
|
-
find_params = params.merge(:include => :rating)
|
19
|
-
find_params[:order] = ['ratings.average_rating DESC', find_params.delete(:order)].compact.join(", ")
|
20
|
-
find_params[:limit] = 20 unless find_params.key?(:limit)
|
21
|
-
find(:all, find_params)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
# Rates the object by a given score. A user object should be passed to the method.
|
27
|
-
def rate_it(score, user)
|
28
|
-
create_rating unless rating
|
29
|
-
rating.rate(score, user)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Returns the average rating. Calculation based on the already given scores.
|
33
|
-
def average_rating
|
34
|
-
rating && rating.average_rating || 0.0
|
35
|
-
end
|
36
|
-
|
37
|
-
# Rounds the average rating value.
|
38
|
-
def average_rating_round
|
39
|
-
average_rating.round
|
40
|
-
end
|
41
|
-
|
42
|
-
# Returns the average rating in percent.
|
43
|
-
def average_rating_percent
|
44
|
-
f = 100 / max_rating.to_f
|
45
|
-
average_rating * f
|
46
|
-
end
|
47
|
-
|
48
|
-
# Returns the number of ratings.
|
49
|
-
def ratings_count
|
50
|
-
rating && rating.ratings_count || 0
|
51
|
-
end
|
52
|
-
|
53
|
-
# Checks whether a user rated the object or not.
|
54
|
-
def rated_by?(user)
|
55
|
-
rating && rating.user_ratings.exists?(:user_id => user)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Returns the rating a specific user has given the object.
|
59
|
-
def rating_by(user)
|
60
|
-
user_rating = rating && rating.user_ratings.find_by_user_id(user.id)
|
61
|
-
user_rating ? user_rating.score : nil
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_rateable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arun
|
@@ -51,12 +51,13 @@ files:
|
|
51
51
|
- LICENSE.txt
|
52
52
|
- README.md
|
53
53
|
- Rakefile
|
54
|
+
- lib/app/models/rails_rateable.rb
|
55
|
+
- lib/app/models/rating.rb
|
56
|
+
- lib/app/models/user_rating.rb
|
54
57
|
- lib/generators/rails_rateable/rails_rateable_generator.rb
|
55
58
|
- lib/generators/rails_rateable/templates/migration.rb
|
56
59
|
- lib/rails_rateable.rb
|
57
60
|
- lib/rails_rateable/version.rb
|
58
|
-
- lib/rating.rb
|
59
|
-
- lib/user_rating.rb
|
60
61
|
- rails_rateable.gemspec
|
61
62
|
homepage: https://github.com/trustarun/rails_rateable
|
62
63
|
licenses:
|