Module: Mongoid::Liker

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_likes/liker.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(missing_method, *args, &block) ⇒ Object (private)



127
128
129
130
131
132
133
134
135
# File 'lib/mongoid_likes/liker.rb', line 127

def method_missing(missing_method, *args, &block)
  if missing_method.to_s =~ /^(.+)_likes_count$/
    likes_count_by_model($1.camelize)
  elsif missing_method.to_s =~ /^all_(.+)_likes$/
    all_likes_by_model($1.camelize)
  else
    super
  end
end

Instance Method Details

#all_likesObject

view all selfs likes

Example:

> @joe.all_likes

> [@track]



93
94
95
# File 'lib/mongoid_likes/liker.rb', line 93

def all_likes
  get_likes_of(self)
end

#all_likes_by_model(model) ⇒ Object

view all selfs likes by model

Example:

> @joe.all_likes_by_model

> [@track]



102
103
104
# File 'lib/mongoid_likes/liker.rb', line 102

def all_likes_by_model(model)
  get_likes_of(self, model)
end

#common_likes_with(model) ⇒ Object

view all common likes of self against model

Example:

> @joe.common_likes_with(@max)

> [@track1, @track2]



111
112
113
114
115
116
# File 'lib/mongoid_likes/liker.rb', line 111

def common_likes_with(model)
  model_likes = get_likes_of(model)
  self_likes = get_likes_of(self)

  self_likes & model_likes
end

#like(model) ⇒ Object

like a model

Example:

> @joe.like(@track)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid_likes/liker.rb', line 14

def like(model)
  if self.id != model.id && !self.likes?(model)

    model.before_liked_by(self) if model.respond_to?('before_liked_by')
    model.likers_assoc.create!(:like_type => self.class.name, :like_id => self.id)
    model.inc(:liked_count_field, 1)
    model.after_liked_by(self) if model.respond_to?('after_liked_by')

    self.before_like(model) if self.respond_to?('before_like')
    self.likes_assoc.create!(:like_type => model.class.name, :like_id => model.id)
    self.inc(:likes_count_field, 1)
    self.after_like(model) if self.respond_to?('after_like')

    return true
  else
    return false
  end
end

#likes?(model) ⇒ Boolean

know if self is already liking model

Example:

> @joe.likes?(@tracks)

> true

Returns:

  • (Boolean)


65
66
67
# File 'lib/mongoid_likes/liker.rb', line 65

def likes?(model)
  self.likes_assoc.find(:all, conditions: {like_id: model.id}).limit(1).count > 0
end

#likes_countObject

get likes count Note: this is a cache counter

Example:

> @joe.likes_count

> 1



75
76
77
# File 'lib/mongoid_likes/liker.rb', line 75

def likes_count
  self.likes_count_field
end

#likes_count_by_model(model) ⇒ Object

get likes count by model

Example:

> @joe.likes_coun_by_model(User)

> 1



84
85
86
# File 'lib/mongoid_likes/liker.rb', line 84

def likes_count_by_model(model)
  self.likes_assoc.where(:like_type => model.to_s).count
end

#unlike(model) ⇒ Object

unlike a model

Example:

> @joe.unlike(@track)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongoid_likes/liker.rb', line 37

def unlike(model)
  if self.id != model.id && self.likes?(model)

    # this is necessary to handle mongodb caching on collection if unlike is following a like
    model.reload
    self.reload

    model.before_unliked_by(self) if model.respond_to?('before_unliked_by')
    model.likers_assoc.where(:like_type => self.class.name, :like_id => self.id).destroy
    model.inc(:liked_count_field, -1)
    model.after_unliked_by(self) if model.respond_to?('after_unliked_by')

    self.before_unlike(model) if self.respond_to?('before_unlike')
    self.likes_assoc.where(:like_type => model.class.name, :like_id => model.id).destroy
    self.inc(:likes_count_field, -1)
    self.after_unlike(model) if self.respond_to?('after_unlike')

    return true
  else
    return false
  end
end