lessonplanet-acts_as_rateable 3.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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/acts_as_rateable.gemspec +18 -0
- data/lib/acts_as_rateable.rb +14 -0
- data/lib/acts_as_rateable/acts_as_rateable/core.rb +49 -0
- data/lib/acts_as_rateable/rateable.rb +10 -0
- data/lib/acts_as_rateable/rating.rb +6 -0
- data/lib/acts_as_rateable/version.rb +3 -0
- data/lib/tasks/acts_as_rateable_tasks.rake +4 -0
- data/test/acts_as_rateable_test.rb +8 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Cosmin Radoi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
ActsAsRateable
|
2
|
+
==============
|
3
|
+
A Rails 3.2 ready gem that adds rateable functionality to models.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
* ```gem 'lessonplanet-acts_as_rateable', :require => 'acts_as_rateable'```
|
7
|
+
* Create a new rails migration and add the following self.up and self.down methods
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def self.up
|
11
|
+
create_table "ratings", :force => true do |t|
|
12
|
+
t.column "rating", :integer, :default => 0
|
13
|
+
t.column "created_at", :datetime, :null => false
|
14
|
+
t.column "rateable_type", :string, :limit => 15, :default => "", :null => false
|
15
|
+
t.column "rateable_id", :integer, :default => 0, :null => false
|
16
|
+
t.column "user_id", :integer, :default => 0, :null => false
|
17
|
+
end
|
18
|
+
|
19
|
+
add_index "ratings", ["user_id"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.down
|
23
|
+
drop_table :ratings
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Make you ActiveRecord model act as rateable:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Model < ActiveRecord::Base
|
33
|
+
acts_as_rateable
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Add a rating to a model instance:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
object.rate_it 1, current_user
|
41
|
+
```
|
42
|
+
|
43
|
+
Get average rating:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Model.last.rating
|
47
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_rateable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "lessonplanet-acts_as_rateable"
|
7
|
+
s.version = ActsAsRateable::VERSION
|
8
|
+
s.authors = ["Jason Rust"]
|
9
|
+
s.email = ["jason@lessonplanet.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Rails plugin providing a rating interface for ActiveRecord models}
|
12
|
+
s.description = %q{Rails plugin providing a rating interface for ActiveRecord models}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/version"
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
|
6
|
+
require "acts_as_rateable/rateable.rb"
|
7
|
+
require "acts_as_rateable/acts_as_rateable/core.rb"
|
8
|
+
require "acts_as_rateable/rating.rb"
|
9
|
+
|
10
|
+
$LOAD_PATH.shift
|
11
|
+
|
12
|
+
if defined?(ActiveRecord::Base)
|
13
|
+
ActiveRecord::Base.extend ActsAsRateable::Rateable
|
14
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActsAsRateable::Rateable
|
2
|
+
module Core
|
3
|
+
def self.included(base)
|
4
|
+
base.send :include, ActsAsRateable::Rateable::Core::InstanceMethods
|
5
|
+
base.extend ActsAsRateable::Rateable::Core::ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Helper method to lookup for ratings for a given object.
|
10
|
+
# This method is equivalent to obj.ratings
|
11
|
+
def ratings_for(obj)
|
12
|
+
rateable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
13
|
+
scoped(
|
14
|
+
:conditions => { :rateable_id => obj.id, :rateable_type => rateable },
|
15
|
+
:order => 'created_at DESC'
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Helper class method to lookup ratings for
|
20
|
+
# the mixin rateable type written by a given user.
|
21
|
+
# This method is NOT equivalent to Rating.find_ratings_for_user
|
22
|
+
def ratings_by_user(user)
|
23
|
+
rateable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
24
|
+
scoped(
|
25
|
+
:conditions => { :user_id => user.id, :rateable_type => rateable },
|
26
|
+
:order => 'created_at DESC'
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module InstanceMethods
|
32
|
+
# Helper method that defaults the current time to the submitted field.
|
33
|
+
def rate_it(rating, user)
|
34
|
+
ratings.where(:user_id => user.id).delete_all if user.present?
|
35
|
+
ratings << ActsAsRateable::Rating.new(:rating => rating.to_i, :user_id => user.try(:id))
|
36
|
+
end
|
37
|
+
|
38
|
+
# Helper method that returns the average rating
|
39
|
+
def rating
|
40
|
+
ratings.average(:rating).to_f
|
41
|
+
end
|
42
|
+
|
43
|
+
# Check to see if a user already rated this rateable
|
44
|
+
def rated_by_user?(user)
|
45
|
+
ratings.where(:user_id => user.id).count > 0
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module ActsAsRateable
|
2
|
+
module Rateable
|
3
|
+
def acts_as_rateable(options = {})
|
4
|
+
class_eval do
|
5
|
+
has_many :ratings, :as => :rateable, :dependent => :destroy, :class_name => 'ActsAsRateable::Rating'
|
6
|
+
include ActsAsRateable::Rateable::Core
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lessonplanet-acts_as_rateable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Rust
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Rails plugin providing a rating interface for ActiveRecord models
|
15
|
+
email:
|
16
|
+
- jason@lessonplanet.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- MIT-LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- acts_as_rateable.gemspec
|
27
|
+
- lib/acts_as_rateable.rb
|
28
|
+
- lib/acts_as_rateable/acts_as_rateable/core.rb
|
29
|
+
- lib/acts_as_rateable/rateable.rb
|
30
|
+
- lib/acts_as_rateable/rating.rb
|
31
|
+
- lib/acts_as_rateable/version.rb
|
32
|
+
- lib/tasks/acts_as_rateable_tasks.rake
|
33
|
+
- test/acts_as_rateable_test.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.17
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Rails plugin providing a rating interface for ActiveRecord models
|
58
|
+
test_files:
|
59
|
+
- test/acts_as_rateable_test.rb
|