module Likeable

Attributes

cast_id[W]
find_many[W]
find_one[W]

Public Class Methods

adapter=(adapter) click to toggle source
# File lib/likeable/module_methods.rb, line 75
def adapter=(adapter)
  self.find_one  = adapter.find_one
  self.find_many = adapter.find_many
  @adapter = adapter
end
after_like(&block) click to toggle source
# File lib/likeable/module_methods.rb, line 63
def after_like(&block)
  @after_like = block if block.present?
  @after_like ||= lambda {|like|}
  @after_like
end
after_unlike(&block) click to toggle source
# File lib/likeable/module_methods.rb, line 69
def after_unlike(&block)
  @after_unlike = block if block.present?
  @after_unlike ||= lambda {|like|}
  @after_unlike
end
cast_id(id) click to toggle source
# File lib/likeable/module_methods.rb, line 81
def cast_id(id)
  @cast_id ||= if @adapter && @adapter.respond_to?(:cast_id)
    @adapter.cast_id
  else
    DefaultAdapter.cast_id
  end
  @cast_id.call(id)
end
classes() click to toggle source
# File lib/likeable/module_methods.rb, line 13
def classes
  (@classes||[]).flatten
end
classes=(*args) click to toggle source
# File lib/likeable/module_methods.rb, line 17
def classes=(*args)
  @classes = args
end
find_by_resource_id(resource_name, target_id) click to toggle source

Likeable.find_by_resource_id(“highlight”, 22) —————————————- # #<Highlight id: … # if highlight 22 exists nil # if highlight 22 does not exist

# File lib/likeable/module_methods.rb, line 35
def find_by_resource_id(resource_name, target_id)
  target = Likeable.get_class_for_resource_name(resource_name)
  if target.present?
    Likeable.find_one(target, target_id)
  else
    false
  end
end
find_many(klass, ids) click to toggle source
# File lib/likeable/module_methods.rb, line 90
def find_many(klass, ids)
  @find_many ||= DefaultAdapter.find_many
  @find_many.call(klass, ids)
end
find_one(klass, id) click to toggle source
# File lib/likeable/module_methods.rb, line 95
def find_one(klass, id)
  @find_one ||= DefaultAdapter.find_one
  @find_one.call(klass, id)
end
get_class_for_resource_name(resource_name) click to toggle source

Likeable.get_class_for_resource_name('photo') ————————- # Returns the class for the resource name

# File lib/likeable/module_methods.rb, line 47
def get_class_for_resource_name(resource_name)
  self.model(resource_name)
end
get_resource_name_for_class(klass) click to toggle source
# File lib/likeable/module_methods.rb, line 51
def get_resource_name_for_class(klass)
  klass
end
model(target_model) click to toggle source

Likeable.model(“Highlight”) ————————- # turns a string into a model “Highlight”.constantize # => Highlight; “Hi1i6ht”.constantize = #=> false

# File lib/likeable/module_methods.rb, line 25
def model(target_model)
  target_model.camelcase.constantize
rescue NameError => ex
  return false
end
redis() click to toggle source
# File lib/likeable/module_methods.rb, line 55
def redis
  @redis ||= Redis.new
end
redis=(redis) click to toggle source
# File lib/likeable/module_methods.rb, line 59
def redis=(redis)
  @redis = redis
end
setup() { |self| ... } click to toggle source

Likeable.setup do |like|

like.redis     = Redis.new(#...)
like.find_one  = lambda {|klass, id | klass.where(:id => id)}
like.find_many = lambda {|klass, ids| klass.where(:id => ids)}

end

# File lib/likeable/module_methods.rb, line 118
def setup(&block)
  yield self unless block.blank?
  true
end
user_class() click to toggle source
# File lib/likeable/module_methods.rb, line 100
def user_class
  begin
    @user_class ||= ::User
  rescue NameError
    nil
  end
end
user_class=(klass) click to toggle source
# File lib/likeable/module_methods.rb, line 108
def user_class=(klass)
  raise ArgumentError, "Argument must be a class" unless klass.is_a?(Class)
  @user_class = klass
end

Public Instance Methods

add_like_from(user, time = Time.now.to_f) click to toggle source

create a like the user who created the like has a reference to the object liked

# File lib/likeable.rb, line 25
def add_like_from(user, time = Time.now.to_f)
  Likeable.redis.hset(like_key, user.id, time)
  Likeable.redis.hset(user.like_key(self.class.to_s.downcase), self.id, time)
  like = Like.new(:target => self, :user => user, :time => time)
  after_like(like)
  clear_memoized_methods(:like_count, :like_user_ids, :liked_user_ids, :liked_users, :likes)
  like
end
after_like(like) click to toggle source
# File lib/likeable.rb, line 40
def after_like(like)
  Likeable.after_like.call(like)
end
after_unlike(user) click to toggle source
# File lib/likeable.rb, line 54
def after_unlike(user)
  Likeable.after_unlike.call(user)
end
clear_memoized_methods(*methods) click to toggle source
# File lib/likeable.rb, line 34
def clear_memoized_methods(*methods)
  methods.each do |method|
    eval("@#{method} = nil")
  end
end
destroy_all_likes() click to toggle source
# File lib/likeable.rb, line 19
def destroy_all_likes
  liked_users.each {|user| self.remove_like_from(user) }
end
like_count() click to toggle source
# File lib/likeable.rb, line 58
def like_count
  @like_count ||= @like_user_ids.try(:count) || @likes.try(:count) || Likeable.redis.hlen(like_key)
end
like_user_ids() click to toggle source

get all user ids that have liked a target object

# File lib/likeable.rb, line 63
def like_user_ids
  @like_user_ids ||= (Likeable.redis.hkeys(like_key)||[]).map {|id| Likeable.cast_id(id)}
end
likeable_resource_name() click to toggle source
# File lib/likeable.rb, line 87
def likeable_resource_name
  Likeable.get_resource_name_for_class(self.class)
end
liked_by?(user) click to toggle source

did given user like the object

# File lib/likeable.rb, line 80
def liked_by?(user)
  return false unless user
  liked_by =    @like_user_ids.include?(Likeable.cast_id(user.id)) if @like_user_ids
  liked_by ||=  true & Likeable.redis.hexists(like_key, user.id)
end
liked_users(limit = nil) click to toggle source
# File lib/likeable.rb, line 67
def liked_users(limit = nil)
  @liked_users ||= Likeable.find_many(Likeable.user_class, like_user_ids)
end
likes() click to toggle source
# File lib/likeable.rb, line 71
def likes
  @likes ||= begin
    Likeable.redis.hgetall(like_key).collect do |user_id, time|
      Like.new(:user_id => user_id, :time => time, :target => self)
    end
  end
end
remove_like_from(user) click to toggle source

removes a like

# File lib/likeable.rb, line 45
def remove_like_from(user)
  if Likeable.redis.hexists(like_key, user.id)
    Likeable.redis.hdel(like_key, user.id)
    Likeable.redis.hdel(user.like_key(self.class.to_s.downcase), self.id)
    after_unlike(user)
    clear_memoized_methods(:like_count, :like_user_ids, :liked_user_ids, :liked_users)
  end
end