class Socialization::ActiveRecordStores::Follow

Public Class Methods

follow!(follower, followable) click to toggle source
# 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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/follow.rb, line 96
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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/follow.rb, line 80
def followables_relation(follower, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/follow.rb, line 70
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 = {}) click to toggle source

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

# 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.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) click to toggle source
# File lib/socialization/stores/active_record/follow.rb, line 49
def follows?(follower, followable)
  !follow_for(follower, followable).empty?
end
remove_followables(follower) click to toggle source

Remove all the followables for follower

# File lib/socialization/stores/active_record/follow.rb, line 112
def remove_followables(follower)
  self.where(:follower_type => follower.class.name.classify).
       where(:follower_id => follower.id).destroy_all
end
remove_followers(followable) click to toggle source

Remove all the followers for followable

# File lib/socialization/stores/active_record/follow.rb, line 106
def remove_followers(followable)
  self.where(:followable_type => followable.class.name.classify).
       where(:followable_id => followable.id).destroy_all
end
unfollow!(follower, followable) click to toggle source
# 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

Private Class Methods

follow_for(follower, followable) click to toggle source
# File lib/socialization/stores/active_record/follow.rb, line 118
def follow_for(follower, followable)
  followed_by(follower).following(followable)
end