acts_as_follower1 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +6 -0
  5. data/MIT-LICENSE +44 -0
  6. data/README.rdoc +247 -0
  7. data/Rakefile +37 -0
  8. data/acts_as_follower.gemspec +29 -0
  9. data/init.rb +1 -0
  10. data/lib/acts_as_follower/follow_scopes.rb +45 -0
  11. data/lib/acts_as_follower/followable.rb +113 -0
  12. data/lib/acts_as_follower/follower.rb +112 -0
  13. data/lib/acts_as_follower/follower_lib.rb +42 -0
  14. data/lib/acts_as_follower/railtie.rb +14 -0
  15. data/lib/acts_as_follower/version.rb +3 -0
  16. data/lib/acts_as_follower1.rb +33 -0
  17. data/lib/generators/USAGE +5 -0
  18. data/lib/generators/acts_as_follower_generator.rb +30 -0
  19. data/lib/generators/templates/migration.rb +17 -0
  20. data/lib/generators/templates/model.rb +14 -0
  21. data/test/README +24 -0
  22. data/test/acts_as_followable_test.rb +283 -0
  23. data/test/acts_as_follower_test.rb +224 -0
  24. data/test/dummy30/Gemfile +1 -0
  25. data/test/dummy30/Rakefile +7 -0
  26. data/test/dummy30/app/models/application_record.rb +3 -0
  27. data/test/dummy30/app/models/band.rb +4 -0
  28. data/test/dummy30/app/models/band/punk.rb +4 -0
  29. data/test/dummy30/app/models/band/punk/pop_punk.rb +4 -0
  30. data/test/dummy30/app/models/custom_record.rb +3 -0
  31. data/test/dummy30/app/models/some.rb +5 -0
  32. data/test/dummy30/app/models/user.rb +5 -0
  33. data/test/dummy30/config.ru +4 -0
  34. data/test/dummy30/config/application.rb +42 -0
  35. data/test/dummy30/config/boot.rb +10 -0
  36. data/test/dummy30/config/database.yml +7 -0
  37. data/test/dummy30/config/environment.rb +5 -0
  38. data/test/dummy30/config/environments/development.rb +19 -0
  39. data/test/dummy30/config/environments/test.rb +20 -0
  40. data/test/dummy30/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy30/config/initializers/inflections.rb +10 -0
  42. data/test/dummy30/config/initializers/secret_token.rb +7 -0
  43. data/test/dummy30/config/initializers/session_store.rb +8 -0
  44. data/test/dummy30/config/locales/en.yml +5 -0
  45. data/test/dummy30/config/routes.rb +2 -0
  46. data/test/factories/bands.rb +17 -0
  47. data/test/factories/somes.rb +9 -0
  48. data/test/factories/users.rb +13 -0
  49. data/test/follow_test.rb +28 -0
  50. data/test/schema.rb +25 -0
  51. data/test/test_helper.rb +18 -0
  52. metadata +213 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 221eb2a83c10938705f230ab135d2042aaa350ac
4
+ data.tar.gz: 8e644ba33f42bda3ed023da54d444057f9599dd5
5
+ SHA512:
6
+ metadata.gz: f34f11816159e50f7e8fcde852501c612d247354c323fb8f3cd605c66ac51591e93d8d8c4765dd06ef26b0f0087400472f313354070fb89045b810d7b4511251
7
+ data.tar.gz: 473d2f0f9f46a5ff8e8526534643e2cf833b46eb2cb096f25b8cd95333a3292f2c650f01f2effe24200caed8c4e9aafc4d863dcf65543d6e3ead04e32c531513
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ test/debug.log
2
+ coverage
3
+ rdoc
4
+ memory
5
+ *.gem
6
+ .rvmrc
7
+ Gemfile.lock
8
+ log
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.5
4
+ - 2.3.1
5
+ env:
6
+ matrix:
7
+ - "RAILS_VERSION=4.2.7.1"
8
+ - "RAILS_VERSION=5.0.0.1"
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_follower.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport', ENV['RAILS_VERSION'] if ENV['RAILS_VERSION']
data/MIT-LICENSE ADDED
@@ -0,0 +1,44 @@
1
+ Copyright (c) 2008 Tom Cocca
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.
21
+
22
+ The major design pattern of this plugin was abstracted from Peter Jackson's VoteFu, which is subject to the same license.
23
+ Here is the original copyright notice for VoteFu:
24
+
25
+ Copyright (c) 2008 Peter Jackson (peteonrails.com)
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining
28
+ a copy of this software and associated documentation files (the
29
+ "Software"), to deal in the Software without restriction, including
30
+ without limitation the rights to use, copy, modify, merge, publish,
31
+ distribute, sublicense, and/or sell copies of the Software, and to
32
+ permit persons to whom the Software is furnished to do so, subject to
33
+ the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be
36
+ included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
42
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
43
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
44
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,247 @@
1
+ = acts_as_follower
2
+
3
+ acts_as_follower is a gem to allow any model to follow any other model. This is accomplished through a double polymorphic relationship on the Follow model. There is also built in support for blocking/un-blocking follow records.
4
+
5
+ Main uses would be for Users to follow other Users or for Users to follow Books, etc...
6
+
7
+ (Basically, to develop the type of follow system that GitHub has)
8
+
9
+ {<img src="https://travis-ci.org/tcocca/acts_as_follower.png" />}[https://travis-ci.org/tcocca/acts_as_follower]
10
+
11
+
12
+ == Installation
13
+
14
+ === The master branch supports Rails 5
15
+
16
+ Add the gem to the gemfile:
17
+ gem 'acts_as_follower', github: 'tcocca/acts_as_follower', branch: 'master'
18
+
19
+ Other instructions same as for Rails 4.
20
+
21
+ === Rails 4.x support
22
+
23
+ The first release that support Rails 4 is 0.2.0
24
+
25
+ Add the gem to the gemfile:
26
+ gem "acts_as_follower"
27
+
28
+ Run the generator:
29
+ rails generate acts_as_follower
30
+
31
+ This will generate a migration file as well as a model called Follow.
32
+
33
+ === Rails 3.x support
34
+
35
+ Rails 3 is supports in the rails_3 branch https://github.com/tcocca/acts_as_follower/tree/rails_3
36
+ The last gem release for Rails 3 support was 0.1.1, so install the gem using ~> 0.1.1
37
+
38
+ Add the gem to the gemfile:
39
+ gem "acts_as_follower", '~> 0.1.1'
40
+
41
+ or install from the branch
42
+
43
+ gem "acts_as_follower", git: 'git://github.com/tcocca/acts_as_follower.git', branch: 'rails_3'
44
+
45
+ Run the generator:
46
+ rails generate acts_as_follower
47
+
48
+ This will generate a migration file as well as a model called Follow.
49
+
50
+ === Rails 2.3.x support
51
+
52
+ Rails 2.3.x is supported in the rails_2.3.x branch http://github.com/tcocca/acts_as_follower/tree/rails_2.3.x but must be installed as a plugin.
53
+ The gem version does not work with rails 2.3.x.
54
+
55
+ Run the following command:
56
+ script/plugin install git://github.com/tcocca/acts_as_follower.git -r rails_2.3.x
57
+
58
+ Run the generator:
59
+ script/generate acts_as_follower
60
+
61
+
62
+ == Usage
63
+
64
+ === Setup
65
+
66
+ Make your model(s) that you want to allow to be followed acts_as_followable, just add the mixin:
67
+ class User < ActiveRecord::Base
68
+ ...
69
+ acts_as_followable
70
+ ...
71
+ end
72
+
73
+ class Book < ActiveRecord::Base
74
+ ...
75
+ acts_as_followable
76
+ ...
77
+ end
78
+
79
+ Make your model(s) that can follow other models acts_as_follower
80
+ class User < ActiveRecord::Base
81
+ ...
82
+ acts_as_follower
83
+ ...
84
+ end
85
+
86
+ ---
87
+
88
+ === acts_as_follower methods
89
+
90
+ To have an object start following another use the following:
91
+ book = Book.find(1)
92
+ user = User.find(1)
93
+ user.follow(book) # Creates a record for the user as the follower and the book as the followable
94
+
95
+ To stop following an object use the following
96
+ user.stop_following(book) # Deletes that record in the Follow table
97
+
98
+ You can check to see if an object that acts_as_follower is following another object through the following:
99
+ user.following?(book) # Returns true or false
100
+
101
+ To get the total number (count) of follows for a user use the following on a model that acts_as_follower
102
+ user.follow_count # Returns an integer
103
+
104
+ To get follow records that have not been blocked use the following
105
+ user.all_follows # returns an array of Follow records
106
+
107
+ To get all of the records that an object is following that have not been blocked use the following
108
+ user.all_following
109
+ # Returns an array of every followed object for the user, this can be a collection of different object types, eg: User, Book
110
+
111
+ To get all Follow records by a certain type use the following
112
+ user.follows_by_type('Book') # returns an array of Follow objects where the followable_type is 'Book'
113
+
114
+ To get all followed objects by a certain type use the following.
115
+ user.following_by_type('Book') # Returns an array of all followed objects for user where followable_type is 'Book', this can be a collection of different object types, eg: User, Book
116
+
117
+ There is also a method_missing to accomplish the exact same thing a following_by_type('Book') to make you life easier
118
+ user.following_users # exact same results as user.following_by_type('User')
119
+
120
+ To get the count of all Follow records by a certain type use the following
121
+ user.following_by_type_count('Book') # Returns the sql count of the number of followed books by that user
122
+
123
+ There is also a method_missing to get the count by type
124
+ user.following_books_count # Calls the user.following_by_type_count('Book') method
125
+
126
+ There is now a method that will just return the Arel scope for follows so that you can chain anything else you want onto it:
127
+ book.follows_scoped
128
+ This does not return the actual follows, just the scope of followings including the followables, essentially: book.follows.unblocked.includes(:followable)
129
+
130
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
131
+ follows_by_type, all_follows, all_following, following_by_type
132
+ ---
133
+
134
+ === acts_as_followable methods
135
+
136
+ To get all the followers of a model that acts_as_followable
137
+ book.followers # Returns an array of all the followers for that book, a collection of different object types (eg. type User or type Book)
138
+
139
+ There is also a method that will just return the Arel scope for followers so that you can chain anything else you want onto it:
140
+ book.followers_scoped
141
+ This does not return the actual followers, just the scope of followings including the followers, essentially: book.followings.includes(:follower)
142
+
143
+ To get just the number of follows use
144
+ book.followers_count
145
+
146
+ To get the followers of a certain type, eg: all followers of type 'User'
147
+ book.followers_by_type('User') # Returns an array of the user followers
148
+
149
+ There is also a method_missing for this to make it easier:
150
+ book.user_followers # Calls followers_by_type('User')
151
+
152
+ To get just the sql count of the number of followers of a certain type use the following
153
+ book.followers_by_type_count('User') # Return the count on the number of followers of type 'User'
154
+
155
+ Again, there is a method_missing for this method as well
156
+ book.count_user_followers # Calls followers_by_type_count('User')
157
+
158
+ To see is a model that acts_as_followable is followed by a model that acts_as_follower use the following
159
+ book.followed_by?(user)
160
+
161
+ # Returns true if the current instance is followed by the passed record
162
+ # Returns false if the current instance is blocked by the passed record or no follow is found
163
+
164
+ To block a follower call the following
165
+ book.block(user)
166
+ # Blocks the user from appearing in the followers list, and blocks the book from appearing in the user.all_follows or user.all_following lists
167
+
168
+ To unblock is just as simple
169
+ book.unblock(user)
170
+
171
+ To get all blocked records
172
+ book.blocks # Returns an array of blocked follower records (only unblocked) (eg. type User or type Book)
173
+
174
+ If you only need the number of blocks use the count method provided
175
+ book.blocked_followers_count
176
+
177
+ Unblocking deletes all records of that follow, instead of just the :blocked attribute => false the follow is deleted. So, a user would need to try and follow the book again.
178
+ I would like to hear thoughts on this, I may change this to make the follow as blocked: false instead of deleting the record.
179
+
180
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
181
+ followers_by_type, followers, blocks
182
+ ---
183
+
184
+ === Follow Model
185
+
186
+ The Follow model has a set of named_scope's. In case you want to interface directly with the Follow model you can use them.
187
+ Follow.unblocked # returns all "unblocked" follow records
188
+
189
+ Follow.blocked # returns all "blocked" follow records
190
+
191
+ Follow.descending # returns all records in a descending order based on created_at datetime
192
+
193
+ This method pulls all records created after a certain date. The default is 2 weeks but it takes an optional parameter.
194
+ Follow.recent
195
+ Follow.recent(4.weeks.ago)
196
+
197
+ Follow.for_follower is a named_scope that is mainly there to reduce code in the modules but it could be used directly. It takes an object and will return all Follow records where the follower is the record passed in. Note that this will return all blocked and unblocked records.
198
+ Follow.for_follower(user)
199
+ If you don't need the blocked records just use the methods provided for this:
200
+ user.all_follows
201
+ # or
202
+ user.all_following
203
+
204
+ Follow.for_followable acts the same as its counterpart (for_follower). It is mainly there to reduce duplication, however it can be used directly. It takes an object that is the followed object and return all Follow records where that record is the followable. Again, this returns all blocked and unblocked records.
205
+ Follow.for_followable(book)
206
+ Again, if you don't need the blocked records use the method provided for this:
207
+ book.followers
208
+ If you need blocked records only
209
+ book.blocks
210
+
211
+
212
+ == Comments/Requests
213
+
214
+ If anyone has comments or questions please let me know (tom dot cocca at gmail dot com).
215
+ If you have updates or patches or want to contribute I would love to see what you have or want to add.
216
+
217
+
218
+ == Note on Patches/Pull Requests
219
+
220
+ * Fork the project.
221
+ * Make your feature addition or bug fix.
222
+ * Add tests for it. This is important so I don't break it in a future version unintentionally (acts_as_follower uses Shoulda and Factory Girl)
223
+ * Send me a pull request. Bonus points for topic branches.
224
+
225
+
226
+ == Contributers
227
+
228
+ Thanks to everyone for their interest and time in committing to making this plugin better.
229
+
230
+ * dougal (Douglas F Shearer) - https://github.com/dougal
231
+ * jdg (Jonathan George) - https://github.com/jdg
232
+ * m3talsmith (Michael Christenson II) - https://github.com/m3talsmith
233
+ * joergbattermann (Jörg Battermann) - https://github.com/joergbattermann
234
+ * TomK32 (Thomas R. Koll) - https://github.com/TomK32
235
+ * drcapulet (Alex Coomans) - https://github.com/drcapulet
236
+ * jhchabran (Jean Hadrien Chabran) - https://github.com/jhchabran
237
+ * arthurgeek (Arthur Zapparoli) - https://github.com/arthurgeek
238
+ * james2m (James McCarthy) - https://github.com/james2m
239
+ * peterjm (Peter McCracken) - https://github.com/peterjm
240
+ * merqlove (Alexander Merkulov) - https://github.com/merqlove
241
+
242
+ Please let me know if I missed you.
243
+
244
+
245
+ == Copyright
246
+
247
+ Copyright (c) 2008 - 2010 ( tom dot cocca at gmail dot com ), released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rdoc/task'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task default: :test
10
+
11
+ desc 'Test the acts_as_follower gem.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for the acts_as_follower gem.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'Acts As Follower'
22
+ rdoc.main = 'README.rdoc'
23
+ rdoc.options << '--line-numbers' << '--inline-source'
24
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
25
+ end
26
+
27
+ namespace :rcov do
28
+ desc "Generate a coverage report in coverage/"
29
+ task :gen do
30
+ sh "rcov --output coverage test/*_test.rb --exclude 'gems/*'"
31
+ end
32
+
33
+ desc "Remove generated coverage files."
34
+ task :clobber do
35
+ sh "rm -rdf coverage"
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_follower/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_follower1"
7
+ s.version = ActsAsFollower::VERSION
8
+ s.authors = ["Tom Cocca"]
9
+ s.email = ["tom dot cocca at gmail dot com"]
10
+ s.homepage = "https://github.com/tcocca/acts_as_follower"
11
+ s.summary = %q{A Rubygem to add Follow functionality for ActiveRecord models}
12
+ s.description = %q{acts_as_follower is a Rubygem to allow any model to follow any other model. This is accomplished through a double polymorphic relationship on the Follow model. There is also built in support for blocking/un-blocking follow records. Main uses would be for Users to follow other Users or for Users to follow Books, etc… (Basically, to develop the type of follow system that GitHub has)}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "acts_as_follower"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activerecord', '>= 4.0'
23
+
24
+ s.add_development_dependency "sqlite3"
25
+ s.add_development_dependency "shoulda_create"
26
+ s.add_development_dependency "shoulda", ">= 3.5.0"
27
+ s.add_development_dependency "factory_girl", ">= 4.2.0"
28
+ s.add_development_dependency "rails", ">= 4.0"
29
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_follower'
@@ -0,0 +1,45 @@
1
+ module ActsAsFollower #:nodoc:
2
+ module FollowScopes
3
+
4
+ # returns Follow records where follower is the record passed in.
5
+ def for_follower(follower)
6
+ where(follower_id: follower.id, follower_type: parent_class_name(follower))
7
+ end
8
+
9
+ # returns Follow records where followable is the record passed in.
10
+ def for_followable(followable)
11
+ where(followable_id: followable.id, followable_type: parent_class_name(followable))
12
+ end
13
+
14
+ # returns Follow records where follower_type is the record passed in.
15
+ def for_follower_type(follower_type)
16
+ where(follower_type: follower_type)
17
+ end
18
+
19
+ # returns Follow records where followeable_type is the record passed in.
20
+ def for_followable_type(followable_type)
21
+ where(followable_type: followable_type)
22
+ end
23
+
24
+ # returns Follow records from past 2 weeks with default parameter.
25
+ def recent(from)
26
+ where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)])
27
+ end
28
+
29
+ # returns Follow records in descending order.
30
+ def descending
31
+ order("follows.created_at DESC")
32
+ end
33
+
34
+ # returns unblocked Follow records.
35
+ def unblocked
36
+ where(blocked: false)
37
+ end
38
+
39
+ # returns blocked Follow records.
40
+ def blocked
41
+ where(blocked: true)
42
+ end
43
+
44
+ end
45
+ end