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::Follow

after_follow, after_unfollow, touch

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_victim?

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
# 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
    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


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

def followables(follower, klass, opts = {})
  rel = followables_relation(follower, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end

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

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


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

def followables_relation(follower, klass, opts = {})
  rel = klass.where(:id =>
    self.select(:followable_id).
      where(:followable_type => klass.table_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


66
67
68
69
70
71
72
73
# File 'lib/socialization/stores/active_record/follow.rb', line 66

def followers(followable, klass, opts = {})
  rel = followers_relation(followable, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end

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

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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/socialization/stores/active_record/follow.rb', line 50

def followers_relation(followable, klass, opts = {})
  rel = klass.where(:id =>
    self.select(:follower_id).
      where(:follower_type => klass.table_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)

45
46
47
# File 'lib/socialization/stores/active_record/follow.rb', line 45

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

.remove_followables(follower) ⇒ Object

Remove all the followables for follower


108
109
110
111
# File 'lib/socialization/stores/active_record/follow.rb', line 108

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


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

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

.unfollow!(follower, followable) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/socialization/stores/active_record/follow.rb', line 35

def unfollow!(follower, followable)
  if follows?(follower, followable)
    follow_for(follower, followable).destroy_all
    call_after_destroy_hooks(follower, followable)
    true
  else
    false
  end
end