Module: FollowableBehaviour::Follower::InstanceMethods

Defined in:
lib/followable_behaviour/follower.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 following_by_type e.g. following_users == following_by_type(‘User’) Allows magic names on following_by_type_count e.g. following_users_count == following_by_type_count(‘User’)



92
93
94
95
96
97
98
99
100
# File 'lib/followable_behaviour/follower.rb', line 92

def method_missing(m, *args)
  if m.to_s[/following_(.+)_count/]
    following_by_type_count($1.singularize.classify)
  elsif m.to_s[/following_(.+)/]
    following_by_type($1.singularize.classify)
  else
    super
  end
end

Instance Method Details

#all_following(options = {}) ⇒ Object

Returns the actual records which this instance is following.



63
64
65
# File 'lib/followable_behaviour/follower.rb', line 63

def all_following(options={})
  all_follows(options).collect{ |f| f.followable }
end

#all_follows(options = {}) ⇒ Object

Returns the follow records related to this instance with the followable included.



57
58
59
60
# File 'lib/followable_behaviour/follower.rb', line 57

def all_follows(options={})
  follows_scope = follows_scoped
  follows_scope = apply_options_to_scope(follows_scope, options)
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.



31
32
33
34
35
36
# File 'lib/followable_behaviour/follower.rb', line 31

def follow(followable)
  if self != followable
    params = {followable_id: followable.id, followable_type: parent_class_name(followable)}
    self.follows.where(params).first_or_create!
  end
end

#follow_countObject

Returns the number of objects this instance is following.



25
26
27
# File 'lib/followable_behaviour/follower.rb', line 25

def follow_count
  Follow.unblocked.for_follower(self).count
end

#following?(followable) ⇒ Boolean

Returns true if this instance is following the object passed as an argument.

Returns:

  • (Boolean)


20
21
22
# File 'lib/followable_behaviour/follower.rb', line 20

def following?(followable)
  0 < Follow.unblocked.for_follower(self).for_followable(followable).count
end

#following_by_type(followable_type, options = {}) ⇒ Object

Returns the actual records of a particular type which this record is following.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/followable_behaviour/follower.rb', line 68

def following_by_type(followable_type, options={})
  followables = followable_type.constantize.
    joins(:followings).
    where('follows.blocked'         => false,
          'follows.follower_id'     => self.id,
          'follows.follower_type'   => parent_class_name(self),
          'follows.followable_type' => followable_type)
  if options.has_key?(:limit)
    followables = followables.limit(options[:limit])
  end
  if options.has_key?(:includes)
    followables = followables.includes(options[:includes])
  end
  followables
end

#following_by_type_count(followable_type) ⇒ Object



84
85
86
# File 'lib/followable_behaviour/follower.rb', line 84

def following_by_type_count(followable_type)
  follows.unblocked.for_followable_type(followable_type).count
end

#follows_by_type(followable_type, options = {}) ⇒ Object

Returns the follow records related to this instance by type.



51
52
53
54
# File 'lib/followable_behaviour/follower.rb', line 51

def follows_by_type(followable_type, options={})
  follows_scope  = follows_scoped.for_followable_type(followable_type)
  follows_scope = apply_options_to_scope(follows_scope, options)
end

#follows_scopedObject

returns the follows records to the current instance



46
47
48
# File 'lib/followable_behaviour/follower.rb', line 46

def follows_scoped
  self.follows.unblocked.includes(:followable)
end

#get_follow(followable) ⇒ Object

Returns a follow record for the current instance and followable object.



107
108
109
# File 'lib/followable_behaviour/follower.rb', line 107

def get_follow(followable)
  self.follows.unblocked.for_followable(followable).first
end

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/followable_behaviour/follower.rb', line 102

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

#stop_following(followable) ⇒ Object

Deletes the follow record if it exists.



39
40
41
42
43
# File 'lib/followable_behaviour/follower.rb', line 39

def stop_following(followable)
  if follow = get_follow(followable)
    follow.destroy
  end
end