Class: FollowSystem::Follow
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- FollowSystem::Follow
- Defined in:
- lib/follow_system/follow.rb
Overview
Follow class
This class defines the follow model in follow system
Class Method Summary collapse
- .follow(follower, followee) ⇒ Boolean
- .follows?(follower, followee) ⇒ Boolean
- .scope_by_followee(followee) ⇒ ActiveRecord::Relation
- .scope_by_followee_type(klass) ⇒ ActiveRecord::Relation
- .scope_by_follower(follower) ⇒ ActiveRecord::Relation
- .scope_by_follower_type(klass) ⇒ ActiveRecord::Relation
- .toggle_follow(follower, followee) ⇒ Boolean
- .unfollow(follower, followee) ⇒ Boolean
-
.validate_followee(followee) ⇒ Object
private
Validates a followee object.
-
.validate_follower(follower) ⇒ Object
private
Validates a follower object.
Class Method Details
.follow(follower, followee) ⇒ Boolean
Creates a FollowSystem::Follow relationship between a FollowSystem::Follower object and a FollowSystem::Followee object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/follow_system/follow.rb', line 30 def self.follow(follower, followee) validate_followee(followee) validate_follower(follower) if follows?(follower, followee) false else follow = scope_by_follower(follower).scope_by_followee(followee).build follow.save true end end |
.follows?(follower, followee) ⇒ Boolean
Specifies if a FollowSystem::Follower object follows a FollowSystem::Followee object
88 89 90 91 92 93 |
# File 'lib/follow_system/follow.rb', line 88 def self.follows?(follower, followee) validate_followee(followee) validate_follower(follower) scope_by_follower(follower).scope_by_followee(followee).exists? end |
.scope_by_followee(followee) ⇒ ActiveRecord::Relation
Retrieves a scope of FollowSystem::Follow objects filtered by a FollowSystem::Followee object
101 102 103 |
# File 'lib/follow_system/follow.rb', line 101 def self.scope_by_followee(followee) where(followee: followee) end |
.scope_by_followee_type(klass) ⇒ ActiveRecord::Relation
Retrieves a scope of FollowSystem::Follow objects filtered by a FollowSystem::Followee type
111 112 113 |
# File 'lib/follow_system/follow.rb', line 111 def self.scope_by_followee_type(klass) where(followee_type: klass.to_s.classify) end |
.scope_by_follower(follower) ⇒ ActiveRecord::Relation
Retrieves a scope of FollowSystem::Follow objects filtered by a FollowSystem::Follower object
121 122 123 |
# File 'lib/follow_system/follow.rb', line 121 def self.scope_by_follower(follower) where(follower: follower) end |
.scope_by_follower_type(klass) ⇒ ActiveRecord::Relation
Retrieves a scope of FollowSystem::Follow objects filtered by a FollowSystem::Follower type
131 132 133 |
# File 'lib/follow_system/follow.rb', line 131 def self.scope_by_follower_type(klass) where(follower_type: klass.to_s.classify) end |
.toggle_follow(follower, followee) ⇒ Boolean
Toggles a FollowSystem::Follow relationship between a FollowSystem::Follower object and a FollowSystem::Followee object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/follow_system/follow.rb', line 70 def self.toggle_follow(follower, followee) validate_followee(followee) validate_follower(follower) if follows?(follower, followee) unfollow(follower, followee) else follow(follower, followee) end end |
.unfollow(follower, followee) ⇒ Boolean
Destroys a FollowSystem::Follow relationship between a FollowSystem::Follower object and a FollowSystem::Followee object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/follow_system/follow.rb', line 50 def self.unfollow(follower, followee) validate_followee(followee) validate_follower(follower) if follows?(follower, followee) follow = scope_by_follower(follower).scope_by_followee(followee).take follow.destroy true else false end end |
.validate_followee(followee) ⇒ Object (private)
Validates a followee object
141 142 143 |
# File 'lib/follow_system/follow.rb', line 141 def self.validate_followee(followee) raise ArgumentError.new unless followee.respond_to?(:is_followee?) && followee.is_followee? end |
.validate_follower(follower) ⇒ Object (private)
Validates a follower object
150 151 152 |
# File 'lib/follow_system/follow.rb', line 150 def self.validate_follower(follower) raise ArgumentError.new unless follower.respond_to?(:is_follower?) && follower.is_follower? end |