class Socialization::ActiveRecordStores::Like

Public Class Methods

like!(liker, likeable) click to toggle source
# File lib/socialization/stores/active_record/like.rb, line 22
def like!(liker, likeable)
  unless likes?(liker, likeable)
    self.create! do |like|
      like.liker = liker
      like.likeable = likeable
    end
    update_counter(liker, likees_count: +1)
    update_counter(likeable, likers_count: +1)
    call_after_create_hooks(liker, likeable)
    true
  else
    false
  end
end
likeables(liker, klass, opts = {}) click to toggle source

Returns all the likeables of a certain type that are liked by liker

# File lib/socialization/stores/active_record/like.rb, line 96
def likeables(liker, klass, opts = {})
  rel = likeables_relation(liker, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end
likeables_relation(liker, klass, opts = {}) click to toggle source

Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker

# File lib/socialization/stores/active_record/like.rb, line 80
def likeables_relation(liker, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:likeable_id).
      where(:likeable_type => klass.table_name.classify).
      where(:liker_type => liker.class.to_s).
      where(:liker_id => liker.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end
likers(likeable, klass, opts = {}) click to toggle source

Returns all the likers of a certain type that are liking likeable

# File lib/socialization/stores/active_record/like.rb, line 70
def likers(likeable, klass, opts = {})
  rel = likers_relation(likeable, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end
likers_relation(likeable, klass, opts = {}) click to toggle source

Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable

# File lib/socialization/stores/active_record/like.rb, line 54
def likers_relation(likeable, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:liker_id).
      where(:liker_type => klass.table_name.classify).
      where(:likeable_type => likeable.class.to_s).
      where(:likeable_id => likeable.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end
likes?(liker, likeable) click to toggle source
# File lib/socialization/stores/active_record/like.rb, line 49
def likes?(liker, likeable)
  !like_for(liker, likeable).empty?
end
remove_likeables(liker) click to toggle source

Remove all the likeables for liker

# File lib/socialization/stores/active_record/like.rb, line 112
def remove_likeables(liker)
  self.where(:liker_type => liker.class.name.classify).
       where(:liker_id => liker.id).destroy_all
end
remove_likers(likeable) click to toggle source

Remove all the likers for likeable

# File lib/socialization/stores/active_record/like.rb, line 106
def remove_likers(likeable)
  self.where(:likeable_type => likeable.class.name.classify).
       where(:likeable_id => likeable.id).destroy_all
end
unlike!(liker, likeable) click to toggle source
# File lib/socialization/stores/active_record/like.rb, line 37
def unlike!(liker, likeable)
  if likes?(liker, likeable)
    like_for(liker, likeable).destroy_all
    update_counter(liker, likees_count: -1)
    update_counter(likeable, likers_count: -1)
    call_after_destroy_hooks(liker, likeable)
    true
  else
    false
  end
end

Private Class Methods

like_for(liker, likeable) click to toggle source
# File lib/socialization/stores/active_record/like.rb, line 118
def like_for(liker, likeable)
  liked_by(liker).liking( likeable)
end