Module: ActsAsFollowable::Followable::InstanceMethods
- Defined in:
- lib/acts_as_followable/followable.rb
Instance Method Summary collapse
-
#destroy_followers_by_type(type) ⇒ Object
Destroys all followers of a given type.
-
#destroy_followings_by_type(type) ⇒ Object
Destroys all followings of a given type.
-
#follow(followable) ⇒ Object
Creates a new follow record for this instance to follow the passed object.
-
#followed_by?(follower) ⇒ Boolean
Returns true if the current instance is followed by the passed record.
-
#followers_by_type(type) ⇒ Object
Returns the followers by a given type.
-
#following?(followable) ⇒ Boolean
Returns true if this instance is following the object passed as an argument.
-
#following_by_type(type) ⇒ Object
Returns the actual records of a particular type which this record is following.
-
#method_missing(m, *args) ⇒ Object
Allows magic names on followers_by_type e.g.
-
#stop_following(followable) ⇒ Object
Deletes the follow record if it exists.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Allows magic names on followers_by_type e.g. followers_by_type(‘User’) > user_followers e.g. following_by_type(‘User’) > following_user
36 37 38 39 40 41 42 43 44 |
# File 'lib/acts_as_followable/followable.rb', line 36 def method_missing(m, *args) if m.to_s[/(.+)_followers/] followers_by_type($1.singularize.classify) elsif m.to_s[/following_(.+)/] following_by_type($1.singularize.classify) else super end end |
Instance Method Details
#destroy_followers_by_type(type) ⇒ Object
Destroys all followers of a given type
68 69 70 |
# File 'lib/acts_as_followable/followable.rb', line 68 def destroy_followers_by_type(type) Follow.where(['follows.followable_id=? AND follows.followable_type=? AND follows.follower_type=?', self.id, class_name(self), type]).destroy_all end |
#destroy_followings_by_type(type) ⇒ Object
Destroys all followings of a given type
73 74 75 |
# File 'lib/acts_as_followable/followable.rb', line 73 def destroy_followings_by_type(type) Follow.where(['follows.follower_id=? AND follows.follower_type=? AND follows.followable_type=?', self.id, class_name(self), type]).destroy_all end |
#follow(followable) ⇒ Object
Creates a new follow record for this instance to follow the passed object. Does not allow duplicate records to be created.
48 49 50 51 52 53 |
# File 'lib/acts_as_followable/followable.rb', line 48 def follow(followable) follow = follow_for_follawable(followable).first if follow.blank? Follow.create(:followable_id => followable.id, :followable_type => class_name(followable), :follower_id => self.id, :follower_type => class_name(self)) end end |
#followed_by?(follower) ⇒ Boolean
Returns true if the current instance is followed by the passed record
78 79 80 |
# File 'lib/acts_as_followable/followable.rb', line 78 def followed_by?(follower) 0 < follow_for_follower(follower).limit(1).count end |
#followers_by_type(type) ⇒ Object
Returns the followers by a given type
24 25 26 |
# File 'lib/acts_as_followable/followable.rb', line 24 def followers_by_type(type) type.constantize.following(self) end |
#following?(followable) ⇒ Boolean
Returns true if this instance is following the object passed as an argument.
56 57 58 |
# File 'lib/acts_as_followable/followable.rb', line 56 def following?(followable) 0 < follow_for_follawable(followable).limit(1).count end |
#following_by_type(type) ⇒ Object
Returns the actual records of a particular type which this record is following.
29 30 31 |
# File 'lib/acts_as_followable/followable.rb', line 29 def following_by_type(type) type.constantize.followed_by(self) end |
#stop_following(followable) ⇒ Object
Deletes the follow record if it exists.
61 62 63 64 65 |
# File 'lib/acts_as_followable/followable.rb', line 61 def stop_following(followable) if follow = follow_for_follawable(followable).first follow.destroy end end |