Class: Socialization::ActiveRecordStores::Follow

Inherits:
ActiveRecord::Base show all
Extended by:
Mixins::Base, Stores::Mixins::Base, Stores::Mixins::Follow
Defined in:
lib/socialization/stores/active_record/follow.rb

Direct Known Subclasses

Follow

Class Method Summary collapse

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_subject?

Methods included from Stores::Mixins::Follow

after_follow, after_unfollow, touch

Methods included from Mixins::Base

update_counter

Methods inherited from ActiveRecord::Base

#is_followable?, #is_follower?, #is_likeable?, #is_liker?, #is_mentionable?, #is_mentioner?

Class Method Details

.follow!(follower, followable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/socialization/stores/active_record/follow.rb', line 22

def follow!(follower, followable)
  unless follows?(follower, followable)
    self.create! do |follow|
      follow.follower = follower
      follow.followable = followable
    end
    update_counter(follower, followees_count: +1)
    update_counter(followable, followers_count: +1)
    call_after_create_hooks(follower, followable)
    true
  else
    false
  end
end

.followables(follower, klass, opts = {}) ⇒ Object

Returns all the followables of a certain type that are followed by follower



91
92
93
# File 'lib/socialization/stores/active_record/follow.rb', line 91

def followables(follower, klass, opts = {})
  followables_relation(follower, klass, opts)
end

.followables_relation(follower, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the followables of a certain type that are followed by follower



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/socialization/stores/active_record/follow.rb', line 75

def followables_relation(follower, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:followable_id).
      where(:followable_type => klass.name.classify).
      where(:follower_type => follower.class.to_s).
      where(:follower_id => follower.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end

.followers(followable, klass, opts = {}) ⇒ Object

Returns all the followers of a certain type that are following followable



70
71
72
# File 'lib/socialization/stores/active_record/follow.rb', line 70

def followers(followable, klass, opts = {})
  followers_relation(followable, klass, opts)
end

.followers_relation(followable, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the followers of a certain type that are following followable



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/socialization/stores/active_record/follow.rb', line 54

def followers_relation(followable, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:follower_id).
      where(:follower_type => klass.name.classify).
      where(:followable_type => followable.class.to_s).
      where(:followable_id => followable.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end

.follows?(follower, followable) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/socialization/stores/active_record/follow.rb', line 49

def follows?(follower, followable)
  !follow_for(follower, followable).empty?
end

.remove_followables(follower) ⇒ Object

Remove all the followables for follower



102
103
104
105
# File 'lib/socialization/stores/active_record/follow.rb', line 102

def remove_followables(follower)
  self.where(:follower_type => follower.class.name.classify).
       where(:follower_id => follower.id).destroy_all
end

.remove_followers(followable) ⇒ Object

Remove all the followers for followable



96
97
98
99
# File 'lib/socialization/stores/active_record/follow.rb', line 96

def remove_followers(followable)
  self.where(:followable_type => followable.class.name.classify).
       where(:followable_id => followable.id).destroy_all
end

.unfollow!(follower, followable) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/socialization/stores/active_record/follow.rb', line 37

def unfollow!(follower, followable)
  if follows?(follower, followable)
    follow_for(follower, followable).destroy_all
    update_counter(follower, followees_count: -1)
    update_counter(followable, followers_count: -1)
    call_after_destroy_hooks(follower, followable)
    true
  else
    false
  end
end