Module: ActsAsFollower::Followable::InstanceMethods

Defined in:
lib/acts_as_follower/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')


48
49
50
51
52
53
54
55
56
# File 'lib/acts_as_follower/followable.rb', line 48

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


89
90
91
# File 'lib/acts_as_follower/followable.rb', line 89

def block(follower)
  get_follow_for(follower) ? block_existing_follow(follower) : block_future_follow(follower)
end

#blocked_followers_countObject


62
63
64
# File 'lib/acts_as_follower/followable.rb', line 62

def blocked_followers_count
  self.followings.blocked.count
end

#blocks(options = {}) ⇒ Object


77
78
79
80
81
# File 'lib/acts_as_follower/followable.rb', line 77

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

Returns:

  • (Boolean)

85
86
87
# File 'lib/acts_as_follower/followable.rb', line 85

def followed_by?(follower)
  self.followings.unblocked.for_follower(follower).first.present?
end

#followers(options = {}) ⇒ Object


71
72
73
74
75
# File 'lib/acts_as_follower/followable.rb', line 71

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/acts_as_follower/followable.rb', line 24

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


40
41
42
# File 'lib/acts_as_follower/followable.rb', line 40

def followers_by_type_count(follower_type)
  self.followings.unblocked.for_follower_type(follower_type).count
end

#followers_countObject

Returns the number of followers a record has.


19
20
21
# File 'lib/acts_as_follower/followable.rb', line 19

def followers_count
  self.followings.unblocked.count
end

#followers_scopedObject

Returns the followings records scoped


67
68
69
# File 'lib/acts_as_follower/followable.rb', line 67

def followers_scoped
  self.followings.includes(:follower)
end

#get_follow_for(follower) ⇒ Object


97
98
99
# File 'lib/acts_as_follower/followable.rb', line 97

def get_follow_for(follower)
  self.followings.for_follower(follower).first
end

#respond_to?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/acts_as_follower/followable.rb', line 58

def respond_to?(m, include_private = false)
  super || m.to_s[/count_(.+)_followers/] || m.to_s[/(.+)_followers/]
end

#unblock(follower) ⇒ Object


93
94
95
# File 'lib/acts_as_follower/followable.rb', line 93

def unblock(follower)
  get_follow_for(follower).try(:delete)
end