follow_system 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.empty +0 -0
- data/.gitignore +15 -0
- data/.keep +0 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +23 -0
- data/Appraisals +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +8 -0
- data/follow_system.gemspec +31 -0
- data/gemfiles/.empty +0 -0
- data/gemfiles/.gitignore +0 -0
- data/gemfiles/.keep +0 -0
- data/gemfiles/rails4.1.gemfile +7 -0
- data/gemfiles/rails4.2.gemfile +7 -0
- data/lib/.empty +0 -0
- data/lib/.gitignore +0 -0
- data/lib/.keep +0 -0
- data/lib/follow_system/.empty +0 -0
- data/lib/follow_system/.gitignore +0 -0
- data/lib/follow_system/.keep +0 -0
- data/lib/follow_system/follow.rb +155 -0
- data/lib/follow_system/followee.rb +58 -0
- data/lib/follow_system/follower.rb +88 -0
- data/lib/follow_system/version.rb +12 -0
- data/lib/follow_system.rb +45 -0
- data/lib/generators/.empty +0 -0
- data/lib/generators/.gitignore +0 -0
- data/lib/generators/.keep +0 -0
- data/lib/generators/follow_system/.empty +0 -0
- data/lib/generators/follow_system/.gitignore +0 -0
- data/lib/generators/follow_system/.keep +0 -0
- data/lib/generators/follow_system/follow_system_generator.rb +46 -0
- data/lib/generators/follow_system/templates/.empty +0 -0
- data/lib/generators/follow_system/templates/.gitignore +0 -0
- data/lib/generators/follow_system/templates/.keep +0 -0
- data/lib/generators/follow_system/templates/migration.rb +47 -0
- data/spec/.empty +0 -0
- data/spec/.gitignore +0 -0
- data/spec/.keep +0 -0
- data/spec/db/.empty +0 -0
- data/spec/db/.gitignore +0 -0
- data/spec/db/.keep +0 -0
- data/spec/db/migrate/.empty +0 -0
- data/spec/db/migrate/.gitignore +0 -0
- data/spec/db/migrate/.keep +0 -0
- data/spec/db/migrate/20140926000000_create_follows.rb +47 -0
- data/spec/db/migrate/20140926000005_create_dummy_followers.rb +22 -0
- data/spec/db/migrate/20140926000010_create_dummy_followees.rb +22 -0
- data/spec/follow_system/.empty +0 -0
- data/spec/follow_system/.gitignore +0 -0
- data/spec/follow_system/.keep +0 -0
- data/spec/follow_system/follow_spec.rb +177 -0
- data/spec/follow_system/followee_spec.rb +69 -0
- data/spec/follow_system/follower_spec.rb +96 -0
- data/spec/spec_helper.rb +116 -0
- data/spec/support/.empty +0 -0
- data/spec/support/.gitignore +0 -0
- data/spec/support/.keep +0 -0
- data/spec/support/active_record.rb +12 -0
- data/spec/support/shoulda_matchers.rb +2 -0
- metadata +237 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2bb5f4c3e1b5fac7f89133975eed0675d3df87df
|
4
|
+
data.tar.gz: 13191b7027a9273d2715c2a3c251096bcb0be5f6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4520134d84b88e0b494f6ae0d1185a6eca9f9d832408ad4615e331e1fa966dabfd295cbab2c866a8491ee483c375a0abb27d7e16ee7f0d6a8112b7b0469fd8f4
|
7
|
+
data.tar.gz: 6ca7a689ac9511fe4c34b6ccdf25fdcb348bb73c8fa61e14283e8c266e6547cce6c543a63cbe161a752c298d1b7e5c3bcdaaf35d4f328ba47a43a236040896da
|
data/.empty
ADDED
File without changes
|
data/.gitignore
ADDED
data/.keep
ADDED
File without changes
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
follow_system
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2.1
|
data/.travis.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
language:
|
2
|
+
- ruby
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.3
|
7
|
+
- 2.2.1
|
8
|
+
|
9
|
+
gemfile:
|
10
|
+
- gemfiles/rails4.1.gemfile
|
11
|
+
- gemfiles/rails4.2.gemfile
|
12
|
+
|
13
|
+
install:
|
14
|
+
- "travis_retry bundle install"
|
15
|
+
|
16
|
+
before_script:
|
17
|
+
|
18
|
+
script:
|
19
|
+
- "bundle exec rake"
|
20
|
+
|
21
|
+
branches:
|
22
|
+
only:
|
23
|
+
- master
|
data/Appraisals
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Pablo Martin Viva
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# FollowSystem
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/pmviva/follow_system.png?branch=master)](https://travis-ci.org/pmviva/follow_system)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/follow_system.svg)](http://badge.fury.io/rb/follow_system)
|
5
|
+
|
6
|
+
An active record follow system developed using ruby on rails 4.1 applying domain driven design and test driven development principles.
|
7
|
+
|
8
|
+
This gem is heavily influenced by cmer/socialization.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'follow_system'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
$ gem install follow_system
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Run the generator
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
$ rails g follow_system
|
36
|
+
```
|
37
|
+
|
38
|
+
Let's suppose for a moment that you have a celebrity news application and a User can follow a Celebrity or several Celebrity models.
|
39
|
+
The user model becomes the follower and the celebrity model becomes the followee.
|
40
|
+
|
41
|
+
### Celebrity object
|
42
|
+
```ruby
|
43
|
+
class Celebrity < ActiveRecord::Base
|
44
|
+
act_as_followee
|
45
|
+
|
46
|
+
validates :name, { presence: true, uniqueness: true }
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
### User object
|
51
|
+
```ruby
|
52
|
+
class User < ActiveRecord::Base
|
53
|
+
act_as_follower
|
54
|
+
|
55
|
+
validates :username, { presence: true, uniqueness: true }
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Followee object methods
|
60
|
+
```ruby
|
61
|
+
celebrity.is_followee? # returns true
|
62
|
+
|
63
|
+
celebrity.followed_by?(user) # returns true if user follows the celebrity object, false otherwise
|
64
|
+
|
65
|
+
celebrity.followers_by(User) # returns a scope of FollowSystem::Follow join model that belongs to the celebrity object and belongs to follower objects of type User
|
66
|
+
```
|
67
|
+
|
68
|
+
|
69
|
+
### Follower object methods
|
70
|
+
```ruby
|
71
|
+
user.is_follower? # returns true
|
72
|
+
|
73
|
+
user.follow(celebrity) # Creates an instance of FollowSystem::Follow join model associating the user object and the celebrity object, returns true if succeded, false otherwise
|
74
|
+
|
75
|
+
user.unfollow(celebrity) # Destroys an instance of FollowSystem::Follow join model that associates the user object and the celebrity object, returns true if succeded, false otherwise
|
76
|
+
|
77
|
+
user.toggle_follow(celebrity) # Follows / unfollows the celebrity
|
78
|
+
|
79
|
+
user.follows?(celebrity) # returns true if the user object follows the celebrity object, false otherwise
|
80
|
+
|
81
|
+
user.followees_by(Celebrity) # returns a scope of FollowSystem::Follow join model that belongs to the user object and belongs to followee objects of type Celebrity
|
82
|
+
```
|
83
|
+
|
84
|
+
For more information read the [api documentation](http://rubydoc.info/gems/follow_system).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
1. Fork it ( https://github.com/pmviva/follow_system/fork )
|
89
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
90
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
91
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
92
|
+
5. Create a new Pull Request
|
93
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'follow_system/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "follow_system"
|
8
|
+
spec.version = FollowSystem::VERSION
|
9
|
+
spec.authors = ["Pablo Martin Viva"]
|
10
|
+
spec.email = ["pmviva@gmail.com"]
|
11
|
+
spec.summary = %q{An active record follow system.}
|
12
|
+
spec.description = %q{An active record follow system developed using ruby on rails 4.1 applying domain driven design and test driven development principles.}
|
13
|
+
spec.homepage = "http://github.com/pmviva/follow_system"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
|
21
|
+
|
22
|
+
spec.add_dependency "rails", [ ">= 4.1", "< 5" ]
|
23
|
+
|
24
|
+
spec.add_development_dependency "appraisal", "~> 2.0"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
28
|
+
spec.add_development_dependency "shoulda-matchers", "~> 2.7"
|
29
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
30
|
+
end
|
31
|
+
|
data/gemfiles/.empty
ADDED
File without changes
|
data/gemfiles/.gitignore
ADDED
File without changes
|
data/gemfiles/.keep
ADDED
File without changes
|
data/lib/.empty
ADDED
File without changes
|
data/lib/.gitignore
ADDED
File without changes
|
data/lib/.keep
ADDED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,155 @@
|
|
1
|
+
###
|
2
|
+
# FollowSystem module
|
3
|
+
#
|
4
|
+
# This module defines common behavior in follow system
|
5
|
+
###
|
6
|
+
module FollowSystem
|
7
|
+
###
|
8
|
+
# Follow class
|
9
|
+
#
|
10
|
+
# This class defines the follow model in follow system
|
11
|
+
###
|
12
|
+
class Follow < ActiveRecord::Base
|
13
|
+
###
|
14
|
+
# Belongs to followee association configuration
|
15
|
+
###
|
16
|
+
belongs_to :followee, polymorphic: :true
|
17
|
+
|
18
|
+
###
|
19
|
+
# Belongs to follower association configuration
|
20
|
+
###
|
21
|
+
belongs_to :follower, polymorphic: :true
|
22
|
+
|
23
|
+
###
|
24
|
+
# Creates a {Follow} relationship between a {Follower} object and a {Followee} object
|
25
|
+
#
|
26
|
+
# @param [Follower] follower - the {Follower} of the relationship
|
27
|
+
# @param [Followee] followee - the {Followee} of the relationship
|
28
|
+
# @return [Boolean]
|
29
|
+
###
|
30
|
+
def self.follow(follower, followee)
|
31
|
+
validate_followee(followee)
|
32
|
+
validate_follower(follower)
|
33
|
+
|
34
|
+
if follows?(follower, followee)
|
35
|
+
false
|
36
|
+
else
|
37
|
+
follow = scope_by_follower(follower).scope_by_followee(followee).build
|
38
|
+
follow.save
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
###
|
44
|
+
# Destroys a {Follow} relationship between a {Follower} object and a {Followee} object
|
45
|
+
#
|
46
|
+
# @param [Follower] follower - the {Follower} of the relationship
|
47
|
+
# @param [Followee] followee - the {Followee} of the relationship
|
48
|
+
# @return [Boolean]
|
49
|
+
###
|
50
|
+
def self.unfollow(follower, followee)
|
51
|
+
validate_followee(followee)
|
52
|
+
validate_follower(follower)
|
53
|
+
|
54
|
+
if follows?(follower, followee)
|
55
|
+
follow = scope_by_follower(follower).scope_by_followee(followee).take
|
56
|
+
follow.destroy
|
57
|
+
true
|
58
|
+
else
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
###
|
64
|
+
# Toggles a {Follow} relationship between a {Follower} object and a {Followee} object
|
65
|
+
#
|
66
|
+
# @param [Follower] follower - the {Follower} of the relationship
|
67
|
+
# @param [Followee] followee - the {Followee} of the relationship
|
68
|
+
# @return [Boolean]
|
69
|
+
###
|
70
|
+
def self.toggle_follow(follower, followee)
|
71
|
+
validate_followee(followee)
|
72
|
+
validate_follower(follower)
|
73
|
+
|
74
|
+
if follows?(follower, followee)
|
75
|
+
unfollow(follower, followee)
|
76
|
+
else
|
77
|
+
follow(follower, followee)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
###
|
82
|
+
# Specifies if a {Follower} object follows a {Followee} object
|
83
|
+
#
|
84
|
+
# @param [Follower] follower - the {Follower} object to test against
|
85
|
+
# @param [Followee] followee - the {Followee} object to test against
|
86
|
+
# @return [Boolean]
|
87
|
+
###
|
88
|
+
def self.follows?(follower, followee)
|
89
|
+
validate_followee(followee)
|
90
|
+
validate_follower(follower)
|
91
|
+
|
92
|
+
scope_by_follower(follower).scope_by_followee(followee).exists?
|
93
|
+
end
|
94
|
+
|
95
|
+
###
|
96
|
+
# Retrieves a scope of {Follow} objects filtered by a {Followee} object
|
97
|
+
#
|
98
|
+
# @param [Followee] followee - the {Followee} to filter
|
99
|
+
# @return [ActiveRecord::Relation]
|
100
|
+
###
|
101
|
+
def self.scope_by_followee(followee)
|
102
|
+
where(followee: followee)
|
103
|
+
end
|
104
|
+
|
105
|
+
###
|
106
|
+
# Retrieves a scope of {Follow} objects filtered by a {Followee} type
|
107
|
+
#
|
108
|
+
# @param [Class] klass - the {Class} to filter
|
109
|
+
# @return [ActiveRecord::Relation]
|
110
|
+
###
|
111
|
+
def self.scope_by_followee_type(klass)
|
112
|
+
where(followee_type: klass.to_s.classify)
|
113
|
+
end
|
114
|
+
|
115
|
+
###
|
116
|
+
# Retrieves a scope of {Follow} objects filtered by a {Follower} object
|
117
|
+
#
|
118
|
+
# @param [Follower] follower - the {Follower} to filter
|
119
|
+
# @return [ActiveRecord::Relation]
|
120
|
+
###
|
121
|
+
def self.scope_by_follower(follower)
|
122
|
+
where(follower: follower)
|
123
|
+
end
|
124
|
+
|
125
|
+
###
|
126
|
+
# Retrieves a scope of {Follow} objects filtered by a {Follower} type
|
127
|
+
#
|
128
|
+
# @param [Class] klass - the {Class} to filter
|
129
|
+
# @return [ActiveRecord::Relation]
|
130
|
+
###
|
131
|
+
def self.scope_by_follower_type(klass)
|
132
|
+
where(follower_type: klass.to_s.classify)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
###
|
137
|
+
# Validates a followee object
|
138
|
+
#
|
139
|
+
# @raise [ArgumentError] if the followee object is invalid
|
140
|
+
###
|
141
|
+
def self.validate_followee(followee)
|
142
|
+
raise ArgumentError.new unless followee.respond_to?(:is_followee?) && followee.is_followee?
|
143
|
+
end
|
144
|
+
|
145
|
+
###
|
146
|
+
# Validates a follower object
|
147
|
+
#
|
148
|
+
# @raise [ArgumentError] if the follower object is invalid
|
149
|
+
###
|
150
|
+
def self.validate_follower(follower)
|
151
|
+
raise ArgumentError.new unless follower.respond_to?(:is_follower?) && follower.is_follower?
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
###
|
2
|
+
# FollowSystem module
|
3
|
+
#
|
4
|
+
# This module defines common behavior in follow system
|
5
|
+
###
|
6
|
+
module FollowSystem
|
7
|
+
###
|
8
|
+
# Followee module
|
9
|
+
#
|
10
|
+
# This module defines followee behavior in follow system
|
11
|
+
###
|
12
|
+
module Followee
|
13
|
+
###
|
14
|
+
# Extends ActiveSupport::Concern
|
15
|
+
###
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
###
|
19
|
+
# Included configuration
|
20
|
+
###
|
21
|
+
included do
|
22
|
+
###
|
23
|
+
# Has many followers association configuration
|
24
|
+
###
|
25
|
+
has_many :followers, class_name: "FollowSystem::Follow", as: :followee, dependent: :destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
###
|
29
|
+
# Specifies if self can be followed by {Follower} objects
|
30
|
+
#
|
31
|
+
# @return [Boolean]
|
32
|
+
###
|
33
|
+
def is_followee?
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
###
|
38
|
+
# Specifies if self is followed by a {Follower} object
|
39
|
+
#
|
40
|
+
# @param [Follower] follower - the {Follower} object to test against
|
41
|
+
# @return [Boolean]
|
42
|
+
###
|
43
|
+
def followed_by?(follower)
|
44
|
+
Follow.follows?(follower, self)
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Retrieves a scope of {Follow} objects that follows self filtered {Follower} type
|
49
|
+
#
|
50
|
+
# @param [Class] klass - the {Class} to filter
|
51
|
+
# @return [ActiveRecord::Relation]
|
52
|
+
###
|
53
|
+
def followers_by(klass)
|
54
|
+
Follow.scope_by_followee(self).scope_by_follower_type(klass)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
###
|
2
|
+
# FollowSystem module
|
3
|
+
#
|
4
|
+
# This module defines common behavior in follow system
|
5
|
+
###
|
6
|
+
module FollowSystem
|
7
|
+
###
|
8
|
+
# Follower module
|
9
|
+
#
|
10
|
+
# This module defines follower behavior in follow system
|
11
|
+
###
|
12
|
+
module Follower
|
13
|
+
###
|
14
|
+
# Extends ActiveSupport::Concern
|
15
|
+
###
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
|
18
|
+
###
|
19
|
+
# Included configuration
|
20
|
+
###
|
21
|
+
included do
|
22
|
+
###
|
23
|
+
# Has many followees association configuration
|
24
|
+
###
|
25
|
+
has_many :followees, class_name: "FollowSystem::Follow", as: :follower, dependent: :destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
###
|
29
|
+
# Specifies if self can follow {Followee} objects
|
30
|
+
#
|
31
|
+
# @return [Boolean]
|
32
|
+
###
|
33
|
+
def is_follower?
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
###
|
38
|
+
# Creates a {Follow} relationship between self and a {Followee} object
|
39
|
+
#
|
40
|
+
# @param [Followee] followee - the followee of the {Follow} relationship
|
41
|
+
# @return [Boolean]
|
42
|
+
###
|
43
|
+
def follow(followee)
|
44
|
+
Follow.follow(self, followee)
|
45
|
+
end
|
46
|
+
|
47
|
+
###
|
48
|
+
# Destroys a {Follow} relationship between self and a {Followee} object
|
49
|
+
#
|
50
|
+
# @param [Followee] followee - the followee of the {Follow} relationship
|
51
|
+
# @return [Boolean]
|
52
|
+
###
|
53
|
+
def unfollow(followee)
|
54
|
+
Follow.unfollow(self, followee)
|
55
|
+
end
|
56
|
+
|
57
|
+
###
|
58
|
+
# Toggles a {Follow} relationship between self and a {Followee} object
|
59
|
+
#
|
60
|
+
# @param [Followee] followee - the followee of the {Follow} relationship
|
61
|
+
# @return [Boolean]
|
62
|
+
###
|
63
|
+
def toggle_follow(followee)
|
64
|
+
Follow.toggle_follow(self, followee)
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# Specifies if self follows a {Follower} object
|
69
|
+
#
|
70
|
+
# @param [Followee] followee - the {Followee} object to test against
|
71
|
+
# @return [Boolean]
|
72
|
+
###
|
73
|
+
def follows?(followee)
|
74
|
+
Follow.follows?(self, followee)
|
75
|
+
end
|
76
|
+
|
77
|
+
###
|
78
|
+
# Retrieves a scope of {Follow} objects that are followed by self
|
79
|
+
#
|
80
|
+
# @param [Class] klass - the {Class} to include
|
81
|
+
# @return [ActiveRecord::Relation]
|
82
|
+
###
|
83
|
+
def followees_by(klass)
|
84
|
+
Follow.scope_by_follower(self).scope_by_followee_type(klass)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'follow_system/follow'
|
2
|
+
require 'follow_system/followee'
|
3
|
+
require 'follow_system/follower'
|
4
|
+
|
5
|
+
###
|
6
|
+
# FollowSystem module
|
7
|
+
#
|
8
|
+
# This module defines common behavior in follow system
|
9
|
+
###
|
10
|
+
module FollowSystem
|
11
|
+
###
|
12
|
+
# Specifies if self can be followed by {Follower} objects
|
13
|
+
#
|
14
|
+
# @return [Boolean]
|
15
|
+
###
|
16
|
+
def is_followee?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
###
|
21
|
+
# Specifies if self can follow {Followee} objects
|
22
|
+
#
|
23
|
+
# @return [Boolean]
|
24
|
+
###
|
25
|
+
def is_follower?
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
###
|
30
|
+
# Instructs self to act as followee
|
31
|
+
###
|
32
|
+
def act_as_followee
|
33
|
+
include Followee
|
34
|
+
end
|
35
|
+
|
36
|
+
###
|
37
|
+
# Instructs self to act as follower
|
38
|
+
###
|
39
|
+
def act_as_follower
|
40
|
+
include Follower
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.extend FollowSystem
|
45
|
+
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|