followable_behaviour 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f52a9f214e6fd3520f27e04bbef950e6d95ed728fbfe2b674045e4aebf618a67
4
+ data.tar.gz: 551eb48d28477189097281fe59a8e20c33168e567d04c4b57ce1e4eb9eb702b3
5
+ SHA512:
6
+ metadata.gz: e52c0f2d30a8b478b033241ce44d622d8654665c62e69dde6d0ff6db06a4617bbd8e285eba050a0e4b2d045391d4636336cf3d9a8f56b99d99bdd491ffa93641
7
+ data.tar.gz: 40b075359dbedd9665762d6d141d43c5c6eb086b9f789d900d634f65dddc2f9ab6cd2594e2faf9512262193a9425791d5be71a09a6f61e3192f4933e8bfacac5
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in followable_behaviour.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,171 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ followable_behaviour (0.1.0)
5
+ activerecord (~> 7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (7.0.4)
11
+ actionpack (= 7.0.4)
12
+ activesupport (= 7.0.4)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (7.0.4)
16
+ actionpack (= 7.0.4)
17
+ activejob (= 7.0.4)
18
+ activerecord (= 7.0.4)
19
+ activestorage (= 7.0.4)
20
+ activesupport (= 7.0.4)
21
+ mail (>= 2.7.1)
22
+ net-imap
23
+ net-pop
24
+ net-smtp
25
+ actionmailer (7.0.4)
26
+ actionpack (= 7.0.4)
27
+ actionview (= 7.0.4)
28
+ activejob (= 7.0.4)
29
+ activesupport (= 7.0.4)
30
+ mail (~> 2.5, >= 2.5.4)
31
+ net-imap
32
+ net-pop
33
+ net-smtp
34
+ rails-dom-testing (~> 2.0)
35
+ actionpack (7.0.4)
36
+ actionview (= 7.0.4)
37
+ activesupport (= 7.0.4)
38
+ rack (~> 2.0, >= 2.2.0)
39
+ rack-test (>= 0.6.3)
40
+ rails-dom-testing (~> 2.0)
41
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
42
+ actiontext (7.0.4)
43
+ actionpack (= 7.0.4)
44
+ activerecord (= 7.0.4)
45
+ activestorage (= 7.0.4)
46
+ activesupport (= 7.0.4)
47
+ globalid (>= 0.6.0)
48
+ nokogiri (>= 1.8.5)
49
+ actionview (7.0.4)
50
+ activesupport (= 7.0.4)
51
+ builder (~> 3.1)
52
+ erubi (~> 1.4)
53
+ rails-dom-testing (~> 2.0)
54
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
55
+ activejob (7.0.4)
56
+ activesupport (= 7.0.4)
57
+ globalid (>= 0.3.6)
58
+ activemodel (7.0.4)
59
+ activesupport (= 7.0.4)
60
+ activerecord (7.0.4)
61
+ activemodel (= 7.0.4)
62
+ activesupport (= 7.0.4)
63
+ activestorage (7.0.4)
64
+ actionpack (= 7.0.4)
65
+ activejob (= 7.0.4)
66
+ activerecord (= 7.0.4)
67
+ activesupport (= 7.0.4)
68
+ marcel (~> 1.0)
69
+ mini_mime (>= 1.1.0)
70
+ activesupport (7.0.4)
71
+ concurrent-ruby (~> 1.0, >= 1.0.2)
72
+ i18n (>= 1.6, < 2)
73
+ minitest (>= 5.1)
74
+ tzinfo (~> 2.0)
75
+ builder (3.2.4)
76
+ concurrent-ruby (1.1.10)
77
+ crass (1.0.6)
78
+ date (3.3.3)
79
+ erubi (1.12.0)
80
+ factory_bot (6.2.1)
81
+ activesupport (>= 5.0.0)
82
+ globalid (1.0.0)
83
+ activesupport (>= 5.0)
84
+ i18n (1.12.0)
85
+ concurrent-ruby (~> 1.0)
86
+ loofah (2.19.1)
87
+ crass (~> 1.0.2)
88
+ nokogiri (>= 1.5.9)
89
+ mail (2.8.0.1)
90
+ mini_mime (>= 0.1.1)
91
+ net-imap
92
+ net-pop
93
+ net-smtp
94
+ marcel (1.0.2)
95
+ method_source (1.0.0)
96
+ mini_mime (1.1.2)
97
+ minitest (5.17.0)
98
+ net-imap (0.3.4)
99
+ date
100
+ net-protocol
101
+ net-pop (0.1.2)
102
+ net-protocol
103
+ net-protocol (0.2.1)
104
+ timeout
105
+ net-smtp (0.3.3)
106
+ net-protocol
107
+ nio4r (2.5.8)
108
+ nokogiri (1.14.0-x86_64-darwin)
109
+ racc (~> 1.4)
110
+ racc (1.6.2)
111
+ rack (2.2.5)
112
+ rack-test (2.0.2)
113
+ rack (>= 1.3)
114
+ rails (7.0.4)
115
+ actioncable (= 7.0.4)
116
+ actionmailbox (= 7.0.4)
117
+ actionmailer (= 7.0.4)
118
+ actionpack (= 7.0.4)
119
+ actiontext (= 7.0.4)
120
+ actionview (= 7.0.4)
121
+ activejob (= 7.0.4)
122
+ activemodel (= 7.0.4)
123
+ activerecord (= 7.0.4)
124
+ activestorage (= 7.0.4)
125
+ activesupport (= 7.0.4)
126
+ bundler (>= 1.15.0)
127
+ railties (= 7.0.4)
128
+ rails-dom-testing (2.0.3)
129
+ activesupport (>= 4.2.0)
130
+ nokogiri (>= 1.6)
131
+ rails-html-sanitizer (1.4.4)
132
+ loofah (~> 2.19, >= 2.19.1)
133
+ railties (7.0.4)
134
+ actionpack (= 7.0.4)
135
+ activesupport (= 7.0.4)
136
+ method_source
137
+ rake (>= 12.2)
138
+ thor (~> 1.0)
139
+ zeitwerk (~> 2.5)
140
+ rake (13.0.6)
141
+ shoulda (4.0.0)
142
+ shoulda-context (~> 2.0)
143
+ shoulda-matchers (~> 4.0)
144
+ shoulda-context (2.0.0)
145
+ shoulda-matchers (4.5.1)
146
+ activesupport (>= 4.2.0)
147
+ shoulda_create (0.0.9)
148
+ sqlite3 (1.6.0-x86_64-darwin)
149
+ thor (1.2.1)
150
+ timeout (0.3.1)
151
+ tzinfo (2.0.5)
152
+ concurrent-ruby (~> 1.0)
153
+ websocket-driver (0.7.5)
154
+ websocket-extensions (>= 0.1.0)
155
+ websocket-extensions (0.1.5)
156
+ zeitwerk (2.6.6)
157
+
158
+ PLATFORMS
159
+ x86_64-darwin-17
160
+
161
+ DEPENDENCIES
162
+ factory_bot (~> 6.2)
163
+ followable_behaviour!
164
+ rails (~> 7.0)
165
+ rake (~> 13.0)
166
+ shoulda
167
+ shoulda_create
168
+ sqlite3 (~> 1.5)
169
+
170
+ BUNDLED WITH
171
+ 2.3.22
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jean-Baptiste François
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # FollowableBehaviour
2
+
3
+ This is a fork from [acts_as_follower](https://github.com/tcocca/acts_as_follower) that has not been updated since 2017.
4
+
5
+ It's updated against Rails 7 and Ruby 3.1.
6
+
7
+ followable_behaviour is a gem to allow any model to follow any other model.
8
+ This is accomplished through a double polymorphic relationship on the Follow model.
9
+ There is also built in support for blocking/un-blocking follow records.
10
+
11
+ Main uses would be for Users to follow other Users or for Users to follow Books, etc...
12
+
13
+ (Basically, to develop the type of follow system that GitHub has)
14
+
15
+
16
+ ## Installation
17
+
18
+ Install the gem and add to the application's Gemfile by executing:
19
+
20
+ $ bundle add followable_behaviour
21
+
22
+ If bundler is not being used to manage dependencies, install the gem by executing:
23
+
24
+ $ gem install followable_behaviour
25
+
26
+ ## Usage
27
+
28
+ ### Setup
29
+
30
+ Make your model(s) that you want to allow to be followed followable_behaviour, just add the mixin:
31
+ ```ruby
32
+ class User < ActiveRecord::Base
33
+ ...
34
+ followable_behaviour
35
+ ...
36
+ end
37
+
38
+ class Book < ActiveRecord::Base
39
+ ...
40
+ followable_behaviour
41
+ ...
42
+ end
43
+ ```
44
+
45
+ Make your model(s) that can follow other models follower_behaviour
46
+ ```ruby
47
+ class User < ActiveRecord::Base
48
+ ...
49
+ follower_behaviour
50
+ ...
51
+ end
52
+ ```
53
+
54
+ ---
55
+
56
+ ### acts as follower methods
57
+
58
+ To have an object start following another use the following:
59
+ ```ruby
60
+ book = Book.find(1)
61
+ user = User.find(1)
62
+ user.follow(book) # Creates a record for the user as the follower and the book as the followable
63
+ ```
64
+
65
+ To stop following an object use the following
66
+ ```ruby
67
+ user.stop_following(book) # Deletes that record in the Follow table
68
+ ```
69
+
70
+ You can check to see if an object that follower_behaviour is following another object through the following:
71
+ ```ruby
72
+ user.following?(book) # Returns true or false
73
+ ```
74
+
75
+ To get the total number (count) of follows for a user use the following on a model that follower_behaviour
76
+ ```ruby
77
+ user.follow_count # Returns an integer
78
+ ```
79
+
80
+ To get follow records that have not been blocked use the following
81
+ ```ruby
82
+ user.all_follows # returns an array of Follow records
83
+ ```
84
+
85
+ To get all of the records that an object is following that have not been blocked use the following
86
+ ```ruby
87
+ user.all_following
88
+ # Returns an array of every followed object for the user, this can be a collection of different object types, eg: User, Book
89
+ ```
90
+
91
+ To get all Follow records by a certain type use the following
92
+ ```ruby
93
+ user.follows_by_type('Book') # returns an array of Follow objects where the followable_type is 'Book'
94
+ ```
95
+
96
+ To get all followed objects by a certain type use the following.
97
+ ```ruby
98
+ 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
99
+ ```
100
+
101
+ There is also a method_missing to accomplish the exact same thing a following_by_type('Book') to make you life easier
102
+ ```ruby
103
+ user.following_users # exact same results as user.following_by_type('User')
104
+ ```
105
+
106
+ To get the count of all Follow records by a certain type use the following
107
+ ```ruby
108
+ user.following_by_type_count('Book') # Returns the sql count of the number of followed books by that user
109
+ ```
110
+
111
+ There is also a method_missing to get the count by type
112
+ ```ruby
113
+ user.following_books_count # Calls the user.following_by_type_count('Book') method
114
+ ```
115
+
116
+ 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:
117
+ ```ruby
118
+ book.follows_scoped
119
+ ```
120
+
121
+ This does not return the actual follows, just the scope of followings including the followables, essentially:
122
+ ```ruby
123
+ book.follows.unblocked.includes(:followable)
124
+ ```
125
+
126
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
127
+ ```ruby
128
+ follows_by_type, all_follows, all_following, following_by_type
129
+ ```
130
+
131
+ ---
132
+
133
+ ### acts as followable methods
134
+
135
+ To get all the followers of a model that followable_behaviour
136
+ ```ruby
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
+
140
+ 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:
141
+ ```ruby
142
+ book.followers_scoped
143
+ ```
144
+ This does not return the actual followers, just the scope of followings including the followers, essentially:
145
+ ```ruby
146
+ book.followings.includes(:follower)
147
+ ```
148
+
149
+ To get just the number of follows use
150
+ ```ruby
151
+ book.followers_count
152
+ ```
153
+
154
+ To get the followers of a certain type, eg: all followers of type 'User'
155
+ ```ruby
156
+ book.followers_by_type('User') # Returns an array of the user followers
157
+ ```
158
+
159
+ There is also a method_missing for this to make it easier:
160
+ ```ruby
161
+ book.user_followers # Calls followers_by_type('User')
162
+ ```
163
+
164
+ To get just the sql count of the number of followers of a certain type use the following
165
+ ```ruby
166
+ book.followers_by_type_count('User') # Return the count on the number of followers of type 'User'
167
+ ```
168
+
169
+ Again, there is a method_missing for this method as well
170
+ ```ruby
171
+ book.count_user_followers # Calls followers_by_type_count('User')
172
+ ```
173
+
174
+ To see is a model that followable_behaviour is followed by a model that follower_behaviour use the following
175
+ ```ruby
176
+ book.followed_by?(user)
177
+ # Returns true if the current instance is followed by the passed record
178
+ # Returns false if the current instance is blocked by the passed record or no follow is found
179
+ ```
180
+
181
+ To block a follower call the following
182
+ ```ruby
183
+ book.block(user)
184
+ # 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
185
+ ```
186
+
187
+ To unblock is just as simple
188
+ ```ruby
189
+ book.unblock(user)
190
+ ```
191
+
192
+ To get all blocked records
193
+ ```ruby
194
+ book.blocks # Returns an array of blocked follower records (only unblocked) (eg. type User or type Book)
195
+ ```
196
+
197
+ If you only need the number of blocks use the count method provided
198
+ ```ruby
199
+ book.blocked_followers_count
200
+ ```
201
+
202
+ 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.
203
+ I would like to hear thoughts on this, I may change this to make the follow as blocked: false instead of deleting the record.
204
+
205
+ The following methods take an optional hash parameter of ActiveRecord options (:limit, :order, etc...)
206
+ ```ruby
207
+ followers_by_type, followers, blocks
208
+ ```
209
+
210
+ ---
211
+
212
+ ### Follow Model
213
+
214
+ 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.
215
+ ```ruby
216
+ Follow.unblocked # returns all "unblocked" follow records
217
+
218
+ Follow.blocked # returns all "blocked" follow records
219
+
220
+ Follow.descending # returns all records in a descending order based on created_at datetime
221
+ ```
222
+
223
+ This method pulls all records created after a certain date. The default is 2 weeks but it takes an optional parameter.
224
+ ```ruby
225
+ Follow.recent
226
+ Follow.recent(4.weeks.ago)
227
+ ```
228
+
229
+ Follow.for_follower is a named_scope that is mainly there to reduce code in the modules but it could be used directly.
230
+ It takes an object and will return all Follow records where the follower is the record passed in.
231
+
232
+ Note that this will return all blocked and unblocked records.
233
+ ```ruby
234
+ Follow.for_follower(user)
235
+ ```
236
+ If you don't need the blocked records just use the methods provided for this:
237
+ ```ruby
238
+ user.all_follows
239
+ # or
240
+ user.all_following
241
+ ```
242
+
243
+ 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.
244
+ ```ruby
245
+ Follow.for_followable(book)
246
+ ```
247
+
248
+ Again, if you don't need the blocked records use the method provided for this:
249
+ ```ruby
250
+ book.followers
251
+ ```
252
+ If you need blocked records only
253
+ ```ruby
254
+ book.blocks
255
+ ```
256
+
257
+
258
+ ## Development
259
+
260
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
261
+
262
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
263
+
264
+ ## Contributing
265
+
266
+ Bug reports and pull requests are welcome on GitHub at https://gitlab.com/jbpfran/followable-behaviour.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: :test
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+
9
+ desc 'Test the followable_behaviour gem.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
@@ -0,0 +1,45 @@
1
+ module FollowableBehaviour #: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
@@ -0,0 +1,114 @@
1
+ module FollowableBehaviour #:nodoc:
2
+ module Followable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def followable_behaviour
10
+ has_many :followings, as: :followable, dependent: :destroy, class_name: 'Follow'
11
+ include FollowableBehaviour::Followable::InstanceMethods
12
+ include FollowableBehaviour::FollowerLib
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+
18
+ # Returns the number of followers a record has.
19
+ def followers_count
20
+ self.followings.unblocked.count
21
+ end
22
+
23
+ # Returns the followers by a given type
24
+ def followers_by_type(follower_type, options={})
25
+ follows = follower_type.constantize.
26
+ joins(:follows).
27
+ where('follows.blocked' => false,
28
+ 'follows.followable_id' => self.id,
29
+ 'follows.followable_type' => parent_class_name(self),
30
+ 'follows.follower_type' => follower_type)
31
+ if options.has_key?(:limit)
32
+ follows = follows.limit(options[:limit])
33
+ end
34
+ if options.has_key?(:includes)
35
+ follows = follows.includes(options[:includes])
36
+ end
37
+ follows
38
+ end
39
+
40
+ def followers_by_type_count(follower_type)
41
+ self.followings.unblocked.for_follower_type(follower_type).count
42
+ end
43
+
44
+ # Allows magic names on followers_by_type
45
+ # e.g. user_followers == followers_by_type('User')
46
+ # Allows magic names on followers_by_type_count
47
+ # e.g. count_user_followers == followers_by_type_count('User')
48
+ def method_missing(m, *args)
49
+ if m.to_s[/count_(.+)_followers/]
50
+ followers_by_type_count($1.singularize.classify)
51
+ elsif m.to_s[/(.+)_followers/]
52
+ followers_by_type($1.singularize.classify)
53
+ else
54
+ super
55
+ end
56
+ end
57
+
58
+ def respond_to?(m, include_private = false)
59
+ super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/]
60
+ end
61
+
62
+ def blocked_followers_count
63
+ self.followings.blocked.count
64
+ end
65
+
66
+ # Returns the followings records scoped
67
+ def followers_scoped
68
+ self.followings.includes(:follower)
69
+ end
70
+
71
+ def followers(options={})
72
+ followers_scope = followers_scoped.unblocked
73
+ followers_scope = apply_options_to_scope(followers_scope, options)
74
+ followers_scope.to_a.collect{|f| f.follower}
75
+ end
76
+
77
+ def blocks(options={})
78
+ blocked_followers_scope = followers_scoped.blocked
79
+ blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
80
+ blocked_followers_scope.to_a.collect{|f| f.follower}
81
+ end
82
+
83
+ # Returns true if the current instance is followed by the passed record
84
+ # Returns false if the current instance is blocked by the passed record or no follow is found
85
+ def followed_by?(follower)
86
+ self.followings.unblocked.for_follower(follower).first.present?
87
+ end
88
+
89
+ def block(follower)
90
+ return if follower == self
91
+ get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
92
+ end
93
+
94
+ def unblock(follower)
95
+ get_follow_for(follower).try(:delete)
96
+ end
97
+
98
+ def get_follow_for(follower)
99
+ self.followings.for_follower(follower).first
100
+ end
101
+
102
+ private
103
+
104
+ def block_future_follow(follower)
105
+ Follow.create(followable: self, follower: follower, blocked: true)
106
+ end
107
+
108
+ def block_existing_follow(follower)
109
+ get_follow_for(follower).block!
110
+ end
111
+
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,112 @@
1
+ module FollowableBehaviour #:nodoc:
2
+ module Follower
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def follower_behaviour
10
+ has_many :follows, as: :follower, dependent: :destroy
11
+ include FollowableBehaviour::Follower::InstanceMethods
12
+ include FollowableBehaviour::FollowerLib
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+
18
+ # Returns true if this instance is following the object passed as an argument.
19
+ def following?(followable)
20
+ 0 < Follow.unblocked.for_follower(self).for_followable(followable).count
21
+ end
22
+
23
+ # Returns the number of objects this instance is following.
24
+ def follow_count
25
+ Follow.unblocked.for_follower(self).count
26
+ end
27
+
28
+ # Creates a new follow record for this instance to follow the passed object.
29
+ # Does not allow duplicate records to be created.
30
+ def follow(followable)
31
+ if self != followable
32
+ params = {followable_id: followable.id, followable_type: parent_class_name(followable)}
33
+ self.follows.where(params).first_or_create!
34
+ end
35
+ end
36
+
37
+ # Deletes the follow record if it exists.
38
+ def stop_following(followable)
39
+ if follow = get_follow(followable)
40
+ follow.destroy
41
+ end
42
+ end
43
+
44
+ # returns the follows records to the current instance
45
+ def follows_scoped
46
+ self.follows.unblocked.includes(:followable)
47
+ end
48
+
49
+ # Returns the follow records related to this instance by type.
50
+ def follows_by_type(followable_type, options={})
51
+ follows_scope = follows_scoped.for_followable_type(followable_type)
52
+ follows_scope = apply_options_to_scope(follows_scope, options)
53
+ end
54
+
55
+ # Returns the follow records related to this instance with the followable included.
56
+ def all_follows(options={})
57
+ follows_scope = follows_scoped
58
+ follows_scope = apply_options_to_scope(follows_scope, options)
59
+ end
60
+
61
+ # Returns the actual records which this instance is following.
62
+ def all_following(options={})
63
+ all_follows(options).collect{ |f| f.followable }
64
+ end
65
+
66
+ # Returns the actual records of a particular type which this record is following.
67
+ def following_by_type(followable_type, options={})
68
+ followables = followable_type.constantize.
69
+ joins(:followings).
70
+ where('follows.blocked' => false,
71
+ 'follows.follower_id' => self.id,
72
+ 'follows.follower_type' => parent_class_name(self),
73
+ 'follows.followable_type' => followable_type)
74
+ if options.has_key?(:limit)
75
+ followables = followables.limit(options[:limit])
76
+ end
77
+ if options.has_key?(:includes)
78
+ followables = followables.includes(options[:includes])
79
+ end
80
+ followables
81
+ end
82
+
83
+ def following_by_type_count(followable_type)
84
+ follows.unblocked.for_followable_type(followable_type).count
85
+ end
86
+
87
+ # Allows magic names on following_by_type
88
+ # e.g. following_users == following_by_type('User')
89
+ # Allows magic names on following_by_type_count
90
+ # e.g. following_users_count == following_by_type_count('User')
91
+ def method_missing(m, *args)
92
+ if m.to_s[/following_(.+)_count/]
93
+ following_by_type_count($1.singularize.classify)
94
+ elsif m.to_s[/following_(.+)/]
95
+ following_by_type($1.singularize.classify)
96
+ else
97
+ super
98
+ end
99
+ end
100
+
101
+ def respond_to?(m, include_private = false)
102
+ super || m.to_s[/following_(.+)_count/] || m.to_s[/following_(.+)/]
103
+ end
104
+
105
+ # Returns a follow record for the current instance and followable object.
106
+ def get_follow(followable)
107
+ self.follows.unblocked.for_followable(followable).first
108
+ end
109
+
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,30 @@
1
+ module FollowableBehaviour
2
+ module FollowerLib
3
+
4
+ private
5
+
6
+ # Retrieves the parent class name if using STI.
7
+ def parent_class_name(obj)
8
+ obj.class.base_class.name
9
+ end
10
+
11
+ def apply_options_to_scope(scope, options = {})
12
+ if options.has_key?(:limit)
13
+ scope = scope.limit(options[:limit])
14
+ end
15
+ if options.has_key?(:includes)
16
+ scope = scope.includes(options[:includes])
17
+ end
18
+ if options.has_key?(:joins)
19
+ scope = scope.joins(options[:joins])
20
+ end
21
+ if options.has_key?(:where)
22
+ scope = scope.where(options[:where])
23
+ end
24
+ if options.has_key?(:order)
25
+ scope = scope.order(options[:order])
26
+ end
27
+ scope
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module FollowableBehaviour
4
+ class Railtie < Rails::Railtie
5
+
6
+ initializer "acts_as_follower.active_record" do |app|
7
+ ActiveSupport.on_load :active_record do
8
+ include FollowableBehaviour::Follower
9
+ include FollowableBehaviour::Followable
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FollowableBehaviour
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "followable_behaviour/version"
4
+
5
+ module FollowableBehaviour
6
+ class Error < StandardError; end
7
+
8
+ autoload :Follower, 'followable_behaviour/follower'
9
+ autoload :Followable, 'followable_behaviour/followable'
10
+ autoload :FollowerLib, 'followable_behaviour/follower_lib'
11
+ autoload :FollowScopes, 'followable_behaviour/follow_scopes'
12
+
13
+ require 'followable_behaviour/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 6
14
+
15
+ end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ rails generate acts_as_follower
3
+
4
+ no need to specify a name after acts_as_follower as you can not change the model name from Follow
5
+ the acts_as_follower_migration file will be created in db/migrate
@@ -0,0 +1,30 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class FollowableBehaviourGenerator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ def self.source_root
9
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
10
+ end
11
+
12
+ # Implement the required interface for Rails::Generators::Migration.
13
+ # taken from https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record.rb
14
+ def self.next_migration_number(dirname)
15
+ if ActiveRecord::Base.timestamped_migrations
16
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
17
+ else
18
+ "%.3d" % (current_migration_number(dirname) + 1)
19
+ end
20
+ end
21
+
22
+ def create_migration_file
23
+ migration_template 'migration.rb', 'db/migrate/followable_behaviour_migration.rb'
24
+ end
25
+
26
+ def create_model
27
+ template "model.rb", File.join('app/models', "follow.rb")
28
+ end
29
+
30
+ end
@@ -0,0 +1,22 @@
1
+ if ActiveRecord.gem_version >= Gem::Version.new('5.0')
2
+ class ActsAsFollowerMigration < ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]; end
3
+ else
4
+ class ActsAsFollowerMigration < ActiveRecord::Migration; end
5
+ end
6
+ FollowableBehaviourMigration.class_eval do
7
+ def self.up
8
+ create_table :follows, force: true do |t|
9
+ t.references :followable, polymorphic: true, null: false
10
+ t.references :follower, polymorphic: true, null: false
11
+ t.boolean :blocked, default: false, null: false
12
+ t.timestamps
13
+ end
14
+
15
+ add_index :follows, ["follower_id", "follower_type"], name: "fk_follows"
16
+ add_index :follows, ["followable_id", "followable_type"], name: "fk_followables"
17
+ end
18
+
19
+ def self.down
20
+ drop_table :follows
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class Follow < ActiveRecord::Base
2
+
3
+ extend FollowableBehaviour::FollowerLib
4
+ extend FollowableBehaviour::FollowScopes
5
+
6
+ # NOTE: Follows belong to the "followable" and "follower" interface
7
+ belongs_to :followable, polymorphic: true
8
+ belongs_to :follower, polymorphic: true
9
+
10
+ def block!
11
+ self.update_attribute(:blocked, true)
12
+ end
13
+
14
+ end
@@ -0,0 +1,4 @@
1
+ module FollowableBehaviour
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: followable_behaviour
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jean-Baptiste Francois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda_create
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_bot
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '6.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '6.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '7.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '7.0'
97
+ description: Updated fork of the unmaintained gem acts_as_followers. It was tested
98
+ with Ruby 3.1.2 and Rails 7
99
+ email:
100
+ - jbpfran@studiosjb.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - CHANGELOG
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - lib/followable_behaviour.rb
112
+ - lib/followable_behaviour/follow_scopes.rb
113
+ - lib/followable_behaviour/followable.rb
114
+ - lib/followable_behaviour/follower.rb
115
+ - lib/followable_behaviour/follower_lib.rb
116
+ - lib/followable_behaviour/railtie.rb
117
+ - lib/followable_behaviour/version.rb
118
+ - lib/generators/USAGE
119
+ - lib/generators/followable_generator.rb
120
+ - lib/generators/templates/migration.rb
121
+ - lib/generators/templates/model.rb
122
+ - sig/followable_behaviour.rbs
123
+ homepage: https://gitlab.com/jbpfran/followable-behaviour
124
+ licenses: []
125
+ metadata:
126
+ homepage_uri: https://gitlab.com/jbpfran/followable-behaviour
127
+ source_code_uri: https://gitlab.com/jbpfran/followable-behaviour
128
+ changelog_uri: https://gitlab.com/jbpfran/followable-behaviour/-/blob/main/CHANGELOG
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 2.6.0
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubygems_version: 3.3.7
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Updated fork of the unmaintained gem acts_as_followers.
148
+ test_files: []