rails_rateable 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/lib/generators/rails_rateable/rails_rateable_generator.rb +16 -0
- data/lib/generators/rails_rateable/templates/migration.rb +24 -0
- data/lib/rails_rateable/version.rb +3 -0
- data/lib/rails_rateable.rb +64 -0
- data/lib/rating.rb +21 -0
- data/lib/user_rating.rb +24 -0
- data/rails_rateable.gemspec +23 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 158d90fcd250b62fd8772598b33323925067f4ae
|
4
|
+
data.tar.gz: b3d26ae78979182f5c86c427f2e716bad6edb7be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9584af79998c5c5feb884fb522c42995180e47eb7d5c4279d92425c78dfc2ce7be1c785356a5f47c10a5701a4693f9e8785be10330a553df29ad287540fda006
|
7
|
+
data.tar.gz: cb336887f9c14e52f8ec71bfa69572b1ec6426f3c52d83e5818516e05249aad906449bc16575f483cee8ff8d596c5d68d0c02c72381f2752ef2b1e0f6a2a3b41
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Arun
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= Rateble
|
2
|
+
|
3
|
+
rateable is a rails gem providing a rating interface for ActiveRecord models.
|
4
|
+
It is compitable only with Rails 3 and above. It is released under the MIT license.
|
5
|
+
|
6
|
+
|
7
|
+
= Features
|
8
|
+
|
9
|
+
* Make any of yor model rateable without any change to the existing model
|
10
|
+
* Users can update previous ratings
|
11
|
+
* Stores computed score and number of ratings for efficient data access
|
12
|
+
* Possibility to change range of the ratings (1..5 is default)
|
13
|
+
|
14
|
+
= Installation
|
15
|
+
|
16
|
+
== Install as gem
|
17
|
+
|
18
|
+
Install as a gem:
|
19
|
+
|
20
|
+
$ sudo gem instal rails_rateable
|
21
|
+
|
22
|
+
or
|
23
|
+
Add below to Gemfile and run bundle install
|
24
|
+
|
25
|
+
gem 'rails_rateable'
|
26
|
+
|
27
|
+
After you install rateable, you need to run the generator:
|
28
|
+
|
29
|
+
rails generate rails_rateable
|
30
|
+
|
31
|
+
The generator will add new migration, so run migration:
|
32
|
+
|
33
|
+
rake db:migrate
|
34
|
+
|
35
|
+
= Example Usage
|
36
|
+
|
37
|
+
Add 'include RailsRateable' to the modal you want to make rateable, then restart your application.
|
38
|
+
|
39
|
+
class Movie < ActiveRecord::Base
|
40
|
+
include RailsRateable
|
41
|
+
end
|
42
|
+
|
43
|
+
Now your model is extended by the plugin, you can rate it (1-#) or calculate the average rating.
|
44
|
+
|
45
|
+
@movie.rate_it(4, current_user)
|
46
|
+
|
47
|
+
@movie.average_rating #=> 4.0
|
48
|
+
@movie.average_rating_round #=> 4
|
49
|
+
@movie.average_rating_percent #=> 80
|
50
|
+
@movie.rated_by?(current_user) #=> true
|
51
|
+
@movie.rating_by(current_user) #=> 4
|
52
|
+
@movie.ratings_count #=> 1
|
53
|
+
|
54
|
+
Movie.find_top_rated #=> top rated records
|
55
|
+
|
56
|
+
Optional you can specify the highest score using the :max_rating paramter as follows. The default for :max_rating is 5.
|
57
|
+
|
58
|
+
class Comment < ActiveRecord::Base
|
59
|
+
acts_as_rateable :max_rating => 10
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
CREDIT:
|
64
|
+
The gem is based on https://github.com/mreinsch/acts_as_rateable. This gem simply enhance it to be compitable with Rails 3 and up
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class RailsRateableMigrationGenerator < Rails::Generator::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
def self.source_root
|
6
|
+
@_rails_rateable_source_root ||= File.expand_path("../templates", __FILE__)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.next_migration_number(path)
|
10
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
11
|
+
end
|
12
|
+
|
13
|
+
def copy_migration_file
|
14
|
+
migration_template 'migration.rb', 'db/migrate/rails_rateable_migration.rb'
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class RateableMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table "ratings" do |t|
|
4
|
+
t.references "rateable", :polymorphic => true, :nil => false
|
5
|
+
t.float "average_rating"
|
6
|
+
t.integer "ratings_count"
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
add_index "ratings", ["rateable_id", "rateable_type"]
|
10
|
+
|
11
|
+
create_table "user_ratings" do |t|
|
12
|
+
t.references "rating", :nil => false
|
13
|
+
t.references "user", :nil => false
|
14
|
+
t.integer "score", :nil => false
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
add_index "user_ratings", ["user_id", "rating_id"]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
drop_table "user_ratings"
|
22
|
+
drop_table "ratings"
|
23
|
+
end
|
24
|
+
end
|
@@ -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
|
data/lib/rating.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Rating < ActiveRecord::Base
|
2
|
+
belongs_to :rateable, :polymorphic => true
|
3
|
+
has_many :user_ratings
|
4
|
+
|
5
|
+
delegate :max_rating, :to => :rateable
|
6
|
+
|
7
|
+
def rate(score, user)
|
8
|
+
user_ratings.find_or_initialize_by(user_id: user.id).update_attributes!(:score => score)
|
9
|
+
reload
|
10
|
+
end
|
11
|
+
|
12
|
+
# Call this method the update the avarage rating; you don't normally need to
|
13
|
+
# do this manually, saving or updating a user rating already takes care of
|
14
|
+
# updating the avarage rating.
|
15
|
+
def update_rating
|
16
|
+
self.average_rating = user_ratings.average(:score)
|
17
|
+
self.ratings_count = user_ratings.count
|
18
|
+
save!
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/user_rating.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class UserRating < ActiveRecord::Base
|
2
|
+
belongs_to :rating
|
3
|
+
|
4
|
+
delegate :max_rating, :to => :rating
|
5
|
+
|
6
|
+
validates_presence_of :score
|
7
|
+
validates_uniqueness_of :user_id, :scope => :rating_id
|
8
|
+
validate :max_rating_allowed_by_parent
|
9
|
+
|
10
|
+
after_save do |user_rating|
|
11
|
+
user_rating.rating.update_rating
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def max_rating_allowed_by_parent
|
17
|
+
if score < 1
|
18
|
+
errors.add(:score, "must be greater than or equal to 1")
|
19
|
+
elsif score > max_rating
|
20
|
+
errors.add(:score, "must be less than or equal to #{max_rating}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_rateable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_rateable"
|
8
|
+
spec.version = RailsRateable::VERSION
|
9
|
+
spec.authors = ["Arun"]
|
10
|
+
spec.email = ["trustarun@yahoo.co.in"]
|
11
|
+
spec.summary = %q{rateable module to be used with rails 4}
|
12
|
+
spec.description = %q{rateable module to be used with rails 4. Made your module rateable by simply calling rateable in your Model}
|
13
|
+
spec.homepage = "https://github.com/trustarun/rails_rateable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_rateable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arun
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: rateable module to be used with rails 4. Made your module rateable by
|
42
|
+
simply calling rateable in your Model
|
43
|
+
email:
|
44
|
+
- trustarun@yahoo.co.in
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/generators/rails_rateable/rails_rateable_generator.rb
|
55
|
+
- lib/generators/rails_rateable/templates/migration.rb
|
56
|
+
- lib/rails_rateable.rb
|
57
|
+
- lib/rails_rateable/version.rb
|
58
|
+
- lib/rating.rb
|
59
|
+
- lib/user_rating.rb
|
60
|
+
- rails_rateable.gemspec
|
61
|
+
homepage: https://github.com/trustarun/rails_rateable
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.4.8
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: rateable module to be used with rails 4
|
85
|
+
test_files: []
|