self_rateable 1.0.0
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +81 -0
- data/Rakefile +4 -0
- data/lib/self_rateable.rb +19 -0
- data/lib/self_rateable/core/class_methods.rb +15 -0
- data/lib/self_rateable/core/instance_methods.rb +80 -0
- data/lib/self_rateable/railtie.rb +12 -0
- data/lib/self_rateable/rateable.rb +25 -0
- data/lib/self_rateable/rating.rb +6 -0
- data/lib/self_rateable/version.rb +3 -0
- data/lib/tasks/prepare.rake +9 -0
- data/self_rateable.gemspec +23 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 20c351175455d48ff66cb1a2f5bf29ac7f782d38
|
4
|
+
data.tar.gz: 2e043b86afb049612bae6e5ec91bfef831436e54
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e8e657e454d172c52415b50bd61623c372391948ec60f13264cbbff6e3bd620311ca6775faa74011d05595796b6493d765acf6d2169c011af5b41f5e9312234
|
7
|
+
data.tar.gz: d3f609f983308e46b9852a5550e5109a53ecf1004842290137ec74578d26576caa5c274e38407b7aca59cf8b6f760e7241df249c42eb3d7e3846ddeffc2ddda6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 oivan
|
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,81 @@
|
|
1
|
+
# SelfRateable
|
2
|
+
|
3
|
+
Clean, powerful, customizable, simple, configurable, gem for displaying and handling ratings, likes, and feedbacks for ActiveRecord model.
|
4
|
+
People!!! Please provide your feedbacks what functionality to add on the issues page. I'm open to whatever questions for improving this gem.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'self_rateable'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install self_rateable
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Make you ActiveRecord model self rateable:
|
23
|
+
|
24
|
+
The default type is :likes
|
25
|
+
|
26
|
+
class Model < ActiveRecord::Base
|
27
|
+
self_rateable by: :class_name_of_AR_model_that_can_rate
|
28
|
+
end
|
29
|
+
|
30
|
+
Rates philosophy as in facebook & vk (like/unlike)
|
31
|
+
|
32
|
+
class Model < ActiveRecord::Base
|
33
|
+
self_rateable by: :class_name_of_AR_model_that_can_rate, type: :likes
|
34
|
+
end
|
35
|
+
|
36
|
+
Rate:
|
37
|
+
|
38
|
+
object.like object_of_AR_model_that_can_rate
|
39
|
+
|
40
|
+
Stars rates diapasone 1..5 (as on movies)
|
41
|
+
|
42
|
+
class Model < ActiveRecord::Base
|
43
|
+
self_rateable by: :class_name_of_AR_model_that_can_rate, type: :stars
|
44
|
+
end
|
45
|
+
|
46
|
+
Rate:
|
47
|
+
|
48
|
+
object.rate object_of_AR_model_that_can_rate, count_of_stars
|
49
|
+
|
50
|
+
Rates philosophy as in stackoverflow (+1 vs -1)
|
51
|
+
|
52
|
+
class Model < ActiveRecord::Base
|
53
|
+
self_rateable by: :class_name_of_AR_model_that_can_rate, type: :points
|
54
|
+
end
|
55
|
+
|
56
|
+
Rate:
|
57
|
+
|
58
|
+
object.vote object_of_AR_model_that_can_rate, bool
|
59
|
+
|
60
|
+
This vote method returns false if object_of_AR_model_that_can_rate has already voted once. You can capture that output to show some messages.
|
61
|
+
|
62
|
+
(If true it adds 1 point if false it decreases 1 point)
|
63
|
+
|
64
|
+
Run rake task to prepare and update your database:
|
65
|
+
|
66
|
+
rake self_rateable
|
67
|
+
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create new Pull Request and send it to me.)
|
76
|
+
|
77
|
+
## What to do?
|
78
|
+
|
79
|
+
1. Create generators for templates to render on UI with images and stylings of different types of ratings.
|
80
|
+
2. Create complete test coverage
|
81
|
+
3. Add more flexibility by providing more class and instance methods.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "self_rateable/version"
|
2
|
+
require "rails"
|
3
|
+
require "active_record"
|
4
|
+
require "active_record/version"
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require "self_rateable/rateable.rb"
|
9
|
+
require "self_rateable/core/instance_methods.rb"
|
10
|
+
require "self_rateable/core/class_methods.rb"
|
11
|
+
require "self_rateable/rating.rb"
|
12
|
+
|
13
|
+
module SelfRateable
|
14
|
+
require "self_rateable/railtie" if defined?(Rails)
|
15
|
+
end
|
16
|
+
|
17
|
+
$LOAD_PATH.shift
|
18
|
+
|
19
|
+
ActiveRecord::Base.extend SelfRateable::Rateable if defined?(ActiveRecord::Base)
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module SelfRateable::Core
|
2
|
+
module InstanceMethods
|
3
|
+
module Likes
|
4
|
+
def likes
|
5
|
+
ratings.sum(:rating).to_f
|
6
|
+
end
|
7
|
+
|
8
|
+
def liked_by_rater?(rater)
|
9
|
+
ratings.where(:rater_id => rater.id).count > 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def like(rater)
|
13
|
+
rater.present? ? rate = ratings.where(rater_id: rater.id).first : raise('Rater object is incorrect.')
|
14
|
+
if rate.present?
|
15
|
+
# unlike if already rated
|
16
|
+
liked_by_rater?(rater) ? ratings.where(rater_id: rater.id).delete_all : rate.rating += 1
|
17
|
+
rate.save!
|
18
|
+
else
|
19
|
+
#create new record if not yet rated
|
20
|
+
ratings << SelfRateable::Rating.new(rating: 1, rater_id: rater.try(:id))
|
21
|
+
end
|
22
|
+
self.save!
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
module Stars
|
28
|
+
def stars
|
29
|
+
ratings.average(:rating).to_f
|
30
|
+
end
|
31
|
+
|
32
|
+
def rated_by_rater?(rater)
|
33
|
+
ratings.where(:rater_id => rater.id).count > 0
|
34
|
+
end
|
35
|
+
|
36
|
+
def rate(rater, stars)
|
37
|
+
rater.present? ? rate = ratings.where(rater_id: rater.id).first : raise('Rater object is incorrect.')
|
38
|
+
stars = 5 if stars > 5
|
39
|
+
stars = 0 if stars < 1
|
40
|
+
if rated_by_rater?(rater)
|
41
|
+
# set new star rating if already rated
|
42
|
+
rate.rating = stars.to_i
|
43
|
+
rate.save!
|
44
|
+
else
|
45
|
+
#create new record if not yet rated
|
46
|
+
ratings << SelfRateable::Rating.new(rating: stars.to_i, rater_id: rater.try(:id))
|
47
|
+
end
|
48
|
+
self.save!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module Points
|
53
|
+
def votes
|
54
|
+
ratings.sum(:rating).to_f
|
55
|
+
end
|
56
|
+
|
57
|
+
def voted_by_rater?(rater)
|
58
|
+
ratings.where(:rater_id => rater.id).count > 0
|
59
|
+
end
|
60
|
+
|
61
|
+
def vote(rater, val)
|
62
|
+
value = parse_val(val)
|
63
|
+
if voted_by_rater?(rater)
|
64
|
+
# set new star rating if already rated
|
65
|
+
return false
|
66
|
+
else
|
67
|
+
#create new record if not yet rated
|
68
|
+
ratings << SelfRateable::Rating.new(rating: value.to_i, rater_id: rater.try(:id))
|
69
|
+
end
|
70
|
+
self.save!
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def parse_val(val)
|
76
|
+
val ? 1 : -1
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module SelfRateable
|
2
|
+
module Rateable
|
3
|
+
def self_rateable(options)
|
4
|
+
ops = validate_opts(options)
|
5
|
+
class_eval do
|
6
|
+
add_associations(ops[:by])
|
7
|
+
include "SelfRateable::Core::InstanceMethods::#{ops[:type].to_s.camelize}".constantize
|
8
|
+
include "SelfRateable::Core::ClassMethods::#{ops[:type].to_s.camelize}".constantize
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def add_associations(rated_by)
|
15
|
+
has_many :ratings, as: :rateable, dependent: :destroy, class_name: 'SelfRateable::Rating'
|
16
|
+
has_one :rater, as: :rater, class_name: rated_by.to_s.camelize
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_opts(options)
|
20
|
+
raise 'Incorect specification of self_rateable in your model please check documentation' if options[:type].blank? && options[:by].blank?
|
21
|
+
raise 'Incorect specification of self_rateable in your model please check documentation' if options[:by].blank?
|
22
|
+
options[:type].blank? ? {by: options[:by], type: 'likes'} : options
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
desc "Creating migration for self_rateable"
|
2
|
+
task :self_rateable do
|
3
|
+
system "rails g model ratings rating:integer rateable_type:string rateable_id:integer rater_id:integer:index"
|
4
|
+
system "rm -rf app/models/ratings.rb"
|
5
|
+
system "rm -rf test/models/ratings_test.rb"
|
6
|
+
system "rm -rf test/fixtures/ratings.yml"
|
7
|
+
system "rake db:migrate"
|
8
|
+
puts '== Do not worry I\'ve deleted ratings.rb model and cleaned up test_unit directory =================================================='
|
9
|
+
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 'self_rateable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "self_rateable"
|
8
|
+
spec.version = SelfRateable::VERSION
|
9
|
+
spec.authors = ["ostaptan"]
|
10
|
+
spec.email = ["otan256@gmail.com"]
|
11
|
+
spec.description = %q{Clean, powerful, customizable, simple, configurable, gem for displaying and handling ratings, likes, or stars for ActiveRecord model.}
|
12
|
+
spec.summary = %q{Gem for displaying and handling ratings, likes, or stars for ActiveRecord model.}
|
13
|
+
spec.homepage = "https://github.com/ostaptan/self_rateable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: self_rateable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ostaptan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-01 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Clean, powerful, customizable, simple, configurable, gem for displaying
|
42
|
+
and handling ratings, likes, or stars for ActiveRecord model.
|
43
|
+
email:
|
44
|
+
- otan256@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/self_rateable.rb
|
55
|
+
- lib/self_rateable/core/class_methods.rb
|
56
|
+
- lib/self_rateable/core/instance_methods.rb
|
57
|
+
- lib/self_rateable/railtie.rb
|
58
|
+
- lib/self_rateable/rateable.rb
|
59
|
+
- lib/self_rateable/rating.rb
|
60
|
+
- lib/self_rateable/version.rb
|
61
|
+
- lib/tasks/prepare.rake
|
62
|
+
- self_rateable.gemspec
|
63
|
+
homepage: https://github.com/ostaptan/self_rateable
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.0.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Gem for displaying and handling ratings, likes, or stars for ActiveRecord
|
87
|
+
model.
|
88
|
+
test_files: []
|