movr_acts_as_likeable 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/like.rb +12 -0
- data/lib/movr_acts_as_likeable.rb +47 -65
- data/movr_acts_as_likeable.gemspec +3 -3
- metadata +5 -5
- data/lib/liking.rb +0 -18
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/like.rb
ADDED
@@ -1,68 +1,50 @@
|
|
1
|
-
#
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
# MovrActsAsLikable
|
2
|
+
module Adventtec
|
3
|
+
module Acts #:nodoc:
|
4
|
+
module Likable #:nodoc:
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def acts_as_likable
|
12
|
+
has_many :likes, :as => :likable, :dependent => :destroy
|
13
|
+
include Adventtec::Acts::Likable::InstanceMethods
|
14
|
+
extend Adventtec::Acts::Likable::SingletonMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# This module contains class methods
|
19
|
+
module SingletonMethods
|
20
|
+
def find_likes_cast_by_user(user)
|
21
|
+
likable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
|
22
|
+
Like.find(:all,
|
23
|
+
:conditions => ["user_id = ? likable_type = ?", user.id, likable],
|
24
|
+
:order => "created_at DESC"
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# This module contains instance methods
|
30
|
+
module InstanceMethods
|
31
|
+
|
32
|
+
# Same as likable.likes.size
|
33
|
+
def likes_count
|
34
|
+
self.likes.count
|
35
|
+
end
|
36
|
+
|
37
|
+
def users_who_liked
|
38
|
+
self.likes.find(:all, :include => [:user]).map(&:user)
|
39
|
+
end
|
40
|
+
|
41
|
+
def liked_by_user?(user = nil)
|
42
|
+
if not user
|
43
|
+
user = current_user
|
44
|
+
end
|
45
|
+
user && self.likes.first(:conditions => {:user_id => user.id})
|
46
|
+
end
|
47
|
+
end
|
14
48
|
end
|
15
|
-
end
|
16
|
-
|
17
|
-
# This module contains instance methods
|
18
|
-
module InstanceMethods
|
19
|
-
# Helper method that associates the Liking with the Object
|
20
|
-
def add_liking(liking)
|
21
|
-
likings << liking
|
22
|
-
end
|
23
|
-
|
24
|
-
# Helper method to calculate the total likes for this Likeable
|
25
|
-
def likes_count
|
26
|
-
likes = Liking.find(
|
27
|
-
:all,
|
28
|
-
:conditions => ["likeable_id = ? AND likeable_type = ? AND liking = TRUE",id, self.type.name])
|
29
|
-
likes.size
|
30
|
-
end
|
31
|
-
|
32
|
-
# Helper method to calculate the total dislikes for this Likeable
|
33
|
-
def dislikes_count
|
34
|
-
likes = Liking.find(
|
35
|
-
:all,
|
36
|
-
:conditions => ["likeable_id = ? AND likeable_type = ? AND liking = FALSE",id, self.type.name])
|
37
|
-
likes.size
|
38
|
-
end
|
39
|
-
|
40
|
-
# Helper method to calculate the total likings for this Likeable
|
41
|
-
def likings_count
|
42
|
-
self.likings.size
|
43
|
-
end
|
44
|
-
|
45
|
-
# Helper method to calculate the percentage of likes to dislikes for this Likeable
|
46
|
-
def likes_percentage
|
47
|
-
(likes_count*100)/likings_count
|
48
|
-
end
|
49
|
-
|
50
|
-
# Helper method to calculate the percentage of dislikes to likes for this Likeable
|
51
|
-
def dislikes_percentage
|
52
|
-
(dislikes_count*100)/likings_count
|
53
|
-
end
|
54
|
-
|
55
|
-
# Helper method to see if a user already liked this likeable
|
56
|
-
def liked_by_user?(user)
|
57
|
-
rtn = false
|
58
|
-
if user
|
59
|
-
self.likings.each { |l|
|
60
|
-
rtn = true if user.id == l.user_id
|
61
|
-
}
|
62
|
-
end
|
63
|
-
rtn
|
64
|
-
end
|
65
|
-
end
|
66
49
|
end
|
67
|
-
end
|
68
50
|
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{movr_acts_as_likeable}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Daryl Moulder, https://github.com/umerfarooq"]
|
12
|
-
s.date = %q{2012-03-
|
12
|
+
s.date = %q{2012-03-22}
|
13
13
|
s.description = %q{This gem is heavily influenced by acts_as_voteable & acts_as_rateable, can be used for Thumbs Up/Down or Likes/Dislikes situations, also helps to get the Percentage of Likes to Dislikes and vice versa on an Object, to depict average popularity.}
|
14
14
|
s.email = %q{dmoulder@qtome.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"init.rb",
|
28
|
-
"lib/
|
28
|
+
"lib/like.rb",
|
29
29
|
"lib/movr_acts_as_likeable.rb",
|
30
30
|
"movr_acts_as_likeable.gemspec",
|
31
31
|
"test/helper.rb",
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: movr_acts_as_likeable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daryl Moulder, https://github.com/umerfarooq
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-22 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
prerelease: false
|
@@ -110,7 +110,7 @@ files:
|
|
110
110
|
- Rakefile
|
111
111
|
- VERSION
|
112
112
|
- init.rb
|
113
|
-
- lib/
|
113
|
+
- lib/like.rb
|
114
114
|
- lib/movr_acts_as_likeable.rb
|
115
115
|
- movr_acts_as_likeable.gemspec
|
116
116
|
- test/helper.rb
|
data/lib/liking.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
class Liking < ActiveRecord::Base
|
2
|
-
belongs_to :likeable, :polymorphic => true
|
3
|
-
|
4
|
-
# NOTE: Likings belong to a user
|
5
|
-
belongs_to :user
|
6
|
-
|
7
|
-
named_scope :likes, :conditions => {:liking => 1}
|
8
|
-
named_scope :dislikes, :conditions => {:liking => 0}
|
9
|
-
|
10
|
-
# Helper class method to lookup all likings assigned to all likeable types for a given user.
|
11
|
-
def self.find_all_likings_by_user(user)
|
12
|
-
find(
|
13
|
-
:all,
|
14
|
-
:conditions => ["user_id = ?", user.id],
|
15
|
-
:order => "created_at DESC"
|
16
|
-
)
|
17
|
-
end
|
18
|
-
end
|