socialization 0.4.0 → 0.5.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. data/.gitignore +5 -1
  2. data/CHANGELOG.md +41 -0
  3. data/README.md +246 -0
  4. data/demo/{demo_app/.gitignore → .gitignore} +0 -0
  5. data/demo/{demo_app/Gemfile → Gemfile} +1 -1
  6. data/demo/{demo_app/README.rdoc → README.rdoc} +0 -0
  7. data/demo/{demo_app/Rakefile → Rakefile} +0 -0
  8. data/demo/{demo_app/app → app}/assets/images/rails.png +0 -0
  9. data/demo/{demo_app/app → app}/assets/javascripts/application.js +0 -0
  10. data/demo/{demo_app/app → app}/assets/stylesheets/application.css +0 -0
  11. data/demo/{demo_app/app → app}/controllers/application_controller.rb +0 -0
  12. data/demo/{demo_app/app → app}/helpers/application_helper.rb +0 -0
  13. data/demo/{demo_app/app → app}/mailers/.gitkeep +0 -0
  14. data/demo/{demo_app/app → app}/models/.gitkeep +0 -0
  15. data/demo/{demo_app/app → app}/models/celebrity.rb +0 -0
  16. data/demo/{demo_app/app → app}/models/comment.rb +0 -0
  17. data/demo/app/models/follow.rb +2 -0
  18. data/demo/app/models/like.rb +2 -0
  19. data/demo/app/models/mention.rb +2 -0
  20. data/demo/{demo_app/app → app}/models/movie.rb +0 -0
  21. data/demo/{demo_app/app → app}/models/user.rb +0 -0
  22. data/demo/{demo_app/app → app}/views/layouts/application.html.erb +0 -0
  23. data/demo/{demo_app/config.ru → config.ru} +0 -0
  24. data/demo/{demo_app/config → config}/application.rb +0 -0
  25. data/demo/{demo_app/config → config}/boot.rb +0 -0
  26. data/demo/{demo_app/config → config}/database.yml +0 -0
  27. data/demo/{demo_app/config → config}/environment.rb +0 -0
  28. data/demo/{demo_app/config → config}/environments/development.rb +0 -0
  29. data/demo/{demo_app/config → config}/environments/production.rb +0 -0
  30. data/demo/{demo_app/config → config}/environments/test.rb +0 -0
  31. data/demo/{demo_app/config → config}/initializers/backtrace_silencers.rb +0 -0
  32. data/demo/{demo_app/config → config}/initializers/inflections.rb +0 -0
  33. data/demo/{demo_app/config → config}/initializers/mime_types.rb +0 -0
  34. data/demo/{demo_app/config → config}/initializers/secret_token.rb +0 -0
  35. data/demo/{demo_app/config → config}/initializers/session_store.rb +1 -1
  36. data/demo/{demo_app/config → config}/initializers/wrap_parameters.rb +1 -1
  37. data/demo/{demo_app/config → config}/locales/en.yml +0 -0
  38. data/demo/{demo_app/config → config}/routes.rb +0 -0
  39. data/demo/{demo_app/db → db}/migrate/20120115051222_create_users.rb +0 -0
  40. data/demo/{demo_app/db → db}/migrate/20120115051234_create_movies.rb +0 -0
  41. data/demo/{demo_app/db → db}/migrate/20120115051255_create_celebrities.rb +0 -0
  42. data/demo/{demo_app/db → db}/migrate/20120115054646_create_follows.rb +0 -0
  43. data/demo/{demo_app/db → db}/migrate/20120115054647_create_likes.rb +0 -0
  44. data/demo/{demo_app/db → db}/migrate/20120221200644_create_mentions.rb +0 -0
  45. data/demo/{demo_app/db → db}/migrate/20120221202703_create_comments.rb +0 -0
  46. data/demo/{demo_app/db → db}/schema.rb +0 -1
  47. data/demo/{demo_app/db → db}/seeds.rb +0 -0
  48. data/demo/{demo_app/lib → lib}/assets/.gitkeep +0 -0
  49. data/demo/{demo_app/lib → lib}/tasks/.gitkeep +0 -0
  50. data/demo/{demo_app/public → public}/404.html +0 -0
  51. data/demo/{demo_app/public → public}/422.html +0 -0
  52. data/demo/{demo_app/public → public}/500.html +0 -0
  53. data/demo/{demo_app/public → public}/favicon.ico +0 -0
  54. data/demo/{demo_app/public → public}/index.html +0 -0
  55. data/demo/{demo_app/public → public}/robots.txt +0 -0
  56. data/demo/{demo_app/script → script}/rails +0 -0
  57. data/demo/{demo_app/test → test}/fixtures/.gitkeep +0 -0
  58. data/demo/{demo_app/test → test}/fixtures/celebrities.yml +0 -0
  59. data/demo/{demo_app/test → test}/fixtures/movies.yml +0 -0
  60. data/demo/{demo_app/test → test}/fixtures/users.yml +0 -0
  61. data/demo/{demo_app/test → test}/functional/.gitkeep +0 -0
  62. data/demo/{demo_app/test → test}/integration/.gitkeep +0 -0
  63. data/demo/{demo_app/test → test}/performance/browsing_test.rb +0 -0
  64. data/demo/{demo_app/test → test}/test_helper.rb +0 -0
  65. data/demo/{demo_app/test → test}/unit/.gitkeep +0 -0
  66. data/demo/{demo_app/test → test}/unit/celebrity_test.rb +0 -0
  67. data/demo/{demo_app/test → test}/unit/movie_test.rb +0 -0
  68. data/demo/{demo_app/test → test}/unit/user_test.rb +0 -0
  69. data/demo/{demo_app/vendor → vendor}/assets/javascripts/.gitkeep +0 -0
  70. data/demo/{demo_app/vendor → vendor}/assets/stylesheets/.gitkeep +0 -0
  71. data/demo/{demo_app/vendor → vendor}/plugins/.gitkeep +0 -0
  72. data/lib/generators/socialization/socialization_generator.rb +20 -9
  73. data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_follows.rb +0 -0
  74. data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_likes.rb +0 -0
  75. data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_mentions.rb +0 -0
  76. data/lib/generators/socialization/templates/active_record/model_follow.rb +2 -0
  77. data/lib/generators/socialization/templates/active_record/model_like.rb +2 -0
  78. data/lib/generators/socialization/templates/active_record/model_mention.rb +2 -0
  79. data/lib/socialization.rb +3 -5
  80. data/lib/socialization/actors/follower.rb +74 -0
  81. data/lib/socialization/actors/liker.rb +74 -0
  82. data/lib/socialization/actors/mentioner.rb +74 -0
  83. data/lib/socialization/acts_as_helpers.rb +43 -0
  84. data/lib/socialization/config.rb +45 -0
  85. data/lib/socialization/helpers/string.rb +17 -0
  86. data/lib/socialization/stores/active_record/follow_store.rb +143 -0
  87. data/lib/socialization/stores/active_record/like_store.rb +143 -0
  88. data/lib/socialization/stores/active_record/mention_store.rb +143 -0
  89. data/lib/socialization/stores/redis/base.rb +8 -0
  90. data/lib/socialization/stores/redis/config.rb +11 -0
  91. data/lib/socialization/stores/redis/follow_store.rb +126 -0
  92. data/lib/socialization/stores/redis/like_store.rb +126 -0
  93. data/lib/socialization/stores/redis/mention_store.rb +126 -0
  94. data/lib/socialization/version.rb +1 -1
  95. data/lib/socialization/victims/followable.rb +38 -0
  96. data/lib/socialization/victims/likeable.rb +38 -0
  97. data/lib/socialization/victims/mentionable.rb +38 -0
  98. data/socialization.gemspec +3 -3
  99. data/test/actors/follower_test.rb +74 -0
  100. data/test/actors/liker_test.rb +74 -0
  101. data/test/actors/mentioner_test.rb +74 -0
  102. data/test/stores/active_record/follow_store_test.rb +121 -0
  103. data/test/stores/active_record/like_store_test.rb +121 -0
  104. data/test/stores/active_record/mention_store_test.rb +121 -0
  105. data/test/stores/redis/config_test.rb +30 -0
  106. data/test/stores/redis/follow_store_test.rb +153 -0
  107. data/test/stores/redis/like_store_test.rb +153 -0
  108. data/test/stores/redis/mention_store_test.rb +153 -0
  109. data/test/string_test.rb +13 -0
  110. data/test/test_helper.rb +70 -12
  111. data/test/victims/followable_test.rb +34 -0
  112. data/test/victims/likeable_test.rb +34 -0
  113. data/test/victims/mentionable_test.rb +34 -0
  114. data/test/world_test.rb +80 -69
  115. metadata +261 -176
  116. data/README.rdoc +0 -186
  117. data/demo/demo_app/app/models/follow.rb +0 -3
  118. data/demo/demo_app/app/models/like.rb +0 -3
  119. data/demo/demo_app/app/models/mention.rb +0 -3
  120. data/demo/demo_app/doc/README_FOR_APP +0 -2
  121. data/gemfiles/activerecord30.gemfile +0 -6
  122. data/gemfiles/activerecord30.gemfile.lock +0 -23
  123. data/gemfiles/activerecord31.gemfile +0 -6
  124. data/gemfiles/activerecord31.gemfile.lock +0 -25
  125. data/gemfiles/activerecord32.gemfile +0 -6
  126. data/gemfiles/activerecord32.gemfile.lock +0 -25
  127. data/generators/socialization/USAGE +0 -1
  128. data/generators/socialization/socialization_generator.rb +0 -15
  129. data/generators/socialization/templates/model_follow.rb +0 -3
  130. data/generators/socialization/templates/model_like.rb +0 -3
  131. data/generators/socialization/templates/model_mention.rb +0 -3
  132. data/lib/socialization/follow_store.rb +0 -14
  133. data/lib/socialization/followable.rb +0 -34
  134. data/lib/socialization/follower.rb +0 -50
  135. data/lib/socialization/hello.rb +0 -53
  136. data/lib/socialization/like_store.rb +0 -14
  137. data/lib/socialization/likeable.rb +0 -34
  138. data/lib/socialization/liker.rb +0 -54
  139. data/lib/socialization/mention_store.rb +0 -14
  140. data/lib/socialization/mentionable.rb +0 -36
  141. data/lib/socialization/mentioner.rb +0 -54
  142. data/test/follow_test.rb +0 -125
  143. data/test/like_test.rb +0 -125
  144. data/test/mention_test.rb +0 -116
@@ -0,0 +1,38 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def is_mentionable?
4
+ false
5
+ end
6
+ end
7
+ end
8
+
9
+ module Socialization
10
+ module Mentionable
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ # Specifies if self can be mentioned.
15
+ #
16
+ # @return [Boolean]
17
+ def is_mentionable?
18
+ true
19
+ end
20
+
21
+ # Specifies if self is mentioned by a {Mentioner} object.
22
+ #
23
+ # @return [Boolean]
24
+ def mentioned_by?(mentioner)
25
+ raise ArgumentError, "#{mentioner} is not mentioner!" unless mentioner.respond_to?(:is_mentioner?) && mentioner.is_mentioner?
26
+ Socialization.mention_model.mentions?(mentioner, self)
27
+ end
28
+
29
+ # Returns a scope of the {Mentioner}s mentioning self.
30
+ #
31
+ # @param [Class] klass the {Mentioner} class to be included in the scope. e.g. `User`.
32
+ # @return [Array<Mentioner, Numeric>] An array of Mentioner objects or IDs
33
+ def mentioners(klass, opts = {})
34
+ Socialization.mention_model.mentioners(self, klass, opts)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -16,9 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.has_rdoc = true
20
- s.extra_rdoc_files = ["README.rdoc"]
21
- s.rdoc_options = ["--charset=UTF-8"]
19
+ s.has_rdoc = false
22
20
 
23
21
  s.add_runtime_dependency "activerecord"
24
22
 
@@ -27,4 +25,6 @@ Gem::Specification.new do |s|
27
25
  s.add_development_dependency "mocha"
28
26
  s.add_development_dependency "shoulda"
29
27
  s.add_development_dependency "sqlite3"
28
+ s.add_development_dependency "yard"
29
+ s.add_development_dependency "mock_redis"
30
30
  end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__))+'/../test_helper'
2
+
3
+ class FollowerTest < Test::Unit::TestCase
4
+ context "Follower" do
5
+ setup do
6
+ @follower = ImAFollower.new
7
+ @followable = ImAFollowable.create
8
+ end
9
+
10
+ context "#is_follower" do
11
+ should "return true" do
12
+ assert_true @follower.is_follower?
13
+ end
14
+ end
15
+
16
+ context "#follow!" do
17
+ should "not accept non-followables" do
18
+ assert_raise(ArgumentError) { @follower.follow!(:foo) }
19
+ end
20
+
21
+ should "call $Follow.follow!" do
22
+ $Follow.expects(:follow!).with(@follower, @followable).once
23
+ @follower.follow!(@followable)
24
+ end
25
+ end
26
+
27
+ context "#unfollow!" do
28
+ should "not accept non-followables" do
29
+ assert_raise(ArgumentError) { @follower.unfollow!(:foo) }
30
+ end
31
+
32
+ should "call $Follow.follow!" do
33
+ $Follow.expects(:unfollow!).with(@follower, @followable).once
34
+ @follower.unfollow!(@followable)
35
+ end
36
+ end
37
+
38
+ context "#toggle_follow!" do
39
+ should "not accept non-followables" do
40
+ assert_raise(ArgumentError) { @follower.unfollow!(:foo) }
41
+ end
42
+
43
+ should "unfollow when following" do
44
+ @follower.expects(:follows?).with(@followable).once.returns(true)
45
+ @follower.expects(:unfollow!).with(@followable).once
46
+ @follower.toggle_follow!(@followable)
47
+ end
48
+
49
+ should "follow when not following" do
50
+ @follower.expects(:follows?).with(@followable).once.returns(false)
51
+ @follower.expects(:follow!).with(@followable).once
52
+ @follower.toggle_follow!(@followable)
53
+ end
54
+ end
55
+
56
+ context "#follows?" do
57
+ should "not accept non-followables" do
58
+ assert_raise(ArgumentError) { @follower.unfollow!(:foo) }
59
+ end
60
+
61
+ should "call $Follow.follows?" do
62
+ $Follow.expects(:follows?).with(@follower, @followable).once
63
+ @follower.follows?(@followable)
64
+ end
65
+ end
66
+
67
+ context "#followables" do
68
+ should "call $Follow.followables" do
69
+ $Follow.expects(:followables).with(@follower, @followable.class, { :foo => :bar })
70
+ @follower.followables(@followable.class, { :foo => :bar })
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__))+'/../test_helper'
2
+
3
+ class LikerTest < Test::Unit::TestCase
4
+ context "Liker" do
5
+ setup do
6
+ @liker = ImALiker.new
7
+ @likeable = ImALikeable.create
8
+ end
9
+
10
+ context "#is_liker" do
11
+ should "return true" do
12
+ assert_true @liker.is_liker?
13
+ end
14
+ end
15
+
16
+ context "#like!" do
17
+ should "not accept non-likeables" do
18
+ assert_raise(ArgumentError) { @liker.like!(:foo) }
19
+ end
20
+
21
+ should "call $Like.like!" do
22
+ $Like.expects(:like!).with(@liker, @likeable).once
23
+ @liker.like!(@likeable)
24
+ end
25
+ end
26
+
27
+ context "#unlike!" do
28
+ should "not accept non-likeables" do
29
+ assert_raise(ArgumentError) { @liker.unlike!(:foo) }
30
+ end
31
+
32
+ should "call $Like.like!" do
33
+ $Like.expects(:unlike!).with(@liker, @likeable).once
34
+ @liker.unlike!(@likeable)
35
+ end
36
+ end
37
+
38
+ context "#toggle_like!" do
39
+ should "not accept non-likeables" do
40
+ assert_raise(ArgumentError) { @liker.unlike!(:foo) }
41
+ end
42
+
43
+ should "unlike when likeing" do
44
+ @liker.expects(:likes?).with(@likeable).once.returns(true)
45
+ @liker.expects(:unlike!).with(@likeable).once
46
+ @liker.toggle_like!(@likeable)
47
+ end
48
+
49
+ should "like when not likeing" do
50
+ @liker.expects(:likes?).with(@likeable).once.returns(false)
51
+ @liker.expects(:like!).with(@likeable).once
52
+ @liker.toggle_like!(@likeable)
53
+ end
54
+ end
55
+
56
+ context "#likes?" do
57
+ should "not accept non-likeables" do
58
+ assert_raise(ArgumentError) { @liker.unlike!(:foo) }
59
+ end
60
+
61
+ should "call $Like.likes?" do
62
+ $Like.expects(:likes?).with(@liker, @likeable).once
63
+ @liker.likes?(@likeable)
64
+ end
65
+ end
66
+
67
+ context "#likeables" do
68
+ should "call $Like.likeables" do
69
+ $Like.expects(:likeables).with(@liker, @likeable.class, { :foo => :bar })
70
+ @liker.likeables(@likeable.class, { :foo => :bar })
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__))+'/../test_helper'
2
+
3
+ class MentionerTest < Test::Unit::TestCase
4
+ context "Mentioner" do
5
+ setup do
6
+ @mentioner = ImAMentioner.new
7
+ @mentionable = ImAMentionable.create
8
+ end
9
+
10
+ context "#is_mentioner" do
11
+ should "return true" do
12
+ assert_true @mentioner.is_mentioner?
13
+ end
14
+ end
15
+
16
+ context "#mention!" do
17
+ should "not accept non-mentionables" do
18
+ assert_raise(ArgumentError) { @mentioner.mention!(:foo) }
19
+ end
20
+
21
+ should "call $Mention.mention!" do
22
+ $Mention.expects(:mention!).with(@mentioner, @mentionable).once
23
+ @mentioner.mention!(@mentionable)
24
+ end
25
+ end
26
+
27
+ context "#unmention!" do
28
+ should "not accept non-mentionables" do
29
+ assert_raise(ArgumentError) { @mentioner.unmention!(:foo) }
30
+ end
31
+
32
+ should "call $Mention.mention!" do
33
+ $Mention.expects(:unmention!).with(@mentioner, @mentionable).once
34
+ @mentioner.unmention!(@mentionable)
35
+ end
36
+ end
37
+
38
+ context "#toggle_mention!" do
39
+ should "not accept non-mentionables" do
40
+ assert_raise(ArgumentError) { @mentioner.unmention!(:foo) }
41
+ end
42
+
43
+ should "unmention when mentioning" do
44
+ @mentioner.expects(:mentions?).with(@mentionable).once.returns(true)
45
+ @mentioner.expects(:unmention!).with(@mentionable).once
46
+ @mentioner.toggle_mention!(@mentionable)
47
+ end
48
+
49
+ should "mention when not mentioning" do
50
+ @mentioner.expects(:mentions?).with(@mentionable).once.returns(false)
51
+ @mentioner.expects(:mention!).with(@mentionable).once
52
+ @mentioner.toggle_mention!(@mentionable)
53
+ end
54
+ end
55
+
56
+ context "#mentions?" do
57
+ should "not accept non-mentionables" do
58
+ assert_raise(ArgumentError) { @mentioner.unmention!(:foo) }
59
+ end
60
+
61
+ should "call $Mention.mentions?" do
62
+ $Mention.expects(:mentions?).with(@mentioner, @mentionable).once
63
+ @mentioner.mentions?(@mentionable)
64
+ end
65
+ end
66
+
67
+ context "#mentionables" do
68
+ should "call $Mention.mentionables" do
69
+ $Mention.expects(:mentionables).with(@mentioner, @mentionable.class, { :foo => :bar })
70
+ @mentioner.mentionables(@mentionable.class, { :foo => :bar })
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
2
+
3
+ class ActiveRecordFollowStoreTest < Test::Unit::TestCase
4
+ context "ActiveRecordStores::FollowStoreTest" do
5
+ setup do
6
+ @klass = Socialization::ActiveRecordStores::FollowStore
7
+ @klass.touch nil
8
+ @klass.after_follow nil
9
+ @klass.after_unfollow nil
10
+ @follower = ImAFollower.create
11
+ @followable = ImAFollowable.create
12
+ end
13
+
14
+ context "data store" do
15
+ should "inherit Socialization::ActiveRecordStores::FollowStore" do
16
+ assert_equal Socialization::ActiveRecordStores::FollowStore, Socialization.follow_model
17
+ end
18
+ end
19
+
20
+ context "#follow!" do
21
+ should "create a Follow record" do
22
+ @klass.follow!(@follower, @followable)
23
+ assert_match_follower @klass.last, @follower
24
+ assert_match_followable @klass.last, @followable
25
+ end
26
+
27
+ should "touch follower when instructed" do
28
+ @klass.touch :follower
29
+ @follower.expects(:touch).once
30
+ @followable.expects(:touch).never
31
+ @klass.follow!(@follower, @followable)
32
+ end
33
+
34
+ should "touch followable when instructed" do
35
+ @klass.touch :followable
36
+ @follower.expects(:touch).never
37
+ @followable.expects(:touch).once
38
+ @klass.follow!(@follower, @followable)
39
+ end
40
+
41
+ should "touch all when instructed" do
42
+ @klass.touch :all
43
+ @follower.expects(:touch).once
44
+ @followable.expects(:touch).once
45
+ @klass.follow!(@follower, @followable)
46
+ end
47
+
48
+ should "call after follow hook" do
49
+ @klass.after_follow :after_follow
50
+ @klass.expects(:after_follow).once
51
+ @klass.follow!(@follower, @followable)
52
+ end
53
+
54
+ should "call after unfollow hook" do
55
+ @klass.after_follow :after_unfollow
56
+ @klass.expects(:after_unfollow).once
57
+ @klass.follow!(@follower, @followable)
58
+ end
59
+ end
60
+
61
+ context "#follows?" do
62
+ should "return true when follow exists" do
63
+ @klass.create! do |f|
64
+ f.follower = @follower
65
+ f.followable = @followable
66
+ end
67
+ assert_true @klass.follows?(@follower, @followable)
68
+ end
69
+
70
+ should "return false when follow doesn't exist" do
71
+ assert_false @klass.follows?(@follower, @followable)
72
+ end
73
+ end
74
+
75
+ context "#followers" do
76
+ should "return an array of followers" do
77
+ follower1 = ImAFollower.create
78
+ follower2 = ImAFollower.create
79
+ follower1.follow!(@followable)
80
+ follower2.follow!(@followable)
81
+ assert_equal [follower1, follower2], @klass.followers(@followable, follower1.class)
82
+ end
83
+
84
+ should "return an array of follower ids when plucking" do
85
+ follower1 = ImAFollower.create
86
+ follower2 = ImAFollower.create
87
+ follower1.follow!(@followable)
88
+ follower2.follow!(@followable)
89
+ assert_equal [follower1.id, follower2.id], @klass.followers(@followable, follower1.class, :pluck => :id)
90
+ end
91
+ end
92
+
93
+ context "#followables" do
94
+ should "return an array of followers" do
95
+ followable1 = ImAFollowable.create
96
+ followable2 = ImAFollowable.create
97
+ @follower.follow!(followable1)
98
+ @follower.follow!(followable2)
99
+ assert_equal [followable1, followable2], @klass.followables(@follower, followable1.class)
100
+ end
101
+
102
+ should "return an array of follower ids when plucking" do
103
+ followable1 = ImAFollowable.create
104
+ followable2 = ImAFollowable.create
105
+ @follower.follow!(followable1)
106
+ @follower.follow!(followable2)
107
+ assert_equal [followable1.id, followable2.id], @klass.followables(@follower, followable1.class, :pluck => :id)
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ # Helpers
114
+ def assert_match_follower(follow_record, follower)
115
+ assert follow_record.follower_type == follower.class.to_s && follow_record.follower_id == follower.id
116
+ end
117
+
118
+ def assert_match_followable(follow_record, followable)
119
+ assert follow_record.followable_type == followable.class.to_s && follow_record.followable_id == followable.id
120
+ end
121
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
2
+
3
+ class ActiveRecordLikeStoreTest < Test::Unit::TestCase
4
+ context "ActiveRecordStore::LikeStoreTest" do
5
+ setup do
6
+ @klass = Socialization::ActiveRecordStores::LikeStore
7
+ @klass.touch nil
8
+ @klass.after_like nil
9
+ @klass.after_unlike nil
10
+ @liker = ImALiker.create
11
+ @likeable = ImALikeable.create
12
+ end
13
+
14
+ context "data store" do
15
+ should "inherit Socialization::ActiveRecordStores::LikeStore" do
16
+ assert_equal Socialization::ActiveRecordStores::LikeStore, Socialization.like_model
17
+ end
18
+ end
19
+
20
+ context "#like!" do
21
+ should "create a Like record" do
22
+ @klass.like!(@liker, @likeable)
23
+ assert_match_liker @klass.last, @liker
24
+ assert_match_likeable @klass.last, @likeable
25
+ end
26
+
27
+ should "touch liker when instructed" do
28
+ @klass.touch :liker
29
+ @liker.expects(:touch).once
30
+ @likeable.expects(:touch).never
31
+ @klass.like!(@liker, @likeable)
32
+ end
33
+
34
+ should "touch likeable when instructed" do
35
+ @klass.touch :likeable
36
+ @liker.expects(:touch).never
37
+ @likeable.expects(:touch).once
38
+ @klass.like!(@liker, @likeable)
39
+ end
40
+
41
+ should "touch all when instructed" do
42
+ @klass.touch :all
43
+ @liker.expects(:touch).once
44
+ @likeable.expects(:touch).once
45
+ @klass.like!(@liker, @likeable)
46
+ end
47
+
48
+ should "call after like hook" do
49
+ @klass.after_like :after_like
50
+ @klass.expects(:after_like).once
51
+ @klass.like!(@liker, @likeable)
52
+ end
53
+
54
+ should "call after unlike hook" do
55
+ @klass.after_like :after_unlike
56
+ @klass.expects(:after_unlike).once
57
+ @klass.like!(@liker, @likeable)
58
+ end
59
+ end
60
+
61
+ context "#likes?" do
62
+ should "return true when like exists" do
63
+ @klass.create! do |f|
64
+ f.liker = @liker
65
+ f.likeable = @likeable
66
+ end
67
+ assert_true @klass.likes?(@liker, @likeable)
68
+ end
69
+
70
+ should "return false when like doesn't exist" do
71
+ assert_false @klass.likes?(@liker, @likeable)
72
+ end
73
+ end
74
+
75
+ context "#likers" do
76
+ should "return an array of likers" do
77
+ liker1 = ImALiker.create
78
+ liker2 = ImALiker.create
79
+ liker1.like!(@likeable)
80
+ liker2.like!(@likeable)
81
+ assert_equal [liker1, liker2], @klass.likers(@likeable, liker1.class)
82
+ end
83
+
84
+ should "return an array of liker ids when plucking" do
85
+ liker1 = ImALiker.create
86
+ liker2 = ImALiker.create
87
+ liker1.like!(@likeable)
88
+ liker2.like!(@likeable)
89
+ assert_equal [liker1.id, liker2.id], @klass.likers(@likeable, liker1.class, :pluck => :id)
90
+ end
91
+ end
92
+
93
+ context "#likeables" do
94
+ should "return an array of likers" do
95
+ likeable1 = ImALikeable.create
96
+ likeable2 = ImALikeable.create
97
+ @liker.like!(likeable1)
98
+ @liker.like!(likeable2)
99
+ assert_equal [likeable1, likeable2], @klass.likeables(@liker, likeable1.class)
100
+ end
101
+
102
+ should "return an array of liker ids when plucking" do
103
+ likeable1 = ImALikeable.create
104
+ likeable2 = ImALikeable.create
105
+ @liker.like!(likeable1)
106
+ @liker.like!(likeable2)
107
+ assert_equal [likeable1.id, likeable2.id], @klass.likeables(@liker, likeable1.class, :pluck => :id)
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ # Helpers
114
+ def assert_match_liker(like_record, liker)
115
+ assert like_record.liker_type == liker.class.to_s && like_record.liker_id == liker.id
116
+ end
117
+
118
+ def assert_match_likeable(like_record, likeable)
119
+ assert like_record.likeable_type == likeable.class.to_s && like_record.likeable_id == likeable.id
120
+ end
121
+ end