Module: FollowableBehaviour::Followable::InstanceMethods
- Defined in:
- lib/followable_behaviour/followable.rb
Instance Method Summary
collapse
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. user_followers == followers_by_type(‘User’) Allows magic names on followers_by_type_count e.g. count_user_followers == followers_by_type_count(‘User’)
49
50
51
52
53
54
55
56
57
|
# File 'lib/followable_behaviour/followable.rb', line 49
def method_missing(m, *args)
if m.to_s[/count_(.+)_followers/]
followers_by_type_count($1.singularize.classify)
elsif m.to_s[/(.+)_followers/]
followers_by_type($1.singularize.classify)
else
super
end
end
|
Instance Method Details
#block(follower) ⇒ Object
90
91
92
93
|
# File 'lib/followable_behaviour/followable.rb', line 90
def block(follower)
return if follower == self
get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
end
|
#blocked_followers_count ⇒ Object
63
64
65
|
# File 'lib/followable_behaviour/followable.rb', line 63
def blocked_followers_count
self.followings.blocked.count
end
|
#blocks(options = {}) ⇒ Object
78
79
80
81
82
|
# File 'lib/followable_behaviour/followable.rb', line 78
def blocks(options={})
blocked_followers_scope = followers_scoped.blocked
blocked_followers_scope = apply_options_to_scope(blocked_followers_scope, options)
blocked_followers_scope.to_a.collect{|f| f.follower}
end
|
#followed_by?(follower) ⇒ Boolean
Returns true if the current instance is followed by the passed record Returns false if the current instance is blocked by the passed record or no follow is found
86
87
88
|
# File 'lib/followable_behaviour/followable.rb', line 86
def followed_by?(follower)
self.followings.unblocked.for_follower(follower).first.present?
end
|
#followers(options = {}) ⇒ Object
72
73
74
75
76
|
# File 'lib/followable_behaviour/followable.rb', line 72
def followers(options={})
followers_scope = followers_scoped.unblocked
followers_scope = apply_options_to_scope(followers_scope, options)
followers_scope.to_a.collect{|f| f.follower}
end
|
#followers_by_type(follower_type, options = {}) ⇒ Object
Returns the followers by a given type
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/followable_behaviour/followable.rb', line 25
def followers_by_type(follower_type, options={})
follows = follower_type.constantize.
joins(:follows).
where('follows.blocked' => false,
'follows.followable_id' => self.id,
'follows.followable_type' => parent_class_name(self),
'follows.follower_type' => follower_type)
if options.has_key?(:limit)
follows = follows.limit(options[:limit])
end
if options.has_key?(:includes)
follows = follows.includes(options[:includes])
end
follows
end
|
#followers_by_type_count(follower_type) ⇒ Object
41
42
43
|
# File 'lib/followable_behaviour/followable.rb', line 41
def followers_by_type_count(follower_type)
self.followings.unblocked.for_follower_type(follower_type).count
end
|
#followers_count ⇒ Object
Returns the number of followers a record has.
20
21
22
|
# File 'lib/followable_behaviour/followable.rb', line 20
def followers_count
self.followings.unblocked.count
end
|
#followers_scoped ⇒ Object
Returns the followings records scoped
68
69
70
|
# File 'lib/followable_behaviour/followable.rb', line 68
def followers_scoped
self.followings.includes(:follower)
end
|
#get_follow_for(follower) ⇒ Object
99
100
101
|
# File 'lib/followable_behaviour/followable.rb', line 99
def get_follow_for(follower)
self.followings.for_follower(follower).first
end
|
#respond_to?(m, include_private = false) ⇒ Boolean
59
60
61
|
# File 'lib/followable_behaviour/followable.rb', line 59
def respond_to?(m, include_private = false)
super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/]
end
|
#unblock(follower) ⇒ Object
95
96
97
|
# File 'lib/followable_behaviour/followable.rb', line 95
def unblock(follower)
get_follow_for(follower).try(:delete)
end
|