socialization 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/socialization/follower.rb +1 -0
- data/lib/socialization/liker.rb +1 -0
- data/lib/socialization/version.rb +1 -1
- data/socialization.gemspec +1 -0
- data/test/follow_test.rb +71 -25
- data/test/like_test.rb +69 -32
- data/test/test_helper.rb +33 -0
- data/test/world_test.rb +91 -0
- metadata +24 -11
@@ -11,6 +11,7 @@ module Socialization
|
|
11
11
|
|
12
12
|
def follow!(followable)
|
13
13
|
raise ArgumentError, "#{followable} is not followable!" unless followable.respond_to?(:is_followable?)
|
14
|
+
raise ArgumentError, "#{self} cannot follow itself!" unless self != followable
|
14
15
|
Follow.create! :follower => self, :followable => followable
|
15
16
|
end
|
16
17
|
|
data/lib/socialization/liker.rb
CHANGED
@@ -11,6 +11,7 @@ module Socialization
|
|
11
11
|
|
12
12
|
def like!(likeable)
|
13
13
|
raise ArgumentError, "#{likeable} is not likeable!" unless likeable.respond_to?(:is_likeable?)
|
14
|
+
raise ArgumentError, "#{self} cannot like itself!" unless self != likeable
|
14
15
|
Like.create! :liker => self, :likeable => likeable
|
15
16
|
end
|
16
17
|
|
data/socialization.gemspec
CHANGED
data/test/follow_test.rb
CHANGED
@@ -1,40 +1,86 @@
|
|
1
1
|
require File.dirname(__FILE__)+'/test_helper'
|
2
2
|
|
3
3
|
class FollowTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
context "a Follower" do
|
5
|
+
setup do
|
6
|
+
seed
|
7
|
+
end
|
8
|
+
|
9
|
+
should "respond to is_follower?" do
|
10
|
+
assert_equal true, @follower1.respond_to?(:is_follower?)
|
11
|
+
assert_equal true, @follower1.is_follower?
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to follow a Followable" do
|
15
|
+
assert @follower1.follow!(@followable1)
|
16
|
+
assert_equal true, @follower1.follows?(@followable1)
|
17
|
+
assert_equal false, @follower2.follows?(@followable1)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be able to unfollow a Followable" do
|
21
|
+
Follow.create :follower => @follower1, :followable => @followable1
|
22
|
+
assert @follower1.unfollow!(@followable1)
|
23
|
+
assert_equal false, @follower1.follows?(@followable1)
|
24
|
+
end
|
10
25
|
end
|
11
26
|
|
12
|
-
|
13
|
-
|
14
|
-
|
27
|
+
context "a Followable" do
|
28
|
+
setup do
|
29
|
+
seed
|
30
|
+
end
|
31
|
+
|
32
|
+
should "respond to is_followable?" do
|
33
|
+
assert_equal true, @followable1.respond_to?(:is_followable?)
|
34
|
+
assert_equal true, @followable1.is_followable?
|
35
|
+
end
|
36
|
+
|
37
|
+
should "be able to determine who follows it" do
|
38
|
+
Follow.create :follower => @follower1, :followable => @followable1
|
39
|
+
assert_equal true, @followable1.followed_by?(@follower1)
|
40
|
+
assert_equal false, @followable1.followed_by?(@follower2)
|
41
|
+
end
|
15
42
|
|
16
|
-
|
17
|
-
|
18
|
-
|
43
|
+
should "expose a list of its followers" do
|
44
|
+
Follow.create :follower => @follower1, :followable => @followable1
|
45
|
+
assert_equal [@follower1], @followable1.followers
|
46
|
+
end
|
47
|
+
|
48
|
+
should "expose followings" do
|
49
|
+
Follow.create :follower => @follower1, :followable => @followable1
|
50
|
+
followings = @followable1.followings
|
51
|
+
assert_equal 1, followings.size
|
52
|
+
assert followings.first.is_a?(Follow)
|
53
|
+
end
|
54
|
+
end
|
19
55
|
|
20
|
-
|
21
|
-
|
22
|
-
|
56
|
+
context "Deleting a Follower" do
|
57
|
+
setup do
|
58
|
+
seed
|
59
|
+
@follower1.follow!(@followable1)
|
60
|
+
end
|
23
61
|
|
24
|
-
|
62
|
+
should "delete its Follow records" do
|
63
|
+
@follower1.destroy
|
64
|
+
assert_equal false, @followable1.followed_by?(@follower1)
|
65
|
+
end
|
66
|
+
end
|
25
67
|
|
26
|
-
|
27
|
-
|
28
|
-
|
68
|
+
context "Deleting a Followable" do
|
69
|
+
setup do
|
70
|
+
seed
|
71
|
+
@follower1.follow!(@followable1)
|
29
72
|
end
|
30
73
|
|
31
|
-
|
32
|
-
|
74
|
+
should "delete its Follow records" do
|
75
|
+
@followable1.destroy
|
76
|
+
assert_equal false, @follower1.follows?(@followable1)
|
77
|
+
end
|
33
78
|
end
|
34
79
|
|
35
|
-
def
|
36
|
-
@
|
37
|
-
|
38
|
-
|
80
|
+
def seed
|
81
|
+
@follower1 = ImAFollower.create
|
82
|
+
@follower2 = ImAFollower.create
|
83
|
+
@followable1 = ImAFollowable.create
|
84
|
+
@followable2 = ImAFollowable.create
|
39
85
|
end
|
40
86
|
end
|
data/test/like_test.rb
CHANGED
@@ -1,49 +1,86 @@
|
|
1
1
|
require File.dirname(__FILE__)+'/test_helper'
|
2
2
|
|
3
3
|
class LikeTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
context "a Liker" do
|
5
|
+
setup do
|
6
|
+
seed
|
7
|
+
end
|
8
|
+
|
9
|
+
should "respond to is_liker?" do
|
10
|
+
assert_equal true, @liker1.respond_to?(:is_liker?)
|
11
|
+
assert_equal true, @liker1.is_liker?
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to like a Likeable" do
|
15
|
+
assert @liker1.like!(@likeable1)
|
16
|
+
assert_equal true, @liker1.likes?(@likeable1)
|
17
|
+
assert_equal false, @liker2.likes?(@likeable1)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "be able to unlike a Likeable" do
|
21
|
+
Like.create :liker => @liker1, :likeable => @likeable1
|
22
|
+
assert @liker1.unlike!(@likeable1)
|
23
|
+
assert_equal false, @liker1.likes?(@likeable1)
|
24
|
+
end
|
10
25
|
end
|
11
26
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
27
|
+
context "a Likeable" do
|
28
|
+
setup do
|
29
|
+
seed
|
30
|
+
end
|
16
31
|
|
17
|
-
|
18
|
-
|
19
|
-
|
32
|
+
should "respond to is_likeable?" do
|
33
|
+
assert_equal true, @likeable1.respond_to?(:is_likeable?)
|
34
|
+
assert_equal true, @likeable1.is_likeable?
|
35
|
+
end
|
20
36
|
|
21
|
-
|
22
|
-
|
37
|
+
should "be able to determine who likes it" do
|
38
|
+
Like.create :liker => @liker1, :likeable => @likeable1
|
39
|
+
assert_equal true, @likeable1.liked_by?(@liker1)
|
40
|
+
assert_equal false, @likeable1.liked_by?(@liker2)
|
23
41
|
end
|
24
42
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
43
|
+
should "expose a list of its likers" do
|
44
|
+
Like.create :liker => @liker1, :likeable => @likeable1
|
45
|
+
assert_equal [@liker1], @likeable1.likers
|
46
|
+
end
|
47
|
+
|
48
|
+
should "expose likings" do
|
49
|
+
Like.create :liker => @liker1, :likeable => @likeable1
|
50
|
+
likings = @likeable1.likings
|
51
|
+
assert_equal 1, likings.size
|
52
|
+
assert likings.first.is_a?(Like)
|
53
|
+
end
|
54
|
+
end
|
29
55
|
|
30
|
-
|
31
|
-
|
56
|
+
context "Deleting a Liker" do
|
57
|
+
setup do
|
58
|
+
seed
|
59
|
+
@liker1.like!(@likeable1)
|
60
|
+
end
|
32
61
|
|
33
|
-
|
62
|
+
should "delete its Like records" do
|
63
|
+
@liker1.destroy
|
64
|
+
assert_equal false, @likeable1.liked_by?(@liker1)
|
65
|
+
end
|
66
|
+
end
|
34
67
|
|
35
|
-
|
36
|
-
|
37
|
-
|
68
|
+
context "Deleting a Likeable" do
|
69
|
+
setup do
|
70
|
+
seed
|
71
|
+
@liker1.like!(@likeable1)
|
38
72
|
end
|
39
73
|
|
40
|
-
|
41
|
-
|
74
|
+
should "delete its Like records" do
|
75
|
+
@likeable1.destroy
|
76
|
+
assert_equal false, @liker1.likes?(@likeable1)
|
77
|
+
end
|
42
78
|
end
|
43
79
|
|
44
|
-
def
|
45
|
-
@
|
46
|
-
|
47
|
-
|
80
|
+
def seed
|
81
|
+
@liker1 = ImALiker.create
|
82
|
+
@liker2 = ImALiker.create
|
83
|
+
@likeable1 = ImALikeable.create
|
84
|
+
@likeable2 = ImALikeable.create
|
48
85
|
end
|
49
|
-
end
|
86
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'active_record'
|
3
3
|
require 'mocha'
|
4
|
+
require 'shoulda'
|
4
5
|
require 'test/unit'
|
5
6
|
require 'logger'
|
6
7
|
|
@@ -41,6 +42,22 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
41
42
|
t.integer :likeable_id
|
42
43
|
t.datetime :created_at
|
43
44
|
end
|
45
|
+
|
46
|
+
create_table :im_a_followers do |t|
|
47
|
+
t.timestamps
|
48
|
+
end
|
49
|
+
|
50
|
+
create_table :im_a_followables do |t|
|
51
|
+
t.timestamps
|
52
|
+
end
|
53
|
+
|
54
|
+
create_table :im_a_likers do |t|
|
55
|
+
t.timestamps
|
56
|
+
end
|
57
|
+
|
58
|
+
create_table :im_a_likeables do |t|
|
59
|
+
t.timestamps
|
60
|
+
end
|
44
61
|
end
|
45
62
|
|
46
63
|
class Celebrity < ActiveRecord::Base
|
@@ -65,3 +82,19 @@ end
|
|
65
82
|
class Like < ActiveRecord::Base
|
66
83
|
acts_as_like_store
|
67
84
|
end
|
85
|
+
|
86
|
+
class ImAFollower < ActiveRecord::Base
|
87
|
+
acts_as_follower
|
88
|
+
end
|
89
|
+
|
90
|
+
class ImAFollowable < ActiveRecord::Base
|
91
|
+
acts_as_followable
|
92
|
+
end
|
93
|
+
|
94
|
+
class ImALiker < ActiveRecord::Base
|
95
|
+
acts_as_liker
|
96
|
+
end
|
97
|
+
|
98
|
+
class ImALikeable < ActiveRecord::Base
|
99
|
+
acts_as_likeable
|
100
|
+
end
|
data/test/world_test.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.dirname(__FILE__)+'/test_helper'
|
2
|
+
|
3
|
+
# Test Socialization as it would be used in a "real world" scenario
|
4
|
+
class WorldTest < Test::Unit::TestCase
|
5
|
+
attr_reader :users, :movies, :celebs
|
6
|
+
|
7
|
+
|
8
|
+
context "The World" do
|
9
|
+
setup do
|
10
|
+
seed
|
11
|
+
end
|
12
|
+
|
13
|
+
should "be social" do
|
14
|
+
john.like!(pulp)
|
15
|
+
john.follow!(jane)
|
16
|
+
john.follow!(travolta)
|
17
|
+
|
18
|
+
assert john.likes?(pulp)
|
19
|
+
assert john.follows?(jane)
|
20
|
+
assert john.follows?(travolta)
|
21
|
+
|
22
|
+
assert pulp.liked_by?(john)
|
23
|
+
assert travolta.followed_by?(john)
|
24
|
+
|
25
|
+
carl.like!(pulp)
|
26
|
+
camilo.like!(pulp)
|
27
|
+
assert_equal 3, pulp.likers.size
|
28
|
+
|
29
|
+
assert pulp.likers.include?(carl)
|
30
|
+
assert pulp.likers.include?(john)
|
31
|
+
assert pulp.likers.include?(camilo)
|
32
|
+
assert !pulp.likers.include?(mat)
|
33
|
+
|
34
|
+
carl.follow!(mat)
|
35
|
+
mat.follow!(carl)
|
36
|
+
camilo.follow!(carl)
|
37
|
+
|
38
|
+
assert carl.follows?(mat)
|
39
|
+
assert mat.followed_by?(carl)
|
40
|
+
assert mat.follows?(carl)
|
41
|
+
assert carl.followed_by?(mat)
|
42
|
+
assert camilo.follows?(carl)
|
43
|
+
assert !carl.follows?(camilo)
|
44
|
+
|
45
|
+
assert_raise ArgumentError do
|
46
|
+
john.like!(travolta) # Can't like a Celeb
|
47
|
+
end
|
48
|
+
|
49
|
+
assert_raise ArgumentError do
|
50
|
+
john.follow!(killbill) # Can't follow a movie
|
51
|
+
end
|
52
|
+
|
53
|
+
assert_raise ArgumentError do
|
54
|
+
john.follow!(john) # Can't follow yourself, duh!
|
55
|
+
end
|
56
|
+
|
57
|
+
assert_raise ArgumentError do
|
58
|
+
john.like!(john) # Can't like yourself, duh!
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def seed
|
64
|
+
@users = {}
|
65
|
+
@celebs = {}
|
66
|
+
@movies = {}
|
67
|
+
|
68
|
+
@users[:john] = User.create :name => 'John Doe'
|
69
|
+
@users[:jane] = User.create :name => 'Jane Doe'
|
70
|
+
@users[:mat] = User.create :name => 'Mat'
|
71
|
+
@users[:carl] = User.create :name => 'Carl'
|
72
|
+
@users[:camilo] = User.create :name => 'Camilo'
|
73
|
+
|
74
|
+
@movies[:pulp] = Movie.create :name => 'Pulp Fiction'
|
75
|
+
@movies[:reservoir] = Movie.create :name => 'Reservoir Dogs'
|
76
|
+
@movies[:killbill] = Movie.create :name => 'Kill Bill'
|
77
|
+
|
78
|
+
@celebs[:willis] = Celebrity.create :name => 'Bruce Willis'
|
79
|
+
@celebs[:travolta] = Celebrity.create :name => 'John Travolta'
|
80
|
+
@celebs[:jackson] = Celebrity.create :name => 'Samuel L. Jackson'
|
81
|
+
end
|
82
|
+
|
83
|
+
def method_missing(meth, *args, &block)
|
84
|
+
sym = meth.to_sym
|
85
|
+
return users[sym] if users[sym]
|
86
|
+
return celebs[sym] if celebs[sym]
|
87
|
+
return movies[sym] if movies[sym]
|
88
|
+
super
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-15 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70117266360600 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70117266360600
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: appraisal
|
27
|
-
requirement: &
|
27
|
+
requirement: &70117266360180 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70117266360180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: logger
|
38
|
-
requirement: &
|
38
|
+
requirement: &70117266359760 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70117266359760
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mocha
|
49
|
-
requirement: &
|
49
|
+
requirement: &70117266359340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,21 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70117266359340
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: shoulda
|
60
|
+
requirement: &70117266358920 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70117266358920
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: sqlite3
|
60
|
-
requirement: &
|
71
|
+
requirement: &70117266358500 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,7 +76,7 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70117266358500
|
69
80
|
description: Socialization allows any model to Follow and/or Like any other model.
|
70
81
|
This is accomplished through a double polymorphic relationship on the Follow and
|
71
82
|
Like models. But you don't need to know that since all the complexity is hidden
|
@@ -168,6 +179,7 @@ files:
|
|
168
179
|
- test/follow_test.rb
|
169
180
|
- test/like_test.rb
|
170
181
|
- test/test_helper.rb
|
182
|
+
- test/world_test.rb
|
171
183
|
homepage: https://github.com/cmer/socialization
|
172
184
|
licenses: []
|
173
185
|
post_install_message:
|
@@ -197,4 +209,5 @@ test_files:
|
|
197
209
|
- test/follow_test.rb
|
198
210
|
- test/like_test.rb
|
199
211
|
- test/test_helper.rb
|
212
|
+
- test/world_test.rb
|
200
213
|
has_rdoc: true
|