twrk-socialization 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +8 -0
  5. data/Appraisals +23 -0
  6. data/CHANGELOG.md +67 -0
  7. data/Gemfile +3 -0
  8. data/Guardfile +12 -0
  9. data/LICENSE +20 -0
  10. data/README.md +298 -0
  11. data/Rakefile +14 -0
  12. data/demo/.gitignore +15 -0
  13. data/demo/Gemfile +35 -0
  14. data/demo/README.rdoc +261 -0
  15. data/demo/Rakefile +7 -0
  16. data/demo/app/assets/images/rails.png +0 -0
  17. data/demo/app/assets/javascripts/application.js +15 -0
  18. data/demo/app/assets/stylesheets/application.css +13 -0
  19. data/demo/app/controllers/application_controller.rb +3 -0
  20. data/demo/app/helpers/application_helper.rb +2 -0
  21. data/demo/app/mailers/.gitkeep +0 -0
  22. data/demo/app/models/.gitkeep +0 -0
  23. data/demo/app/models/celebrity.rb +4 -0
  24. data/demo/app/models/comment.rb +5 -0
  25. data/demo/app/models/follow.rb +3 -0
  26. data/demo/app/models/like.rb +2 -0
  27. data/demo/app/models/mention.rb +2 -0
  28. data/demo/app/models/movie.rb +3 -0
  29. data/demo/app/models/user.rb +7 -0
  30. data/demo/app/views/layouts/application.html.erb +14 -0
  31. data/demo/config/application.rb +59 -0
  32. data/demo/config/boot.rb +6 -0
  33. data/demo/config/database.yml +25 -0
  34. data/demo/config/environment.rb +5 -0
  35. data/demo/config/environments/development.rb +37 -0
  36. data/demo/config/environments/production.rb +67 -0
  37. data/demo/config/environments/test.rb +37 -0
  38. data/demo/config/initializers/backtrace_silencers.rb +7 -0
  39. data/demo/config/initializers/inflections.rb +15 -0
  40. data/demo/config/initializers/mime_types.rb +5 -0
  41. data/demo/config/initializers/secret_token.rb +7 -0
  42. data/demo/config/initializers/session_store.rb +8 -0
  43. data/demo/config/initializers/wrap_parameters.rb +14 -0
  44. data/demo/config/locales/en.yml +5 -0
  45. data/demo/config/routes.rb +58 -0
  46. data/demo/config.ru +4 -0
  47. data/demo/db/migrate/20120115051222_create_users.rb +9 -0
  48. data/demo/db/migrate/20120115051234_create_movies.rb +9 -0
  49. data/demo/db/migrate/20120115051255_create_celebrities.rb +9 -0
  50. data/demo/db/migrate/20120115054646_create_follows.rb +14 -0
  51. data/demo/db/migrate/20120115054647_create_likes.rb +14 -0
  52. data/demo/db/migrate/20120221200644_create_mentions.rb +14 -0
  53. data/demo/db/migrate/20120221202703_create_comments.rb +9 -0
  54. data/demo/db/schema.rb +72 -0
  55. data/demo/db/seeds.rb +17 -0
  56. data/demo/lib/assets/.gitkeep +0 -0
  57. data/demo/lib/tasks/.gitkeep +0 -0
  58. data/demo/public/404.html +26 -0
  59. data/demo/public/422.html +26 -0
  60. data/demo/public/500.html +25 -0
  61. data/demo/public/favicon.ico +0 -0
  62. data/demo/public/index.html +204 -0
  63. data/demo/public/robots.txt +5 -0
  64. data/demo/script/rails +6 -0
  65. data/demo/test/fixtures/.gitkeep +0 -0
  66. data/demo/test/fixtures/celebrities.yml +7 -0
  67. data/demo/test/fixtures/movies.yml +7 -0
  68. data/demo/test/fixtures/users.yml +7 -0
  69. data/demo/test/functional/.gitkeep +0 -0
  70. data/demo/test/integration/.gitkeep +0 -0
  71. data/demo/test/performance/browsing_test.rb +12 -0
  72. data/demo/test/test_helper.rb +13 -0
  73. data/demo/test/unit/.gitkeep +0 -0
  74. data/demo/test/unit/celebrity_test.rb +7 -0
  75. data/demo/test/unit/movie_test.rb +7 -0
  76. data/demo/test/unit/user_test.rb +7 -0
  77. data/demo/vendor/assets/javascripts/.gitkeep +0 -0
  78. data/demo/vendor/assets/stylesheets/.gitkeep +0 -0
  79. data/demo/vendor/plugins/.gitkeep +0 -0
  80. data/init.rb +1 -0
  81. data/lib/generators/socialization/socialization_generator.rb +38 -0
  82. data/lib/generators/socialization/templates/active_record/migration_follows.rb +14 -0
  83. data/lib/generators/socialization/templates/active_record/migration_likes.rb +14 -0
  84. data/lib/generators/socialization/templates/active_record/migration_mentions.rb +14 -0
  85. data/lib/generators/socialization/templates/active_record/model_follow.rb +2 -0
  86. data/lib/generators/socialization/templates/active_record/model_like.rb +2 -0
  87. data/lib/generators/socialization/templates/active_record/model_mention.rb +2 -0
  88. data/lib/generators/socialization/templates/redis/model_follow.rb +2 -0
  89. data/lib/generators/socialization/templates/redis/model_like.rb +2 -0
  90. data/lib/generators/socialization/templates/redis/model_mention.rb +2 -0
  91. data/lib/socialization/actors/follower.rb +88 -0
  92. data/lib/socialization/actors/liker.rb +88 -0
  93. data/lib/socialization/actors/mentioner.rb +88 -0
  94. data/lib/socialization/config/config.rb +39 -0
  95. data/lib/socialization/helpers/acts_as_helpers.rb +39 -0
  96. data/lib/socialization/helpers/string.rb +17 -0
  97. data/lib/socialization/lib/exceptions.rb +3 -0
  98. data/lib/socialization/stores/active_record/follow.rb +125 -0
  99. data/lib/socialization/stores/active_record/like.rb +121 -0
  100. data/lib/socialization/stores/active_record/mention.rb +125 -0
  101. data/lib/socialization/stores/active_record/mixins/base.rb +12 -0
  102. data/lib/socialization/stores/mixins/base.rb +22 -0
  103. data/lib/socialization/stores/mixins/follow.rb +39 -0
  104. data/lib/socialization/stores/mixins/like.rb +40 -0
  105. data/lib/socialization/stores/mixins/mention.rb +40 -0
  106. data/lib/socialization/stores/redis/base.rb +125 -0
  107. data/lib/socialization/stores/redis/config.rb +11 -0
  108. data/lib/socialization/stores/redis/follow.rb +22 -0
  109. data/lib/socialization/stores/redis/like.rb +22 -0
  110. data/lib/socialization/stores/redis/mention.rb +22 -0
  111. data/lib/socialization/stores/redis/mixins/base.rb +8 -0
  112. data/lib/socialization/version.rb +3 -0
  113. data/lib/socialization/victims/followable.rb +50 -0
  114. data/lib/socialization/victims/likeable.rb +51 -0
  115. data/lib/socialization/victims/mentionable.rb +51 -0
  116. data/lib/socialization.rb +20 -0
  117. data/socialization.gemspec +32 -0
  118. data/spec/actors/follower_spec.rb +113 -0
  119. data/spec/actors/liker_spec.rb +105 -0
  120. data/spec/actors/mentioner_spec.rb +105 -0
  121. data/spec/spec_helper.rb +21 -0
  122. data/spec/spec_support/data_stores.rb +232 -0
  123. data/spec/spec_support/matchers.rb +54 -0
  124. data/spec/stores/active_record/follow_store_spec.rb +147 -0
  125. data/spec/stores/active_record/like_store_spec.rb +146 -0
  126. data/spec/stores/active_record/mention_store_spec.rb +148 -0
  127. data/spec/stores/active_record/mixins/base_spec.rb +25 -0
  128. data/spec/stores/redis/base_spec.rb +195 -0
  129. data/spec/stores/redis/config_spec.rb +28 -0
  130. data/spec/stores/redis/follow_store_spec.rb +26 -0
  131. data/spec/stores/redis/like_store_spec.rb +23 -0
  132. data/spec/stores/redis/mention_store_spec.rb +23 -0
  133. data/spec/string_spec.rb +13 -0
  134. data/spec/victims/followable_spec.rb +59 -0
  135. data/spec/victims/likeable_spec.rb +58 -0
  136. data/spec/victims/mentionable_spec.rb +59 -0
  137. data/spec/world_spec.rb +107 -0
  138. metadata +322 -0
@@ -0,0 +1,232 @@
1
+ require 'mock_redis' if $MOCK_REDIS
2
+ require 'redis' unless $MOCK_REDIS
3
+
4
+ silence_warnings do
5
+ Redis = MockRedis if $MOCK_REDIS # Magic!
6
+ end
7
+
8
+ def use_redis_store
9
+ Socialization.follow_model = Socialization::RedisStores::Follow
10
+ Socialization.mention_model = Socialization::RedisStores::Mention
11
+ Socialization.like_model = Socialization::RedisStores::Like
12
+ setup_model_shortcuts
13
+ end
14
+
15
+ def use_ar_store
16
+ Socialization.follow_model = Socialization::ActiveRecordStores::Follow
17
+ Socialization.mention_model = Socialization::ActiveRecordStores::Mention
18
+ Socialization.like_model = Socialization::ActiveRecordStores::Like
19
+ setup_model_shortcuts
20
+ end
21
+
22
+ def setup_model_shortcuts
23
+ $Follow = Socialization.follow_model
24
+ $Mention = Socialization.mention_model
25
+ $Like = Socialization.like_model
26
+ end
27
+
28
+ def clear_redis
29
+ Socialization.redis.keys(nil).each do |k|
30
+ Socialization.redis.del k
31
+ end
32
+ end
33
+
34
+ ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
35
+ ActiveRecord::Base.establish_connection(:sqlite3)
36
+
37
+ ActiveRecord::Base.logger = Logger.new(STDERR)
38
+ ActiveRecord::Base.logger.level = Logger::WARN
39
+
40
+ ActiveRecord::Migration.verbose = false
41
+ ActiveRecord::Schema.define(:version => 0) do
42
+ create_table :users do |t|
43
+ t.string :name
44
+ end
45
+
46
+ create_table :celebrities do |t|
47
+ t.string :name
48
+ end
49
+
50
+ create_table :movies do |t|
51
+ t.string :name
52
+ end
53
+
54
+ create_table :comments do |t|
55
+ t.integer :user_id
56
+ t.integer :movie_id
57
+ t.string :body
58
+ end
59
+
60
+ create_table :follows do |t|
61
+ t.string :follower_type
62
+ t.integer :follower_id
63
+ t.string :followable_type
64
+ t.integer :followable_id
65
+ t.datetime :created_at
66
+ end
67
+
68
+ create_table :likes do |t|
69
+ t.string :liker_type
70
+ t.integer :liker_id
71
+ t.string :likeable_type
72
+ t.integer :likeable_id
73
+ t.datetime :created_at
74
+ end
75
+
76
+ create_table :mentions do |t|
77
+ t.string :mentioner_type
78
+ t.integer :mentioner_id
79
+ t.string :mentionable_type
80
+ t.integer :mentionable_id
81
+ t.datetime :created_at
82
+ end
83
+
84
+ create_table :im_a_followers do |t|
85
+ t.timestamps null: true
86
+ end
87
+
88
+ create_table :im_a_follower_with_counter_caches do |t|
89
+ t.integer :followees_count, default: 0
90
+ t.timestamps null: true
91
+ end
92
+
93
+ create_table :im_a_followables do |t|
94
+ t.timestamps null: true
95
+ end
96
+
97
+ create_table :im_a_followable_with_counter_caches do |t|
98
+ t.integer :followers_count, default: 0
99
+ t.timestamps null: true
100
+ end
101
+
102
+ create_table :im_a_likers do |t|
103
+ t.timestamps null: true
104
+ end
105
+
106
+ create_table :im_a_liker_with_counter_caches do |t|
107
+ t.integer :likees_count, default: 0
108
+ t.timestamps null: true
109
+ end
110
+
111
+ create_table :im_a_likeables do |t|
112
+ t.timestamps null: true
113
+ end
114
+
115
+ create_table :im_a_likeable_with_counter_caches do |t|
116
+ t.integer :likers_count, default: 0
117
+ t.timestamps null: true
118
+ end
119
+
120
+ create_table :im_a_mentioners do |t|
121
+ t.timestamps null: true
122
+ end
123
+
124
+ create_table :im_a_mentioner_with_counter_caches do |t|
125
+ t.integer :mentionees_count, default: 0
126
+ t.timestamps null: true
127
+ end
128
+
129
+ create_table :im_a_mentionables do |t|
130
+ t.timestamps null: true
131
+ end
132
+
133
+ create_table :im_a_mentionable_with_counter_caches do |t|
134
+ t.integer :mentioners_count, default: 0
135
+ t.timestamps null: true
136
+ end
137
+
138
+ create_table :im_a_mentioner_and_mentionables do |t|
139
+ t.timestamps null: true
140
+ end
141
+
142
+ create_table :vanillas do |t|
143
+ t.timestamps null: true
144
+ end
145
+ end
146
+
147
+ class ::Celebrity < ActiveRecord::Base
148
+ acts_as_followable
149
+ acts_as_mentionable
150
+ end
151
+
152
+ class ::User < ActiveRecord::Base
153
+ acts_as_follower
154
+ acts_as_followable
155
+ acts_as_liker
156
+ acts_as_likeable
157
+ acts_as_mentionable
158
+
159
+ has_many :comments
160
+ end
161
+
162
+ class ::Comment < ActiveRecord::Base
163
+ acts_as_mentioner
164
+ belongs_to :user
165
+ belongs_to :movie
166
+ end
167
+
168
+ class ::Movie < ActiveRecord::Base
169
+ acts_as_likeable
170
+ has_many :comments
171
+ end
172
+
173
+ # class Follow < Socialization::ActiveRecordStores::Follow; end
174
+ # class Like < Socialization::ActiveRecordStores::Like; end
175
+ # class Mention < Socialization::ActiveRecordStores::Mention; end
176
+
177
+ class ::ImAFollower < ActiveRecord::Base
178
+ acts_as_follower
179
+ end
180
+ class ::ImAFollowerWithCounterCache < ActiveRecord::Base
181
+ acts_as_follower
182
+ end
183
+ class ::ImAFollowerChild < ImAFollower; end
184
+
185
+ class ::ImAFollowable < ActiveRecord::Base
186
+ acts_as_followable
187
+ end
188
+ class ::ImAFollowableWithCounterCache < ActiveRecord::Base
189
+ acts_as_followable
190
+ end
191
+ class ::ImAFollowableChild < ImAFollowable; end
192
+
193
+ class ::ImALiker < ActiveRecord::Base
194
+ acts_as_liker
195
+ end
196
+ class ::ImALikerWithCounterCache < ActiveRecord::Base
197
+ acts_as_liker
198
+ end
199
+ class ::ImALikerChild < ImALiker; end
200
+
201
+ class ::ImALikeable < ActiveRecord::Base
202
+ acts_as_likeable
203
+ end
204
+ class ::ImALikeableWithCounterCache < ActiveRecord::Base
205
+ acts_as_likeable
206
+ end
207
+ class ::ImALikeableChild < ImALikeable; end
208
+
209
+ class ::ImAMentioner < ActiveRecord::Base
210
+ acts_as_mentioner
211
+ end
212
+ class ::ImAMentionerWithCounterCache < ActiveRecord::Base
213
+ acts_as_mentioner
214
+ end
215
+ class ::ImAMentionerChild < ImAMentioner; end
216
+
217
+ class ::ImAMentionable < ActiveRecord::Base
218
+ acts_as_mentionable
219
+ end
220
+ class ::ImAMentionableWithCounterCache < ActiveRecord::Base
221
+ acts_as_mentionable
222
+ end
223
+ class ::ImAMentionableChild < ImAMentionable; end
224
+
225
+ class ::ImAMentionerAndMentionable < ActiveRecord::Base
226
+ acts_as_mentioner
227
+ acts_as_mentionable
228
+ end
229
+
230
+ class ::Vanilla < ActiveRecord::Base
231
+ end
232
+
@@ -0,0 +1,54 @@
1
+ RSpec::Matchers.define :be_a_public_method_of do |obj|
2
+ match do |actual|
3
+ method = RUBY_VERSION.match(/^1\.8/) ? actual.to_s : actual.to_s.to_sym
4
+ obj.public_methods.include?(method)
5
+ end
6
+
7
+ failure_message do |actual|
8
+ "expected that #{actual} would be a public method of #{actual.class}"
9
+ end
10
+
11
+ failure_message_when_negated do |actual|
12
+ "expected that #{actual} would not be a public method of #{actual.class}"
13
+ end
14
+
15
+ description do
16
+ "be a public method"
17
+ end
18
+ end
19
+
20
+ RSpec::Matchers.define :match_follower do |expected|
21
+ match do |actual|
22
+ expected.follower_type == actual.class.to_s && expected.follower_id == actual.id
23
+ end
24
+ end
25
+
26
+ RSpec::Matchers.define :match_followable do |expected|
27
+ match do |actual|
28
+ expected.followable_type == actual.class.to_s && expected.followable_id == actual.id
29
+ end
30
+ end
31
+
32
+ RSpec::Matchers.define :match_liker do |expected|
33
+ match do |actual|
34
+ expected.liker_type == actual.class.to_s && expected.liker_id == actual.id
35
+ end
36
+ end
37
+
38
+ RSpec::Matchers.define :match_likeable do |expected|
39
+ match do |actual|
40
+ expected.likeable_type == actual.class.to_s && expected.likeable_id == actual.id
41
+ end
42
+ end
43
+
44
+ RSpec::Matchers.define :match_mentioner do |expected|
45
+ match do |actual|
46
+ expected.mentioner_type == actual.class.to_s && expected.mentioner_id == actual.id
47
+ end
48
+ end
49
+
50
+ RSpec::Matchers.define :match_mentionable do |expected|
51
+ match do |actual|
52
+ expected.mentionable_type == actual.class.to_s && expected.mentionable_id == actual.id
53
+ end
54
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe Socialization::ActiveRecordStores::Follow do
4
+ before do
5
+ @klass = Socialization::ActiveRecordStores::Follow
6
+ @klass.touch nil
7
+ @klass.after_follow nil
8
+ @klass.after_unfollow nil
9
+ @follower = ImAFollower.create
10
+ @followable = ImAFollowable.create
11
+ end
12
+
13
+ describe "data store" do
14
+ it "inherits Socialization::ActiveRecordStores::Follow" do
15
+ expect(Socialization.follow_model).to eq(Socialization::ActiveRecordStores::Follow)
16
+ end
17
+ end
18
+
19
+ describe "#follow!" do
20
+ it "creates a Follow record" do
21
+ @klass.follow!(@follower, @followable)
22
+ expect(@follower).to match_follower(@klass.last)
23
+ expect(@followable).to match_followable(@klass.last)
24
+ end
25
+
26
+ it "increments counter caches" do
27
+ follower = ImAFollowerWithCounterCache.create
28
+ followable = ImAFollowableWithCounterCache.create
29
+ @klass.follow!(follower, followable)
30
+ expect(follower.reload.followees_count).to eq(1)
31
+ expect(followable.reload.followers_count).to eq(1)
32
+ end
33
+
34
+ it "touches follower when instructed" do
35
+ @klass.touch :follower
36
+ expect(@follower).to receive(:touch).once
37
+ expect(@followable).to receive(:touch).never
38
+ @klass.follow!(@follower, @followable)
39
+ end
40
+
41
+ it "touches followable when instructed" do
42
+ @klass.touch :followable
43
+ expect(@follower).to receive(:touch).never
44
+ expect(@followable).to receive(:touch).once
45
+ @klass.follow!(@follower, @followable)
46
+ end
47
+
48
+ it "touches all when instructed" do
49
+ @klass.touch :all
50
+ expect(@follower).to receive(:touch).once
51
+ expect(@followable).to receive(:touch).once
52
+ @klass.follow!(@follower, @followable)
53
+ end
54
+
55
+ it "calls after follow hook" do
56
+ @klass.after_follow :after_follow
57
+ expect(@klass).to receive(:after_follow).once
58
+ @klass.follow!(@follower, @followable)
59
+ end
60
+
61
+ it "calls after unfollow hook" do
62
+ @klass.after_follow :after_unfollow
63
+ expect(@klass).to receive(:after_unfollow).once
64
+ @klass.follow!(@follower, @followable)
65
+ end
66
+ end
67
+
68
+ describe "#unfollow!" do
69
+ it "decrements counter caches" do
70
+ follower = ImAFollowerWithCounterCache.create
71
+ followable = ImAFollowableWithCounterCache.create
72
+ @klass.follow!(follower, followable)
73
+ @klass.unfollow!(follower, followable)
74
+ expect(follower.reload.followees_count).to eq(0)
75
+ expect(followable.reload.followers_count).to eq(0)
76
+ end
77
+ end
78
+
79
+ describe "#follows?" do
80
+ it "returns true when follow exists" do
81
+ @klass.create! do |f|
82
+ f.follower = @follower
83
+ f.followable = @followable
84
+ end
85
+ expect(@klass.follows?(@follower, @followable)).to be true
86
+ end
87
+
88
+ it "returns false when follow doesn't exist" do
89
+ expect(@klass.follows?(@follower, @followable)).to be false
90
+ end
91
+ end
92
+
93
+ describe "#followers" do
94
+ it "returns an array of followers" do
95
+ follower1 = ImAFollower.create
96
+ follower2 = ImAFollower.create
97
+ follower1.follow!(@followable)
98
+ follower2.follow!(@followable)
99
+ expect(@klass.followers(@followable, follower1.class)).to eq([follower1, follower2])
100
+ end
101
+
102
+ it "returns an array of follower ids when plucking" do
103
+ follower1 = ImAFollower.create
104
+ follower2 = ImAFollower.create
105
+ follower1.follow!(@followable)
106
+ follower2.follow!(@followable)
107
+ expect(@klass.followers(@followable, follower1.class, :pluck => :id)).to eq([follower1.id, follower2.id])
108
+ end
109
+ end
110
+
111
+ describe "#followables" do
112
+ it "returns an array of followers" do
113
+ followable1 = ImAFollowable.create
114
+ followable2 = ImAFollowable.create
115
+ @follower.follow!(followable1)
116
+ @follower.follow!(followable2)
117
+ expect(@klass.followables(@follower, followable1.class)).to eq([followable1, followable2])
118
+ end
119
+
120
+ it "returns an array of follower ids when plucking" do
121
+ followable1 = ImAFollowable.create
122
+ followable2 = ImAFollowable.create
123
+ @follower.follow!(followable1)
124
+ @follower.follow!(followable2)
125
+ expect(@klass.followables(@follower, followable1.class, :pluck => :id)).to eq([followable1.id, followable2.id])
126
+ end
127
+ end
128
+
129
+ describe "#remove_followers" do
130
+ it "deletes all followers relationships for a followable" do
131
+ @follower.follow!(@followable)
132
+ expect(@followable.followers(@follower.class).count).to eq(1)
133
+ @klass.remove_followers(@followable)
134
+ expect(@followable.followers(@follower.class).count).to eq(0)
135
+ end
136
+ end
137
+
138
+ describe "#remove_followables" do
139
+ it "deletes all followables relationships for a follower" do
140
+ @follower.follow!(@followable)
141
+ expect(@follower.followables(@followable.class).count).to eq(1)
142
+ @klass.remove_followables(@follower)
143
+ expect(@follower.followables(@followable.class).count).to eq(0)
144
+ end
145
+ end
146
+ end
147
+
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+
3
+ describe Socialization::ActiveRecordStores::Like do
4
+ before do
5
+ @klass = Socialization::ActiveRecordStores::Like
6
+ @klass.touch nil
7
+ @klass.after_like nil
8
+ @klass.after_unlike nil
9
+ @liker = ImALiker.create
10
+ @likeable = ImALikeable.create
11
+ end
12
+
13
+ describe "data store" do
14
+ it "inherits Socialization::ActiveRecordStores::Like" do
15
+ expect(Socialization.like_model).to eq Socialization::ActiveRecordStores::Like
16
+ end
17
+ end
18
+
19
+ describe "#like!" do
20
+ it "creates a Like record" do
21
+ @klass.like!(@liker, @likeable)
22
+ expect(@liker).to match_liker @klass.last
23
+ expect(@likeable).to match_likeable @klass.last
24
+ end
25
+
26
+ it "increments counter caches" do
27
+ liker = ImALikerWithCounterCache.create
28
+ likeable = ImALikeableWithCounterCache.create
29
+ @klass.like!(liker, likeable)
30
+ expect(liker.reload.likees_count).to eq(1)
31
+ expect(likeable.reload.likers_count).to eq(1)
32
+ end
33
+
34
+ it "touches liker when instructed" do
35
+ @klass.touch :liker
36
+ expect(@liker).to receive(:touch).once
37
+ expect(@likeable).to receive(:touch).never
38
+ @klass.like!(@liker, @likeable)
39
+ end
40
+
41
+ it "touches likeable when instructed" do
42
+ @klass.touch :likeable
43
+ expect(@liker).to receive(:touch).never
44
+ expect(@likeable).to receive(:touch).once
45
+ @klass.like!(@liker, @likeable)
46
+ end
47
+
48
+ it "touches all when instructed" do
49
+ @klass.touch :all
50
+ expect(@liker).to receive(:touch).once
51
+ expect(@likeable).to receive(:touch).once
52
+ @klass.like!(@liker, @likeable)
53
+ end
54
+
55
+ it "calls after like hook" do
56
+ @klass.after_like :after_like
57
+ expect(@klass).to receive(:after_like).once
58
+ @klass.like!(@liker, @likeable)
59
+ end
60
+
61
+ it "calls after unlike hook" do
62
+ @klass.after_like :after_unlike
63
+ expect(@klass).to receive(:after_unlike).once
64
+ @klass.like!(@liker, @likeable)
65
+ end
66
+ end
67
+
68
+ describe "#unlike!" do
69
+ it "decrements counter caches" do
70
+ liker = ImALikerWithCounterCache.create
71
+ likeable = ImALikeableWithCounterCache.create
72
+ @klass.like!(liker, likeable)
73
+ @klass.unlike!(liker, likeable)
74
+ expect(liker.reload.likees_count).to eq 0
75
+ expect(likeable.reload.likers_count).to eq 0
76
+ end
77
+ end
78
+
79
+ describe "#likes?" do
80
+ it "returns true when like exists" do
81
+ @klass.create! do |f|
82
+ f.liker = @liker
83
+ f.likeable = @likeable
84
+ end
85
+ expect(@klass.likes?(@liker, @likeable)).to be true
86
+ end
87
+
88
+ it "returns false when like doesn't exist" do
89
+ expect(@klass.likes?(@liker, @likeable)).to be false
90
+ end
91
+ end
92
+
93
+ describe "#likers" do
94
+ it "returns an array of likers" do
95
+ liker1 = ImALiker.create
96
+ liker2 = ImALiker.create
97
+ liker1.like!(@likeable)
98
+ liker2.like!(@likeable)
99
+ expect(@klass.likers(@likeable, liker1.class)).to eq [liker1, liker2]
100
+ end
101
+
102
+ it "returns an array of liker ids when plucking" do
103
+ liker1 = ImALiker.create
104
+ liker2 = ImALiker.create
105
+ liker1.like!(@likeable)
106
+ liker2.like!(@likeable)
107
+ expect(@klass.likers(@likeable, liker1.class, :pluck => :id)).to eq [liker1.id, liker2.id]
108
+ end
109
+ end
110
+
111
+ describe "#likeables" do
112
+ it "returns an array of likers" do
113
+ likeable1 = ImALikeable.create
114
+ likeable2 = ImALikeable.create
115
+ @liker.like!(likeable1)
116
+ @liker.like!(likeable2)
117
+ expect(@klass.likeables(@liker, likeable1.class)).to eq [likeable1, likeable2]
118
+ end
119
+
120
+ it "returns an array of liker ids when plucking" do
121
+ likeable1 = ImALikeable.create
122
+ likeable2 = ImALikeable.create
123
+ @liker.like!(likeable1)
124
+ @liker.like!(likeable2)
125
+ expect(@klass.likeables(@liker, likeable1.class, :pluck => :id)).to eq [likeable1.id, likeable2.id]
126
+ end
127
+ end
128
+
129
+ describe "#remove_likers" do
130
+ it "deletes all likers relationships for a likeable" do
131
+ @liker.like!(@likeable)
132
+ expect(@likeable.likers(@liker.class).count).to eq 1
133
+ @klass.remove_likers(@likeable)
134
+ expect(@likeable.likers(@liker.class).count).to eq 0
135
+ end
136
+ end
137
+
138
+ describe "#remove_likeables" do
139
+ it "deletes all likeables relationships for a liker" do
140
+ @liker.like!(@likeable)
141
+ expect(@liker.likeables(@likeable.class).count).to eq 1
142
+ @klass.remove_likeables(@liker)
143
+ expect(@liker.likeables(@likeable.class).count).to eq 0
144
+ end
145
+ end
146
+ end