Module: Likeable

Extended by:
ActiveSupport::Concern
Defined in:
lib/likeable.rb,
lib/likeable/facepile.rb,
lib/likeable/module_methods.rb,
lib/likeable/adapters/ohm_adapter.rb,
lib/likeable/adapters/default_adapter.rb,
lib/likeable/adapters/mongoid_adapter.rb

Defined Under Namespace

Modules: ClassMethods, DefaultAdapter, Facepile, MongoidAdapter, OhmAdapter, UserMethods Classes: Like

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cast_id(id) ⇒ Object



81
82
83
84
85
86
87
88
# 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

.find_many(klass, ids) ⇒ Object



90
91
92
93
# 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) ⇒ Object



95
96
97
98
# File 'lib/likeable/module_methods.rb', line 95

def find_one(klass, id)
  @find_one ||= DefaultAdapter.find_one
  @find_one.call(klass, id)
end

Class Method Details

.adapter=(adapter) ⇒ Object



75
76
77
78
79
# 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) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object



69
70
71
72
73
# 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

.classesObject



13
14
15
# File 'lib/likeable/module_methods.rb', line 13

def classes
  (@classes||[]).flatten
end

.classes=(*args) ⇒ Object



17
18
19
# File 'lib/likeable/module_methods.rb', line 17

def classes=(*args)
  @classes = args
end

.find_by_resource_id(resource_name, target_id) ⇒ Object

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



35
36
37
38
39
40
41
42
# 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

.get_class_for_resource_name(resource_name) ⇒ Object

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



47
48
49
# 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) ⇒ Object



51
52
53
# File 'lib/likeable/module_methods.rb', line 51

def get_resource_name_for_class(klass)
  klass
end

.model(target_model) ⇒ Object

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



25
26
27
28
29
# File 'lib/likeable/module_methods.rb', line 25

def model(target_model)
  target_model.camelcase.constantize
rescue NameError => ex
  return false
end

.redisObject



55
56
57
# File 'lib/likeable/module_methods.rb', line 55

def redis
  @redis ||= Redis.new
end

.redis=(redis) ⇒ Object



59
60
61
# File 'lib/likeable/module_methods.rb', line 59

def redis=(redis)
  @redis = redis
end

.setup {|_self| ... } ⇒ Object

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Likeable)

    the object that the method was called on



118
119
120
121
# File 'lib/likeable/module_methods.rb', line 118

def setup(&block)
  yield self unless block.blank?
  true
end

.user_classObject



100
101
102
103
104
105
106
# File 'lib/likeable/module_methods.rb', line 100

def user_class
  begin
    @user_class ||= ::User
  rescue NameError
    nil
  end
end

.user_class=(klass) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
# 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

Instance Method Details

#add_like_from(user, time = Time.now.to_f) ⇒ Object

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



25
26
27
28
29
30
31
32
# 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) ⇒ Object



40
41
42
# File 'lib/likeable.rb', line 40

def after_like(like)
  Likeable.after_like.call(like)
end

#after_unlike(user) ⇒ Object



54
55
56
# File 'lib/likeable.rb', line 54

def after_unlike(user)
  Likeable.after_unlike.call(user)
end

#clear_memoized_methods(*methods) ⇒ Object



34
35
36
37
38
# File 'lib/likeable.rb', line 34

def clear_memoized_methods(*methods)
  methods.each do |method|
    eval("@#{method} = nil")
  end
end

#destroy_all_likesObject



19
20
21
# File 'lib/likeable.rb', line 19

def destroy_all_likes
  liked_users.each {|user| self.remove_like_from(user) }
end

#like_countObject



58
59
60
# 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_idsObject

get all user ids that have liked a target object



63
64
65
# 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_nameObject



87
88
89
# File 'lib/likeable.rb', line 87

def likeable_resource_name
  Likeable.get_resource_name_for_class(self.class)
end

#liked_by?(user) ⇒ Boolean

did given user like the object

Returns:

  • (Boolean)


80
81
82
83
84
# 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) ⇒ Object



67
68
69
# File 'lib/likeable.rb', line 67

def liked_users(limit = nil)
  @liked_users ||= Likeable.find_many(Likeable.user_class, like_user_ids)
end

#likesObject



71
72
73
74
75
76
77
# 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) ⇒ Object

removes a like



45
46
47
48
49
50
51
52
# 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