acts_as_follower 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +8 -0
  2. data/Gemfile +4 -0
  3. data/MIT-LICENSE +44 -0
  4. data/README.rdoc +208 -0
  5. data/Rakefile +39 -0
  6. data/acts_as_follower.gemspec +25 -0
  7. data/init.rb +1 -0
  8. data/lib/acts_as_follower.rb +11 -0
  9. data/lib/acts_as_follower/followable.rb +102 -0
  10. data/lib/acts_as_follower/follower.rb +100 -0
  11. data/lib/acts_as_follower/follower_lib.rb +15 -0
  12. data/lib/acts_as_follower/railtie.rb +15 -0
  13. data/lib/acts_as_follower/version.rb +3 -0
  14. data/lib/generators/USAGE +5 -0
  15. data/lib/generators/acts_as_follower_generator.rb +30 -0
  16. data/lib/generators/templates/migration.rb +17 -0
  17. data/lib/generators/templates/model.rb +21 -0
  18. data/test/README +24 -0
  19. data/test/acts_as_followable_test.rb +241 -0
  20. data/test/acts_as_follower_test.rb +209 -0
  21. data/test/dummy30/Gemfile +1 -0
  22. data/test/dummy30/Rakefile +7 -0
  23. data/test/dummy30/app/models/band.rb +4 -0
  24. data/test/dummy30/app/models/user.rb +5 -0
  25. data/test/dummy30/config.ru +4 -0
  26. data/test/dummy30/config/application.rb +42 -0
  27. data/test/dummy30/config/boot.rb +10 -0
  28. data/test/dummy30/config/database.yml +7 -0
  29. data/test/dummy30/config/environment.rb +5 -0
  30. data/test/dummy30/config/environments/development.rb +19 -0
  31. data/test/dummy30/config/environments/test.rb +23 -0
  32. data/test/dummy30/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy30/config/initializers/inflections.rb +10 -0
  34. data/test/dummy30/config/initializers/secret_token.rb +7 -0
  35. data/test/dummy30/config/initializers/session_store.rb +8 -0
  36. data/test/dummy30/config/locales/en.yml +5 -0
  37. data/test/dummy30/config/routes.rb +2 -0
  38. data/test/factories/bands.rb +7 -0
  39. data/test/factories/users.rb +11 -0
  40. data/test/follow_test.rb +10 -0
  41. data/test/schema.rb +21 -0
  42. data/test/test_helper.rb +17 -0
  43. metadata +190 -0
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_follower.gemspec
4
+ gemspec
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,208 @@
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
+
10
+ == Installation
11
+
12
+ === The master branch supports rails 3
13
+
14
+ Add the gem to the gemfile:
15
+ gem "acts_as_follower"
16
+
17
+ Run the generator:
18
+ rails generate acts_as_follower
19
+
20
+ This will generate a migration file as well as a model called Follow.
21
+
22
+ === Rails 2.3.x support
23
+
24
+ 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.
25
+ The gem version does not work with rails 2.3.x.
26
+
27
+ Run the following command:
28
+ script/plugin install git://github.com/tcocca/acts_as_follower.git -r rails_2.3.x
29
+
30
+ Run the generator:
31
+ script/generate acts_as_follower
32
+
33
+
34
+ == Usage
35
+
36
+ === Setup
37
+
38
+ Make your model(s) that you want to allow to be followed acts_as_followable, just add the mixin:
39
+ class User < ActiveRecord::Base
40
+ ...
41
+ acts_as_followable
42
+ ...
43
+ end
44
+
45
+ class Book < ActiveRecord::Base
46
+ ...
47
+ acts_as_followable
48
+ ...
49
+ end
50
+
51
+ Make your model(s) that can follow other models acts_as_follower
52
+ class User < ActiveRecord::Base
53
+ ...
54
+ acts_as_follower
55
+ ...
56
+ end
57
+
58
+ ---
59
+
60
+ === acts_as_follower methods
61
+
62
+ To have an object start following another use the following:
63
+ book = Book.find(1)
64
+ user = User.find(1)
65
+ user.follow(book) # Creates a record for the user as the follower and the book as the followable
66
+
67
+ To stop following an object use the following
68
+ user.stop_following(book) # Deletes that record in the Follow table
69
+
70
+ You can check to see if an object that acts_as_follower is following another object through the following:
71
+ user.following?(book) # Returns true or false
72
+
73
+ To get the total number (count) of follows for a user use the following on a model that acts_as_follower
74
+ user.follow_count # Returns and integer
75
+
76
+ To get follow records that have not been blocked use the following
77
+ user.all_follows # returns an array of Follow records
78
+
79
+ To get all of the records that an object is following that have not been blocked use the following
80
+ user.all_following
81
+ # Returns an array of every followed object for the user, this can be a collection of different object types, eg: User, Book
82
+
83
+ To get all Follow records by a certain type use the following
84
+ user.follows_by_type('Book') # returns an array of Follow objects where the followable_type is 'Book'
85
+
86
+ To get all followed objects by a certain type use the following.
87
+ 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
88
+
89
+ There is also a method_missing to accomplish the exact same thing a following_by_type('Book') to make you life easier
90
+ user.following_users # exact same results as user.following_by_type('User')
91
+
92
+ To get the count of all Follow records by a certain type use the following
93
+ user.following_by_type_count('Book') # Returns the sql count of the number of followed books by that user
94
+
95
+ There is also a method_missing to get the count by type
96
+ user.following_books_count # Calls the user.following_by_type_count('Book') method
97
+
98
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
99
+ follows_by_type, all_follows, all_following, following_by_type
100
+ ---
101
+
102
+ === acts_as_followable methods
103
+
104
+ To get all the followers of a model that acts_as_followable
105
+ book.followers # Returns an array of all the followers for that book, a collection of different object types (eg. type User or type Book)
106
+
107
+ To get just the number of follows use
108
+ book.followers_count
109
+
110
+ To get the followers of a certain type, eg: all followers of type 'User'
111
+ book.followers_by_type('User') # Returns an array of the user followers
112
+
113
+ There is also a method_missing for this to make it easier:
114
+ book.user_followers # Calls followers_by_type('User')
115
+
116
+ To get just the sql count of the number of followers of a certain type use the following
117
+ book.followers_by_type_count('User') # Return the count on the number of followers of type 'User'
118
+
119
+ Again, there is a method_missing for this method as well
120
+ book.count_user_followers # Calls followers_by_type_count('User')
121
+
122
+ To see is a model that acts_as_followable is followed by a model that acts_as_follower use the following
123
+ book.followed_by?(user)
124
+
125
+ # Returns true if the current instance is followed by the passed record
126
+ # Returns false if the current instance is blocked by the passed record or no follow is found
127
+
128
+ To block a follower call the following
129
+ book.block(user)
130
+ # 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
131
+
132
+ To unblock is just as simple
133
+ book.unblock(user)
134
+
135
+ To get all blocked records
136
+ book.blocks # Returns an array of blocked follower records (only unblocked) (eg. type User or type Book)
137
+
138
+ If you only need the number of blocks use the count method provided
139
+ book.blocked_followers_count
140
+
141
+ 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.
142
+ I would like to hear thoughts on this, I may change this to make the follow as :blocked => false instead of deleting the record.
143
+
144
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
145
+ followers_by_type, followers, blocks
146
+ ---
147
+
148
+ === Follow Model
149
+
150
+ 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.
151
+ Follow.unblocked # returns all "unblocked" follow records
152
+
153
+ Follow.blocked # returns all "blocked" follow records
154
+
155
+ Follow.descending # returns all records in a descending order based on created_at datetime
156
+
157
+ This method pulls all records created after a certain date. The default is 2 weeks but it takes an optional parameter.
158
+ Follow.recent
159
+ Follow.recent(4.weeks.ago)
160
+
161
+ 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.
162
+ Follow.for_follower(user)
163
+ If you don't need the blocked records just use the methods provided for this:
164
+ user.all_follows
165
+ # or
166
+ user.all_following
167
+
168
+ 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.
169
+ Follow.for_followable(book)
170
+ Again, if you don't need the blocked records use the method provided for this:
171
+ book.followers
172
+ If you need blocked records only
173
+ book.blocks
174
+
175
+
176
+ == Comments/Requests
177
+
178
+ If anyone has comments or questions please let me know (tom dot cocca at gmail dot com).
179
+ If you have updates or patches or want to contribute I would love to see what you have or want to add.
180
+
181
+
182
+ == Note on Patches/Pull Requests
183
+
184
+ * Fork the project.
185
+ * Make your feature addition or bug fix.
186
+ * 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)
187
+ * Send me a pull request. Bonus points for topic branches.
188
+
189
+
190
+ == Contributers
191
+
192
+ Thanks to everyone for their interest and time in committing to making this plugin better.
193
+
194
+ * dougal (Douglas F Shearer) - http://github.com/dougal
195
+ * jdg (Jonathan George) - http://github.com/jdg
196
+ * m3talsmith (Michael Christenson II) - http://github.com/m3talsmith
197
+ * joergbattermann (Jörg Battermann) - http://github.com/joergbattermann
198
+ * TomK32 (Thomas R. Koll) - http://github.com/TomK32
199
+ * drcapulet (Alex Coomans) - http://github.com/drcapulet
200
+ * jhchabran (Jean Hadrien Chabran) - http://github.com/jhchabran
201
+ * arthurgeek (Arthur Zapparoli) - http://github.com/arthurgeek
202
+
203
+ Please let me know if I missed you.
204
+
205
+
206
+ == Copyright
207
+
208
+ Copyright (c) 2008 - 2010 ( tom dot cocca at gmail dot com ), released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
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
+
29
+ desc "Generate a coverage report in coverage/"
30
+ task :gen do
31
+ sh "rcov --output coverage test/*_test.rb --exclude 'gems/*'"
32
+ end
33
+
34
+ desc "Remove generated coverage files."
35
+ task :clobber do
36
+ sh "rm -rdf coverage"
37
+ end
38
+
39
+ end
@@ -0,0 +1,25 @@
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_follower"
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
+
14
+ s.rubyforge_project = "acts_as_follower"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "sqlite3"
22
+ s.add_development_dependency "shoulda"
23
+ s.add_development_dependency "factory_girl"
24
+ s.add_development_dependency "rails", "~>3.0.10"
25
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_follower'
@@ -0,0 +1,11 @@
1
+ require "acts_as_follower/version"
2
+
3
+ module ActsAsFollower
4
+ autoload :Follower, 'acts_as_follower/follower'
5
+ autoload :Followable, 'acts_as_follower/followable'
6
+ autoload :FollowerLib, 'acts_as_follower/follower_lib'
7
+
8
+ require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
9
+ end
10
+
11
+
@@ -0,0 +1,102 @@
1
+ module ActsAsFollower #:nodoc:
2
+ module Followable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_followable
10
+ has_many :followings, :as => :followable, :dependent => :destroy, :class_name => 'Follow'
11
+ include ActsAsFollower::Followable::InstanceMethods
12
+ include ActsAsFollower::FollowerLib
13
+ end
14
+ end
15
+
16
+
17
+ module InstanceMethods
18
+
19
+ # Returns the number of followers a record has.
20
+ def followers_count
21
+ self.followings.unblocked.count
22
+ end
23
+
24
+ # Returns the followers by a given type
25
+ def followers_by_type(follower_type, options={})
26
+ follows = follower_type.constantize.
27
+ includes(:follows).
28
+ where('blocked = ?', false).
29
+ where(
30
+ "follows.followable_id = ? AND follows.followable_type = ? AND follows.follower_type = ?",
31
+ self.id, parent_class_name(self), follower_type
32
+ )
33
+ if options.has_key?(:limit)
34
+ follows = follows.limit(options[:limit])
35
+ end
36
+ follows
37
+ end
38
+
39
+ def followers_by_type_count(follower_type)
40
+ self.followings.unblocked.for_follower_type(follower_type).count
41
+ end
42
+
43
+ # Allows magic names on followers_by_type
44
+ # e.g. user_followers == followers_by_type('User')
45
+ # Allows magic names on followers_by_type_count
46
+ # e.g. count_user_followers == followers_by_type_count('User')
47
+ def method_missing(m, *args)
48
+ if m.to_s[/count_(.+)_followers/]
49
+ followers_by_type_count($1.singularize.classify)
50
+ elsif m.to_s[/(.+)_followers/]
51
+ followers_by_type($1.singularize.classify)
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ def blocked_followers_count
58
+ self.followings.blocked.count
59
+ end
60
+
61
+ # Returns the following records.
62
+ def followers(options={})
63
+ self.followings.unblocked.includes(:follower).all(options).collect{|f| f.follower}
64
+ end
65
+
66
+ def blocks(options={})
67
+ self.followings.blocked.includes(:follower).all(options).collect{|f| f.follower}
68
+ end
69
+
70
+ # Returns true if the current instance is followed by the passed record
71
+ # Returns false if the current instance is blocked by the passed record or no follow is found
72
+ def followed_by?(follower)
73
+ f = get_follow_for(follower)
74
+ (f && !f.blocked?) ? true : false
75
+ end
76
+
77
+ def block(follower)
78
+ get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
79
+ end
80
+
81
+ def unblock(follower)
82
+ get_follow_for(follower).try(:delete)
83
+ end
84
+
85
+ private
86
+
87
+ def get_follow_for(follower)
88
+ Follow.for_followable(self).for_follower(follower).first
89
+ end
90
+
91
+ def block_future_follow(follower)
92
+ follows.create(:followable => self, :follower => follower, :blocked => true)
93
+ end
94
+
95
+ def block_existing_follow(follower)
96
+ get_follow_for(follower).block!
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+ end